I need to process listed content passed to a \foreach loop inside a tikz environment. But \savebox seems not to behave the same as outside tikz.
In particular, the following code:
\documentclass{report}
\usepackage{tikz}
\newsavebox{\aBox}
\newcommand{\withoutTikz}[1]{%
\foreach \content [count=\i] in {#1} {%
\savebox{\aBox}{\content}% store content in the box..
\i : \usebox{\aBox}% use the box
}%
}
\newcommand{\withinTikz}[1]{%
\tikz{% within tikz..
\foreach \content [count=\i] in {#1} {%
\savebox{\aBox}{\content}% store content in the box the same way..
\node at (0, -.5*\i) {\i : \usebox{\aBox}}; % .. but now it seems empty!
}%
}
}
\begin{document}
\withoutTikz{a,b,c}
\withinTikz{a,b,c}
\end{document}
produces:
Why? Am I missing anything?
How do I get this aBox content updated on each iteration within the tikz environment?
[EDIT:] Problem duplicated there. Based on marmot's answer there, here is how I have solved it :)
\newcommand{\tikzSaveBox}[2]{
\begin{pgfinterruptpicture}%
\begin{lrbox}{0\null\global\setbox\csname#1\endcsname}%
#2
\end{lrbox}%
\end{pgfinterruptpicture}
}
\newcommand{\withinTikz}[1]{%
\tikz{% within tikz..
\foreach \content [count=\i] in {#1} {%
\tikzSaveBox{aBox}{\content} % special case within tikz
\node at (0, -.5*\i) {\i : \usebox{\aBox}}; % .. now it works fine :)
}%
}
}

\newcommand{\withinTikz}[1]{% \tikz{% within tikz.. \foreach \content [count=\i] in {#1} {% \begin{pgfinterruptpicture}% \begin{lrbox}{0\null\global\setbox\aBox} % from https://tex.stackexchange.com/questions/49129/lrbox-in-newenvironment/49136#49136 \content \end{lrbox}% \end{pgfinterruptpicture} \node at (0, -.5*\i) {\i : \usebox{\aBox}}; % .. but now it seems empty! }% } }. – Sep 03 '18 at 16:37{\begin{pgfinterruptpicture}\global\savebox{\aBox}{\content}\end{pgfinterruptpicture}}it instead, but it seems not to make any difference.. How do I interrupt-and-globalize in this case? Thank you for informing about this gobbling process anyway :) Any reason why tikz does so? -_-" – iago-lito Sep 03 '18 at 16:37%s after\begin{pgfinterruptpicture}and\end{lrbox}for that reason. I guess you have the choice between the current behavior of TikZ and a bit more complicated syntax if you want to define\saveboxes inside atikzpictureor an orgy of%and less complicated\saveboxes. I'd go for the first option. – Sep 03 '18 at 16:42% https://tex.stackexchange.com/a/448581/72679 \newcommand{\tikzSaveBox}[2]{% \begin{pgfinterruptpicture}% \begin{lrbox}{0\null\global\setbox\csname#1\endcsname}% #2 \end{lrbox}% \end{pgfinterruptpicture} }, which seems to work well. Thank you for your patience anyway :) – iago-lito Sep 03 '18 at 16:44