我有相当多的节点 n1、n2、...、n41,它们都包含诸如\textcolor{c}{blabla}
和\large
等表达式。当将所有这些文本存储在描述形式的数组中时,似乎
tikzpicture 的字符串列表
即
\documentclass{article}
\usepackage{tikz}
\begin{document}
\newcommand\johnlist{{"ala","bla","cla"}}
\begin{tikzpicture}
\node[shape=circle,draw=black] at (0,0) {\pgfmathparse{\johnlist[1]}\pgfmathresult};
\end{tikzpicture}
\end{document}
如果例如将“bla”替换为,则不起作用\Large bla
。
那么问题是如何创建 TeX 兼容字符串的列表或数组?
答案1
我的印象是 PGF 中数组的处理非常脆弱。
\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\setlist}{mm}
{
\clist_clear_new:c { l_jens_#1_array_clist }
\clist_set:cn { l_jens_#1_array_clist } { #2 }
}
\NewExpandableDocumentCommand{\listitem}{mm}
{
\clist_item:cn { l_jens_#1_array_clist } { #2 }
}
\ExplSyntaxOff
\begin{document}
\setlist{john}{\textbf{ala},\Huge bla,\large cla}
\begin{tikzpicture}
\node[shape=circle,draw=black] at (0,0) {\listitem{john}{1}};
\node[shape=circle,draw=black] at (1,0) {\listitem{john}{2}};
\node[shape=circle,draw=black] at (2,0) {\listitem{john}{3}};
\end{tikzpicture}
\bigskip
\begin{tikzpicture}
\foreach \x in { 0,1,2 } {
\node[shape=circle,draw=black] at (\x,0) {\listitem{john}{\x+1}};
}
\end{tikzpicture}
\end{document}
请注意,您可以对 的第二个参数进行算术运算\listitem
。索引从 1 开始。
答案2
像这样吗?
\documentclass{article}
\usepackage{tikz}
\newcommand{\green}[1]{\textcolor{green}{#1}}
\begin{document}
\newcommand\johnlist{{"\noexpand\Huge ala","\noexpand\green{{bla}}","\noexpand\bfseries\noexpand\Large cla"}}
\begin{tikzpicture}
\foreach \i in {0,1,2}
{
\node[shape=circle,draw=black] at (\i,0) {\pgfmathparse{\johnlist[\i]}\pgfmathresult};
}
\end{tikzpicture}
\end{document}
答案3
您可以破解随机列表的内容,尽管指定项目需要括号而不是逗号,并且索引从 1 开始:
\documentclass[tikz,border=5]{standalone}
\def\pgfmathrandomlistitem#1#2{\pgfmathparse{int(#2)}%
\csname pgfmath@randomlist@#1@\pgfmathresult\endcsname}
\pgfmathdeclarerandomlist{list1}{%
{\tiny\textcolor{red}{ala}}
{\small\textcolor{green}{bla}}
{\large\textcolor{blue}{cla}}
}
\begin{document}
\begin{tikzpicture}
\foreach \i in {0,...,2}
\node [circle, draw] at (0, \i) {\pgfmathrandomlistitem{list1}{\i+1}};
\end{tikzpicture}
\end{document}