2

I am trying to learn Tikz and I want to know if there is a way to optimize this 2 graphs. Thanks.

\documentclass[11pt]{article}    
\usepackage{tikz}       
\begin{document}


\begin{figure}[ht]
\centering
\begin{tikzpicture}[y=.6cm, x=.6cm,font=\small]

\draw [dashed,-latex](-1,1)  --  (1,1);
\draw [-latex](1,1)  --  (3,1);
\draw [-latex](3,1)  --  (5,1);
\draw [-latex](5,1)  --  (7,1);
\draw [-latex](7,1)  --  (9,1);
\draw [dashed,-latex](9,1)  --  (11,1);

\draw [dashed,-latex](-1,3)  --  (1,3);
\draw [-latex](1,3)  --  (3,3);
\draw [-latex](3,3)  --  (5,3);
\draw [-latex](5,3)  --  (7,3);
\draw [-latex](7,3)  --  (9,3);
\draw [dashed,-latex](9,3)  --  (11,3);

\draw [dashed,-latex](-1,5)  --  (1,5);
\draw [-latex](1,5)  --  (3,5);
\draw [-latex](3,5)  --  (5,5);
\draw [-latex](5,5)  --  (7,5);
\draw [-latex](7,5)  --  (9,5);
\draw [dashed,-latex](9,5)  --  (11,5);

\draw [dashed,-latex](-1,7)  --  (1,7);
\draw [-latex](1,7)  --  (3,7);
\draw [-latex](3,7)  --  (5,7);
\draw [-latex](5,7)  --  (7,7);
\draw [-latex](7,7)  --  (9,7);
\draw [dashed,-latex](9,7)  --  (11,7);

\draw [dashed,-latex,blue](1,-1)  --  (1,1);
\draw [-latex,blue](1,1)  --  (1,3);
\draw [-latex,blue](1,3)  --  (1,5);
\draw [-latex,blue](1,5)  --  (1,7);
\draw [dashed,-latex,blue](1,7)  --  (1,9);

\draw [dashed,-latex,blue](3,-1)  --  (3,1);
\draw [-latex,blue](3,1)  --  (3,3);
\draw [-latex,blue](3,3)  --  (3,5);
\draw [-latex,blue](3,5)  --  (3,7);
\draw [dashed,-latex,blue](3,7)  --  (3,9);

\draw [dashed,-latex,blue](5,-1)  --  (5,1);
\draw [-latex,blue](5,1)  --  (5,3);
\draw [-latex,blue](5,3)  --  (5,5);
\draw [-latex,blue](5,5)  --  (5,7);
\draw [dashed,-latex,blue](5,7)  --  (5,9);

\draw [dashed,-latex,blue](7,-1)  --  (7,1);
\draw [-latex,blue](7,1)  --  (7,3);
\draw [-latex,blue](7,3)  --  (7,5);
\draw [-latex,blue](7,5)  --  (7,7);
\draw [dashed,-latex,blue](7,7)  --  (7,9);

\draw [dashed,-latex,blue](9,-1)  --  (9,1);
\draw [-latex,blue](9,1)  --  (9,3);
\draw [-latex,blue](9,3)  --  (9,5);
\draw [-latex,blue](9,5)  --  (9,7);
\draw [dashed,-latex,blue](9,7)  --  (9,9);

\filldraw[fill=black!40,draw=black!80] (1,1) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (1,3) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (1,5) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (1,7) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (3,1) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (5,1) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (7,1) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (9,1) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (3,3) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (3,5) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (3,7) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (5,3) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (5,5) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (5,7) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (7,3) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (7,5) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (7,7) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (9,3) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (9,5) circle (2pt) node[anchor=east]{} ;
\filldraw[fill=black!40,draw=black!80] (9,7) circle (2pt) node[anchor=east]{} ;
\end{tikzpicture}
\end{figure}

%AND THIS OTHER


\begin{figure}[ht]
    \centering
\begin{tikzpicture}[y=.6cm, x=.6cm,font=\small]

\draw [latex-](3,1)  --  (1,3);
\draw [latex-](1,3)  --  (3,3);
\draw [latex-](3,3)  --  (3,1);

\draw [-latex](7,1)  --  (7,3);
\draw [-latex](7,3)  --  (9,3);
\draw [-latex](9,3)  --  (7,1);

\draw [-latex](7,7)  --  (9,7);
\draw [-latex](9,7)  --  (7,9);
\draw [-latex](7,9)  --  (7,7);

\draw [-latex](1,7)  --  (3,9);
\draw [-latex](3,9)  --  (3,7);
\draw [-latex](3,7)  --  (1,7);

\draw [latex-latex,dashed,red](3,7)  --  (7,3);
\draw [latex-latex,dashed,red](1,3)  --  (1,7);
\draw [latex-latex,dashed,red](3,1)  --  (7,1);
\draw [latex-latex,dashed,red](9,3)  --  (9,7);
\draw [latex-latex,dashed,red](7,9)  --  (3,9);
\draw [latex-latex,dashed,red](7,7)  --  (3,3);

\filldraw[fill=black!40,draw=black!80] (3,1) circle (2pt) node[anchor=north]{(243)} ;
\filldraw[fill=black!40,draw=black!80] (3,3) circle (2pt) node[anchor=west]{(143)} ;
\filldraw[fill=black!40,draw=black!80] (3,7) circle (2pt) node[anchor=west]{(132)} ;
\filldraw[fill=black!40,draw=black!80] (3,9) circle (2pt) node[anchor=east]{(123)} ;

\filldraw[fill=black!40,draw=black!80] (7,1) circle (2pt) node[anchor=north]{(142)} ;
\filldraw[fill=black!40,draw=black!80] (7,3) circle (2pt) node[anchor=east]{(234)} ;
\filldraw[fill=black!40,draw=black!80] (7,7) circle (2pt) node[anchor=east]{(124)} ;
\filldraw[fill=black!40,draw=black!80] (7,9) circle (2pt) node[anchor=west]{(134)} ;

\filldraw[fill=black!40,draw=black!80] (1,3) circle (2pt) node[anchor=east]{(12)(34)} ;
\filldraw[fill=black!40,draw=black!80] (1,7) circle (2pt) node[anchor=east]{e} ;
\filldraw[fill=black!40,draw=black!80] (9,3) circle (2pt) node[anchor=west]{(13)(24)} ;
\filldraw[fill=black!40,draw=black!80] (9,7) circle (2pt) node[anchor=west]{(14)(23)} ;
\end{tikzpicture}


\end{figure}



\end{document}

I know it is large but I am trying to know how to write in this language. Also if you know a concise page or book to learn how to use Tikz, it would help me a lot. Thanks.

  • It is rather easy to simplify it but the most immediate simplification arises from introducing \foreach loops. They are described in the pgfmanual in section 87 Repeating Things: The Foreach Statement. – marmot Feb 27 at 0:17
4

How can one avoid repetition? There are many ways, but here I focus on

  • \foreach loops,
  • styles,
  • pics

The first figure can be drawn with two nested \foreach loops, in which the repeating directives are stored in styles. The second figure uses a pic with named subnodes that get rotated.

\documentclass[11pt]{article}    
\usepackage{tikz}       

\begin{document}
\begin{figure}[ht]
\centering
\begin{tikzpicture}[y=.6cm, x=.6cm,font=\small,bullet/.style={inner sep=0pt,
 minimum size=4pt,circle,fill=black!40,draw=black!80}]
\pgfmathsetmacro{\Xmax}{5}
\pgfmathsetmacro{\Ymax}{4}
\foreach \X [remember=\X as \LastX] in {1,...,\Xmax}
 {\foreach \Y [remember=\Y as \LastY] in {1,...,\Ymax}
  {
   \node[bullet] (X\X\Y) at (2*\X,2*\Y){};
   \ifnum\Y>1
    \draw [-latex,blue]  (X\X\LastY) -- (X\X\Y);
   \else
    \draw [dashed,latex-,blue]  (X\X\Y) -- ++(0,-2);
   \fi
   \ifnum\X>1
    \draw [-latex]  (X\LastX\Y) -- (X\X\Y);
   \else
    \draw [dashed,latex-]  (X\X\Y) -- ++(-2,0);
   \fi
   \ifnum\Y=\Ymax
    \draw [dashed,-latex,blue]  (X\X\Y) -- ++(0,2);
   \fi
   \ifnum\X=\Xmax
    \draw [dashed,-latex]  (X\X\Y) -- ++(2,0);
   \fi
  }
 }
\end{tikzpicture}
\end{figure}

%AND THIS OTHER


\begin{figure}[ht]
\centering
\begin{tikzpicture}[y=.6cm, x=.6cm,font=\small,bullet/.style={inner sep=0pt,
 minimum size=4pt,circle,fill=black!40,draw=black!80}]
 \tikzset{pics/.cd,
 tricle/.style={code={
 \draw (0,0) node[bullet] (-A){} (2,0) node[bullet] (-B){} (0,2) node[bullet]
 (-C){};
 \draw[latex-] (-A) -- (-B);
 \draw[latex-] (-B) -- (-C);
 \draw[latex-] (-C) -- (-A);
 }}}
  \foreach \X in {1,...,4}
  {\path (-45+\X*90:2cm) pic[rotate=-90+\X*90] (T-\X) {tricle};}
  \foreach \X [remember=\X as \LastX (initially 4)] in {1,...,4}
  { \draw[dashed,red,latex-latex] (T-\X-B) -- (T-\LastX-C);}
  \foreach \X [count=\Y] in {{(134)},{e},{(243)},{(13)(24)}}
  { \node[anchor=\Y*90+135] at (T-\Y-C.{\Y*90-45}) {\X};}
  \foreach \X [count=\Y] in {{(14)(23)},{(123)},{(12)(34)},{(142)}}
  { \node[anchor=\Y*90+135] at (T-\Y-B.{\Y*90-45}) {\X};}
  \draw[dashed,red,latex-latex,text=black] (T-1-A) node[left] {(124)}
  -- (T-3-A) node[right]{(143)};
  \draw[dashed,red,latex-latex,text=black] (T-2-A) node[right] {(132)}
  -- (T-4-A) node[left]{(234)};
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

  • Oh ok, thanks. I have a question, if I want to change the size of the arrowhead, how I do it on that code? Because normaly I put [length=3mm] at the end of -latex, but here I don't know. – J.Rodriguez Feb 27 at 23:10
  • @J.Rodriguez All you need to do is to load \usetikzlibrary{arrows.meta} and then you can use the arrowhead Latex[length=3mm]. – marmot Feb 27 at 23:18
  • Ok, thanks. And one last question, what is the first X in (X\X\Y) in the first code? – J.Rodriguez Feb 27 at 23:43
  • @J.Rodriguez That's just a random label that I chose. You could replace the first X, i.e. the one without backslash, by cute furry rodent, and if you replace it everywhere, it will still work. (I just do not like using plain numbers because there might be confusion with the anchors.) – marmot Feb 28 at 0:10
1

First figure:

I created nodes and not points as in your code because it allows you to draw arrows that go from edge to edge without crossing the nodes.

Second figure:

Not having understood the logic of the textual numbering of the nodes in the second figure, it is possible to simplify the code of the creation of the nodes, but not that of their text. Unless you explain this numbering logic.

\documentclass[tikz,border=5mm]{standalone}    

\begin{document}

\tikzset{noeud/.style={circle,inner sep=0pt,minimum size=4pt,outer sep=0pt}}
\begin{tikzpicture}[y=.6cm, x=.6cm,font=\small,tips= on proper draw]

% Creation of nodes
\foreach \x in {-1,1,...,9,11}{
    \foreach \y in {-1,1,...,9,11}{
        \node[noeud] at (\x,\y)(\x-\y){};
        }
    }
% Inside arrows between the nodes.  
\foreach \x [remember =\x as \lastx (initially 1)] in {1,3,...,9}{
    \foreach \y [remember =\y as \lasty (initially 1)] in {1,3,...,9}{
            \node[noeud,draw,fill=black!40,draw=black!80] at (\x,\y){};
            \draw[-latex] (\lastx-\y)--(\x-\y);
            \draw[-latex,blue](\x-\lasty)--(\x-\y);
            }
    }       

% Horizontal external arrows.
\foreach \x [evaluate=\x as \lastx using int(\x-2)] in {1,11}{
    \foreach \y in {1,3,...,9}{
    \draw[dashed,-latex](\lastx-\y)--(\x-\y);
    }
}

% Vertical external arrows. 
\foreach \y [evaluate=\y as \lasty using int(\y-2)] in {1,11}{
    \foreach \x in {1,3,...,9}{
    \draw[dashed,-latex,blue](\x-\lasty)--(\x-\y);
    }
}

\end{tikzpicture}

\end{document}

screenshot

  • Jmm is not that easy to understand the labeling of the second graph, that one is the Cayley's Graph of the alternating group ($A_4$) based on the generators {(123),(12)(34)}. So it is not so easy to explain. But thanks. – J.Rodriguez Feb 27 at 23:14
  • On this site, it is asking to vote for the answers instead of saying thank you in the comments. Read: tex.stackexchange.com/help/someone-answers – AndréC Feb 28 at 6:05
  • 1
    Yes, as I did on the answer of marmot. – J.Rodriguez Mar 1 at 2:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.