将 tikzpictures 与方程式和用户定义的命令对齐

将 tikzpictures 与方程式和用户定义的命令对齐

我正在尝试将下图与中心对齐。

失败

不知怎的,我就是做不到。我试过scopebaseline甚至\vbox用包裹\begin{matrix}\end{matrix}。它们都给出与上面相同的结果。

这是我使用的代码

\begin{equation}
        \begin{tikzpicture}
        \node[scale=0.6] at (0.3,-0.2) {5};
        \node[scale=0.6] at (0.6,-0.2) {3};
        \node[scale=0.6] at (0.9,-0.2) {4};
        \node[scale=0.6] at (1.2,-0.2) {1};
        \node[scale=0.6] at (1.5,-0.2) {2};
        \node[scale=0.6] at (0.3,0.9) {1};
        \node[scale=0.6] at (0.6,0.9) {2};
        \node[scale=0.6] at (0.9,0.9) {3};
        \node[scale=0.6] at (1.2,0.9) {4};
        \node[scale=0.6] at (1.5,0.9) {5};
        \PlanarDiagram(5){{1/3,2/4,3/1,4/2,5/5}};
        \end{tikzpicture}
    =1
    \end{equation}

使用定义的命令latex 命令的参数数量不确定

\usepackage{tikz}
\usetikzlibrary{braids,backgrounds,arrows.meta,positioning,fit}
\tikzset{pics/planar/.style 2 args = {
        code = {
            \draw[color=red] (0,0) rectangle (#1*0.3+0.3,0.7);
            \foreach \dot in {1,...,#1}{ % draw the dots
                \filldraw (0.3*\dot,0) circle [radius=1pt];
                \filldraw (0.3*\dot,0.7) circle [radius=1pt];
            }
            % draw the lines
            \foreach \x/\y in #2
            \draw[->,>=stealth](0.3*\x,0) .. controls +(0,0.2) and +(0,-0.2) .. (0.3*\y,0.7);
        }
    }
}

\usepackage{xparse}
\NewDocumentCommand\PlanarDiagram{ O{} D(){3} m }{%
    \begin{tikzpicture}[#1]
    \foreach \diag [count=\c] in {#3} {
        \draw(0,\c*0.7) pic[#1]{planar={#2}{\diag}};
    }
    \end{tikzpicture}
}

我是否遗漏了什么或者我应该更改定义的命令?

答案1

与往常一样,有三条重要规则:

  1. 切勿嵌套tikzpicture
  2. 绝不!
  3. 如果您加载pgf,那么您就拥有了 LaTeX 世界中处理可选参数的最佳方法之一。(如果您不加载pgf,当然xparse对于可选参数来说也很棒。)

您可以将所有内容构建为一个,pic只需使用键即可处理所有变体。如果您稍后决定添加某些功能,请添加一个键。旧语法仍然有效。我刚刚show labels为您的 的(某些变体)添加了一个键pic

\documentclass{article}
\usepackage{tikz}
\newif\ifPlanarDiagamShowLabels
\usetikzlibrary{arrows.meta,bending}
\tikzset{pics/planar diagram/.style={code={
            \tikzset{planar diagram/.cd,#1}%
            \def\pv##1{\pgfkeysvalueof{/tikz/planar diagram/##1}}%
            \draw[/tikz/planar diagram/frame] ({-(\pv{n}+1)*\pv{x}/2},-\pv{y}/2) rectangle 
                ({(\pv{n}+1))*\pv{x}/2},\pv{y}/2);
            \ifPlanarDiagamShowLabels
              \path foreach \XX in {1,...,\pv{n}}
              {({-(\pv{n}+1)*\pv{x}/2+\XX*\pv{x}},-\pv{y}/2)
                   node[circle,fill,inner sep=1pt,label=below:$\XX$] (-b-\XX){}
               ({-(\pv{n}+1)*\pv{x}/2+\XX*\pv{x}},\pv{y}/2)
                   node[circle,fill,inner sep=1pt,label=above:$\XX$] (-t-\XX){}};
            \else
              \path foreach \XX in {1,...,\pv{n}}
              {({-(\pv{n}+1)*\pv{x}/2+\XX*\pv{x}},-\pv{y}/2)
                   node[circle,fill,inner sep=1pt] (-b-\XX){}
               ({-(\pv{n}+1)*\pv{x}/2+\XX*\pv{x}},\pv{y}/2)
                   node[circle,fill,inner sep=1pt] (-t-\XX){}};
            \fi
            \edef\localconnections{\pv{connections}}
            \foreach \XX/\YY in \localconnections{%
            \draw[-{Stealth[bend]}] (-b-\XX) to[out=90,in=-90] (-t-\YY);
            }
    }},planar diagram/.cd,n/.initial=5,x/.initial=0.3,y/.initial=0.7,
    show labels/.is if=PlanarDiagamShowLabels,frame/.style={},
    connections/.initial={1/1}
}

\begin{document}
\begin{equation}
  \begin{tikzpicture}[baseline={(X.base)}]
   \path node(X){\phantom{X}} 
   pic[nodes={scale=0.6},every circle node/.append style={scale=5/3}]
   {planar diagram={n=5,% number of nodes
        show labels,% show numbers
        frame/.style={draw=red},% red box
        connections={1/3,2/4,3/1,4/2,5/5}% which circles get connected
        }};      
  \end{tikzpicture}
    =1
\end{equation}


\[
\begin{tikzpicture}
\matrix[row sep=-3pt]{\pic{planar diagram={n=3,connections={3/1}}};\\
\pic{planar diagram={n=3,connections={2/3}}};\\
\pic{planar diagram={n=3,connections={2/2}}};\\
};
   \end{tikzpicture}
\]  

\end{document}

在此处输入图片描述

答案2

感谢薛定谔的猫,我又获得了一次机会。一旦我移除外层,我就可以使用原始代码了。

\documentclass[10pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{braids,backgrounds,arrows.meta,fit}
\tikzset{pics/planar/.style 2 args = {
        code = {
            \draw[color=red] (0,0) rectangle (#1*0.3+0.3,0.7);
            \foreach \dot in {1,...,#1}{ % draw the dots
                \filldraw (0.3*\dot,0) circle [radius=1pt];
                \filldraw (0.3*\dot,0.7) circle [radius=1pt];
            }
            % draw the lines
            \foreach \x/\y in #2
            \draw[->,>=stealth](0.3*\x,0) .. controls +(0,0.2) and +(0,-0.2) .. (0.3*\y,0.7);
        }
    }
}

\usepackage{xparse}
\NewDocumentCommand\PlanarDiagram{ O{} D(){3} m }{%
    \begin{tikzpicture}[#1]
    \foreach \diag [count=\c] in {#3} {
        \draw (0,\c*0.7) pic[#1]{planar={#2}{\diag}};
    }
    \end{tikzpicture}%
}

\NewDocumentCommand\planarDiagram{ O{} D(){3} m }{%
    \foreach \diag [count=\c] in {#3} {
        \draw (0,\c*0.7) pic[#1]{planar={#2}{\diag}};
    }
}


\begin{document}
        \begin{equation} 
        \begin{matrix}
        \begin{tikzpicture}
        \begin{scope}[shift={(0,-0.7)}]
        \planarDiagram(5){{1/3,2/4,3/1,4/2,5/5}};
        \end{scope}
        \node[scale=0.6] at (0.3,-0.2) {3};
        \node[scale=0.6] at (0.6,-0.2) {4};
        \node[scale=0.6] at (0.9,-0.2) {1};
        \node[scale=0.6] at (1.2,-0.2) {2};
        \node[scale=0.6] at (1.5,-0.2) {5};
        \node[scale=0.6] at (0.3,0.9) {5};
        \node[scale=0.6] at (0.6,0.9) {1};
        \node[scale=0.6] at (0.9,0.9) {2};
        \node[scale=0.6] at (1.2,0.9) {3};
        \node[scale=0.6] at (1.5,0.9) {4};

        \end{tikzpicture}
        \end{matrix}=1
        \end{equation}
\end{document}

相关内容