使用参数来外部化 tikz 图表

使用参数来外部化 tikz 图表

由于我的 Latex 文档涉及许多 tikz 图片,而且通常很相似,我定义了一个带有参数的命令,该命令使用由这些参数控制的“装饰”进行 tikz 绘图:

\diag{<-}{white}{black}{$n$}{$i$}{$j$}

生产在此处输入图片描述。这样可以快速更改 latex 文件中的装饰,并且可以在单个 tikz 代码中完成任何编辑。但是,我的文档中有很多图片,比如这些和其他图片,需要很长时间才能完成 latex。有没有办法在不牺牲当前设置的便利性的情况下将所有这些图片外部化?

代码:

\documentclass[11pt]{amsart}
\usepackage{expl3,tikz,ifthen}
\usetikzlibrary{positioning, math, decorations.markings, arrows.meta,calc,shapes.misc}
\usepgfmodule{decorations}
\tikzset{->-/.style n args={2}{decoration={markings, mark=at position #1 with {\arrow{#2}}}, postaction={decorate}}} %adding end=stealth will make the arrows thicker.  
\ExplSyntaxOn
 \cs_new_eq:NN \ifstreqF \str_if_eq:nnF
 \cs_new_eq:NN \ifstreqTF \str_if_eq:nnTF
\ExplSyntaxOff

\tikzset{-o-/.code 2 args={\ifstreqF{#2}{} %none}{\ifstreqTF{#2}{>}
  {\pgfkeysalso{decoration={markings,mark=at position #1 with {\arrow{#2}}}
                ,postaction={decorate}}
}
{\ifstreqTF{#2}{<}
   {\pgfkeysalso{decoration={markings,mark=at position #1 with {\arrow{#2}}}
                ,postaction={decorate}}
    }
   {\pgfkeysalso{decoration={markings,
                mark=at position #1 with 
                {\draw[black, fill={#2}] circle[radius=2pt];}}
                ,postaction={decorate}}
    }
 }
 }}} 

 \newcommand{\diag}[6]{
  %#1 arrow direction, #2,3 white, black, > or <, #4: label the central edge, #5,6 labels
 \raisebox{-0.4\height}{\begin{tikzpicture}
    \tikzmath{\xwidth=1.7;}
    \fill[gray!20!white] (0,0)rectangle(\xwidth,1);
    \tikzmath{\xw=\xwidth;}
    \ifthenelse{\equal{#1}{<-}}{\tikzmath{\yw=0.2;}}{
    \ifthenelse{\equal{#1}{->}}{\tikzmath{\yw=0;}}{\tikzmath{\yw=0.1;}}}
    \draw[very thick, #1] (\xw,0) --(\xw,1); %wall
    \tikzmath{\ya=\yw+0.6; \yb=\yw+0.2; \ycent=\ya/2+\yb/2;} 
    \coordinate[label={right:{\small #5}}] (A) at (\xw,\ya); 
    \coordinate[label={right:{\small #6}}] (B) at (\xw,\yb);
    \draw[thick, -o-={0.6}{#2}] (1.2,\ycent)-- (A);
    \draw[thick, -o-={0.6}{#3}] (1.2,\ycent)-- (B);
    \draw[thick] (0.5,\ycent) -- (1.2,\ycent);
    \coordinate[label={above:{\small #4}}] (B) at (0.85,\yb+0.2);
    \draw[thick, -o-={0.4}{#2}] (0,\ya) -- (0.5,\ycent);
    \draw[thick, -o-={0.4}{#3}] (0,\yb) -- (0.5,\ycent);
  \end{tikzpicture}
  }} 

\begin{document}
My diagram: \diag{<-}{white}{black}{$n$}{$i$}{$j$}
\end{document}

相关内容