4

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:

no tikz boxes!

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 :)
     }%
}
}
iago-lito
  • 1,472
  • 2
    Because in TikZ pictures things get gobbled unless they are in a node, see this question. –  Sep 03 '18 at 16:27
  • 1
    You could make your command within TikZ work using e.g. \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
  • @marmot Jeez! Well, I have tried to {\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
  • @marmot Wow, this works indeed, cheers for that :) How come we end up with soo complicated patterns? X( – iago-lito Sep 03 '18 at 16:40
  • 1
    Please see my 2nd comment for how this can be made work. I guess one reason why TikZ wants to gobble these things is convenience: otherwise empty lines ore simple spaces will screw up every TikZ picture. In my comment above I also had to place %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 a tikzpicture or an orgy of % and less complicated \saveboxes. I'd go for the first option. –  Sep 03 '18 at 16:42
  • 1
    @marmot I see. I have improved readability a little bit with % 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
  • Yes, that's more structured. Do you have any objections of your question being closed as a duplicate? (A question being duplicate is does not mean that the question is of lower value, just someone else happened to ask that question earlier.) I won't close it without your permission. –  Sep 03 '18 at 16:52
  • @marmot No worries. Please proceed. I'll post my "more structured" command as an edit to your answer there, also only with your permission :) – iago-lito Sep 03 '18 at 16:54
  • No, please post an independent answer there. Of course, you might want to say that your answer is based on mine. Or append this trick simply to your question. (I never understood the duplicate thingy completely.) –  Sep 03 '18 at 16:59
  • @marmot Okay, I'll append it here then :) – iago-lito Sep 03 '18 at 17:00

0 Answers0