是否有一个(简短的)解决方案,以便一个标签由多个键定义?
我猜一个解决方案可能是使用 if 语句(我不确定这是否可行)。
另一个解决方案可能是类似的(我不确定换行符......):
\tikzset{%
mylabel/.style = {label={[red]right:{}}},%
one/.style args = {#1}{mylabel.append(O:#1)},%
two/.style args = {#1}{mylabel.append(T:#1)}}
梅威瑟:
\documentclass[tikz]{standalone}
\tikzset{%
one/.style args = {#1}{label={[red]right:O:#1}},%
two/.style args = {#1}{label={[red]right:T:#1}}}
\begin{document}
\begin{tikzpicture}
\node at (0,2) [draw,one=A] {};
\node at (0,1) [draw,two=B] {};
\node at (0,0) [draw,one=C,two=D] {}; %this should automatically look like below
\node at (0,-1) [draw,one=C,label={[red,yshift=-1em]right:T:D}] {};
\end{tikzpicture}
\end{document}
答案1
这也可以使用 pgfkeys 来完成,但 TeX 的语法更可预测。
\documentclass[tikz]{standalone}
\def\prepare{coordinate(X)}
\def\one#1{(X.south west)node(X)[right]{O:#1}}
\def\two#1{(X.south west)node(X)[right]{T:#1}}
\begin{document}
\begin{tikzpicture}
\path
(0,2) node [draw] {} \prepare\one{A}
(0,1) node [draw] {} \prepare\two{B}
(0,0) node [draw] {} \prepare\one{A}\two{B} ;
\end{tikzpicture}
\end{document}
答案2
这是一个可能的解决方案(不是很好,因为使用了全局变量\labelshift
):
\documentclass[tikz, border=7pt, convert={density=2100}]{standalone}
\tikzset{
checkbox/.style = {draw},
checkbox/.append code={\xdef\labelshift{-1}},
plus/.code={\pgfmathsetmacro\labelshift{\labelshift+1}\xdef\labelshift{\labelshift}},
next/.style={label={right,yshift=-\labelshift*4mm,plus:#1}},
one/.style = {every label/.style=red,next={O:#1}},
two/.style = {next={T:#1}}
}
\begin{document}
\begin{tikzpicture}
\node at (0,2) [checkbox,one=A] {};
\node at (0,1) [checkbox,two=B] {};
\node at (0,0) [checkbox,one=C,two=D,one=E] {};
\end{tikzpicture}
\end{document}