1

I'm trying to use the pic-functionality of tikz to draw some figures into a tabular (inspired by this answer here) which I might need to reuse later. But I'm doing something wrong, since I get a error suggesting I forgot a semicolon. I've tested the individual parts that are defined in the tikzset and they work, so I'm a bit confused what exactly is going wrong here.

\documentclass[tikz, border=1pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\usetikzlibrary{calc}
\usepackage{ifthen}

\newcommand{\drawlattice}{%
    \clip (-2,-2) rectangle (2,2);
    \begin{scope}[opacity=0.2, dashed]
        \foreach \x in {-2,-1,0,1,2}{
            \draw ($(-60:2) + (\x,0)$) -- ($(120:2)+(\x,0)$);
            \draw ($(60:2) + (\x,0)$) -- ($(-120:2)+(\x,0)$);
            \pgfmathsetmacro{\y}{{\x * sqrt(3)/2}}
            \draw ($(-2,0) + (0,\y)$) --  ($(2,0)+(0,\y) $); 
        }
    \end{scope}%
}

\tikzset{
    A2/.pic={
        \begin{tikzpicture}
            \drawlattice
            \node (alpha) at (1.2,0) {$\alpha$};
            \node (beta) at (120:1.2) {$\beta$};
            \node (ab) at (60:1.2) {$\alpha+\beta$};
            \foreach \angle in {0, 60,120,180,240,300}{
                \draw[->] (0,0) -- (\angle:1);
            }
        \end{tikzpicture}
    },
    B2/.pic={
        \begin{tikzpicture}
            \clip (-2,-2) rectangle (2,2);
            \begin{scope}[opacity=0.2, dashed]
                \foreach \x in {-2,-1,0,1,2}{
                    \draw ($(-45:2) + (\x,0)$) -- ($(135:2)+(\x,0)$);
                    \draw ($(45:2) + (\x,0)$) -- ($(-135:2)+(\x,0)$);
                    \draw ($(-2,0) + (0,\x)$) --  ($(2,0)+(0,\x) $); 
                }
            \end{scope}

            \node (alpha) at (1.2,0) {$\alpha$};
            \node (beta) at (3/4*180:1.6) {$\beta$};
            \foreach \angle in {0,45,90,135,180,225,270,315}{
                \pgfmathsetmacro{\bigradius}{sqrt(2)}
                \ifthenelse{\angle=45 \OR 
                            \angle=135 \OR 
                            \angle=225 \OR 
                            \angle=315}{
                                \draw[->] (0,0) -- (\angle:\bigradius);
                            }{
                                \draw[->] (0,0) -- (\angle:1);
                            };
            }
        \end{tikzpicture}
    },
    G2/.pic={
        \begin{tikzpicture}
            \drawlattice
            \node (alpha) at (1.2,0) {$\alpha$};
            \node (beta) at (150:1.9) {$\beta$};
            %\node (ab) at (120:1.2) {$\alpha+\beta$};
            \foreach \angle in {0,30,60,90,120,150,180,210,240,270,300,330}{
                \pgfmathsetmacro{\bigradius}{sqrt(3)}
                \ifthenelse{\angle=30 \OR 
                            \angle=90 \OR 
                            \angle=150 \OR 
                            \angle=210 \OR
                            \angle=270 \OR 
                            \angle=330}{
                                \draw[->] (0,0) -- (\angle:\bigradius);
                            }{
                                \draw[->] (0,0) -- (\angle:1);
                            };
            }
        \end{tikzpicture} 
    }}


\begin{document}

\begin{tabular}{cc}
        \tikz\pic{A2} & test \\
        \tikz\pic{B2} & \tikz\pic{G2}
    \end{tabular}

\end{document}
Sito
  • 681

1 Answers1

3

Your pics nest tikzpictures, i.e. if you define a pic you no longer need \begin{tikzpicture} and \end{tikzpicture}. In fact, if you keep them, you may get to see uncontrollable side effects. On way of arranging these three root lattices and the text in a table is to use a built-in matrix (which does not require the library of the same name).

\documentclass[tikz, border=1pt]{standalone}
\usetikzlibrary{calc}
\usepackage{ifthen}

\newcommand{\drawlattice}{%
    \clip (-2,-2) rectangle (2,2);
    \begin{scope}[opacity=0.2, dashed]
        \foreach \x in {-2,-1,0,1,2}{
            \draw ($(-60:2) + (\x,0)$) -- ($(120:2)+(\x,0)$);
            \draw ($(60:2) + (\x,0)$) -- ($(-120:2)+(\x,0)$);
            \pgfmathsetmacro{\y}{{\x * sqrt(3)/2}}
            \draw ($(-2,0) + (0,\y)$) --  ($(2,0)+(0,\y) $); 
        }
    \end{scope}%
}

\tikzset{pics/.cd,
    A2/.style={code={
            \drawlattice
            \node (alpha) at (1.2,0) {$\alpha$};
            \node (beta) at (120:1.2) {$\beta$};
            \node (ab) at (60:1.2) {$\alpha+\beta$};
            \foreach \angle in {0, 60,120,180,240,300}{
                \draw[->] (0,0) -- (\angle:1);
            }
    }},
    B2/.style={code={
            \clip (-2,-2) rectangle (2,2);
            \begin{scope}[opacity=0.2, dashed]
                \foreach \x in {-2,-1,0,1,2}{
                    \draw ($(-45:2) + (\x,0)$) -- ($(135:2)+(\x,0)$);
                    \draw ($(45:2) + (\x,0)$) -- ($(-135:2)+(\x,0)$);
                    \draw ($(-2,0) + (0,\x)$) --  ($(2,0)+(0,\x) $); 
                }
            \end{scope}
            \node (alpha) at (1.2,0) {$\alpha$};
            \node (beta) at (3/4*180:1.6) {$\beta$};
            \foreach \angle in {0,45,90,135,180,225,270,315}{
                \pgfmathsetmacro{\bigradius}{sqrt(2)}
                \ifthenelse{\angle=45 \OR 
                            \angle=135 \OR 
                            \angle=225 \OR 
                            \angle=315}{
                                \draw[->] (0,0) -- (\angle:\bigradius);
                            }{
                                \draw[->] (0,0) -- (\angle:1);
                            };
            }
    }},
    G2/.style={code={
            \drawlattice
            \node (alpha) at (1.2,0) {$\alpha$};
            \node (beta) at (150:1.9) {$\beta$};
            %\node (ab) at (120:1.2) {$\alpha+\beta$};
            \foreach \angle in {0,30,60,90,120,150,180,210,240,270,300,330}{
                \pgfmathsetmacro{\bigradius}{sqrt(3)}
                \ifthenelse{\angle=30 \OR 
                            \angle=90 \OR 
                            \angle=150 \OR 
                            \angle=210 \OR
                            \angle=270 \OR 
                            \angle=330}{
                                \draw[->] (0,0) -- (\angle:\bigradius);
                            }{
                                \draw[->] (0,0) -- (\angle:1);
                            };
            }}
    }}
\begin{document}
\begin{tikzpicture}
       \matrix {\pic{A2}; & \node{test}; \\
        \pic{B2}; & \pic{G2};\\
        };
\end{tikzpicture}

\end{document}

enter image description here

You absolutely can put tikzpictures in a tabular. The reason why your approach didn't work is that you have the documentclass

\documentclass[tikz, border=1pt]{standalone}

which instructs standalone to consider a tikzpicture as a standalone environment, i.e. it want to put each tikzpicture in a separate page. This is add odds with having them in a tabular. Removing tikz from the options cures this.

\documentclass[border=1pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{ifthen}

\newcommand{\drawlattice}{%
    \clip (-2,-2) rectangle (2,2);
    \begin{scope}[opacity=0.2, dashed]
        \foreach \x in {-2,-1,0,1,2}{
            \draw ($(-60:2) + (\x,0)$) -- ($(120:2)+(\x,0)$);
            \draw ($(60:2) + (\x,0)$) -- ($(-120:2)+(\x,0)$);
            \pgfmathsetmacro{\y}{{\x * sqrt(3)/2}}
            \draw ($(-2,0) + (0,\y)$) --  ($(2,0)+(0,\y) $); 
        }
    \end{scope}%
}

\tikzset{pics/.cd,
    A2/.style={code={
            \drawlattice
            \node (alpha) at (1.2,0) {$\alpha$};
            \node (beta) at (120:1.2) {$\beta$};
            \node (ab) at (60:1.2) {$\alpha+\beta$};
            \foreach \angle in {0, 60,120,180,240,300}{
                \draw[->] (0,0) -- (\angle:1);
            }
    }},
    B2/.style={code={
            \clip (-2,-2) rectangle (2,2);
            \begin{scope}[opacity=0.2, dashed]
                \foreach \x in {-2,-1,0,1,2}{
                    \draw ($(-45:2) + (\x,0)$) -- ($(135:2)+(\x,0)$);
                    \draw ($(45:2) + (\x,0)$) -- ($(-135:2)+(\x,0)$);
                    \draw ($(-2,0) + (0,\x)$) --  ($(2,0)+(0,\x) $); 
                }
            \end{scope}
            \node (alpha) at (1.2,0) {$\alpha$};
            \node (beta) at (3/4*180:1.6) {$\beta$};
            \foreach \angle in {0,45,90,135,180,225,270,315}{
                \pgfmathsetmacro{\bigradius}{sqrt(2)}
                \ifthenelse{\angle=45 \OR 
                            \angle=135 \OR 
                            \angle=225 \OR 
                            \angle=315}{
                                \draw[->] (0,0) -- (\angle:\bigradius);
                            }{
                                \draw[->] (0,0) -- (\angle:1);
                            };
            }
    }},
    G2/.style={code={
            \drawlattice
            \node (alpha) at (1.2,0) {$\alpha$};
            \node (beta) at (150:1.9) {$\beta$};
            %\node (ab) at (120:1.2) {$\alpha+\beta$};
            \foreach \angle in {0,30,60,90,120,150,180,210,240,270,300,330}{
                \pgfmathsetmacro{\bigradius}{sqrt(3)}
                \ifthenelse{\angle=30 \OR 
                            \angle=90 \OR 
                            \angle=150 \OR 
                            \angle=210 \OR
                            \angle=270 \OR 
                            \angle=330}{
                                \draw[->] (0,0) -- (\angle:\bigradius);
                            }{
                                \draw[->] (0,0) -- (\angle:1);
                            };
            }}
    }}
\begin{document}
\begin{tabular}{cc}
  \tikz{\pic{A2};} & test \\
  \tikz{\pic{B2};} & \tikz{\pic{G2};}\\
\end{tabular}

\end{document}

enter image description here