我想使用第一个 tizkset 的参数将它们提供给第二个 tikset。这是我的代码
\documentclass{article}
\usepackage{tikz}
\tikzset{pics/message/.style={code={
\draw (0,0) rectangle (4,1);
\draw foreach \X in {1,2,3} {(\X,0) -- (\X,1)};
%someone gave me two lines and i can't figure out how they work
\foreach \X [count=\Y,evaluate=\Y as \Z using {\Y-0.5}] in {#1} %1
{\node at (\Z,0.5) {\X}; %2
\ifnum\Y=4
\breakforeach
\fi}
}}}
\tikzset{pics/broadcast/.style={code={\begin{scope}[local bounding box=#1]
\draw (0,0) rectangle (6,5);
\pic at (1,3) {message={1,2,3,4}};
\end{scope}
}}}
\begin{document}
\begin{tikzpicture}
\pic at (0,0) {message={1,2,3,4}}; %instead it should be the params of broadcast
\pic at (0,3) {broadcast};
\end{tikzpicture}
\end{document}
画给我看这个:
我想这样调用矩形:
\pic at (0,0) {broadcast={1,2,3,4}};
在广播方法中更改此行时:
\pic at (1,3) {message={1,2,3,4}}; %actually
\pic at (1,3) {message={%every param of broadcast%}}; % What I want
我怎样才能做到这一点?
答案1
这是一项建议。它并非绝对万无一失。在目前的形式下,如果您调用消息图片并在之后调用广播图片,则该消息将被广播。要拦截广播,请\broadcastfalse
在消息图片后使用。
\documentclass{article}
\usepackage{tikz}
\newif\ifbroadcast
\broadcastfalse
\tikzset{pics/message/.style={code={\xdef\LastMessage{#1}
\draw (0,0) rectangle (4,1);
\draw foreach \X in {1,2,3} {(\X,0) -- (\X,1)};
%someone gave me two lines and i can't figure out how they work
\ifbroadcast
\foreach \X [count=\Y,evaluate=\Y as \Z using {\Y-0.5}] in #1 %1
{\node at (\Z,0.5) {\X}; %2
\ifnum\Y=4
\breakforeach
\fi}
\else
\foreach \X [count=\Y,evaluate=\Y as \Z using {\Y-0.5}] in {#1} %1
{\node at (\Z,0.5) {\X}; %2
\ifnum\Y=4
\breakforeach
\fi}
\fi
}}}
\tikzset{pics/broadcast/.style={code={\begin{scope}[local bounding box=#1]
\draw (0,0) rectangle (6,5);
\ifx\LastMessage\empty
\pic at (1,3) {message};
\else
\broadcasttrue
\pic at (1,3) {message=\LastMessage};
\fi
\end{scope}
}}}
\begin{document}
\begin{tikzpicture}
\pic at (0,0) {message={1,2,3,4}}; %instead it should be the params of broadcast
\pic at (0,3) {broadcast};
\end{tikzpicture}
\end{document}