使用 tikset 中的 pic 在表格环境中绘制图片

使用 tikset 中的 pic 在表格环境中绘制图片

我正在尝试使用pic功能tikz将一些图形绘制成表格(受此答案启发这里),我以后可能需要重复使用。但我做错了,因为我收到一个错误,提示我忘记了分号。我已经测试了定义在 中的各个部分,tikzset它们都正常工作,所以我有点困惑这里到底出了什么问题。

\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}

答案1

您的图片嵌套tikzpicture,即如果您定义了不再需要的图片\begin{tikzpicture}\end{tikzpicture}。事实上,如果您保留它们,您可能会看到无法控制的副作用。在表格中排列这三个根格子和文本的一种方法是使用内置矩阵(不需要同名的库)。

\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}

在此处输入图片描述

你绝对tikzpictures 放在表格中。你的方法不起作用的原因是你有 documentclass

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

它指示standalone将 视为tikzpicture独立环境,即希望将每个环境放在tikzpicture单独的页面中。这会增加将它们放在表格中的可能性。tikz从选项中删除可以解决这个问题。

\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}

在此处输入图片描述

相关内容