I want to automate some things in LaTeX using loops and saveboxes, but the boxes don't want to go automatically to the next line when there is no space for them (saveboxes are necessary). The code below is a simple example of what I'm trying to achieve (boxes are not created via loop, but I'm making it this way for the purpose of a simple demonstration). X will be some text
\usepackage{xsavebox}
\usepackage{pgffor}
\foreach \n in {3,...,11}{\xsbox{R.1.\n}{\textbf{R.1.\n}~X\hspace{2em}}}
...
\foreach \n in {3,...,11}{\xusebox{R.1.\n}}
Best Answer
You need to use
\global
before\setbox
, because\foreach
from pgf does each loop-step in a group. The local setting is forgotten when the group is leaved. And you want to use\penalty0
in order to allow a breaking point, because each\xsbox
exectes\leavevmode
before putting box, so you are in horizontal mode.