# Calling macros within \newsavebox

Can someone explain to me how I can include the \begin{lrbox} in the loop? My problem is that I do not know how to call the macro \PMOne or \PMTwo within the lrbox environment. A Minimal example:

\documentclass[english]{article}
\usepackage{tikz}
%
\foreach [count=\xi] \x in {One,Two} {
\expandafter\newsavebox\csname PM\x \endcsname
}
%
%
\begin{lrbox}{\PMOne}
\begin{tikzpicture}
\draw (1,0.5) node [right] {+1};
\draw (1,-0.5) node [right] {-1};
\draw[-] (0,0) -- node [above] {$+.5$} (1,0.5);
\draw[-] (0,0) -- node [below] {$-.5$} (1,-0.5);
\end{tikzpicture}
\end{lrbox}
%
\begin{lrbox}{\PMTwo}
\begin{tikzpicture}
\draw (1,0.5) node [right] {+2};
\draw (1,-0.5) node [right] {-2};
\draw[-] (0,0) -- node [above] {$.5$} (1,0.5);
\draw[-] (0,0) -- node [below] {$.5$} (1,-0.5);
\end{tikzpicture}
\end{lrbox}
%
\begin{document}
\foreach [count=\xi] \x in {One,Two} {%
\expandafter\usebox\csname PM\x\endcsname}
\end{document}

• You mean the loop over \usebox? \foreach [count=\xi] \x in {One,Two} {% \expandafter\usebox\csname PM\x\endcsname} perhaps?
– user31729
Sep 18, 2015 at 11:17
• I don't think you wan't to have the \begin{lrbox} ... in the loop?
– user31729
Sep 18, 2015 at 11:24
• Sorry, I do mean the \begin{lrbox} loop since I will need \PMOne tow \PMTen. Thanks! Sep 21, 2015 at 8:19
• It's very unclear what loop you're referring to. Sep 21, 2015 at 8:26
• I want to define lrboxes \PMOne to \PMTen without typing \begin{lrbox} ten times Sep 22, 2015 at 8:55

I am not sure if a lrbox is the right box type for usage within a foreach loop. I changed it to an ordinary \hbox.

\documentclass{article}
\usepackage{tikz}

\foreach [count=\xi] \x in {One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten} {
\expandafter\newbox\csname PM\x\endcsname
\global\setbox\csname PM\x\endcsname=\hbox{%
\begin{tikzpicture}
\draw (1,0.5) node [right] {+\xi};
\draw (1,-0.5) node [right] {-\xi};
\draw[-] (0,0) -- node [above] {$+.5$} (1,0.5);
\draw[-] (0,0) -- node [below] {$-.5$} (1,-0.5);
\end{tikzpicture}%
}
}

\begin{document}

\foreach [count=\xi] \x in {One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten} {%
\expandafter\usebox\csname PM\x\endcsname
}
\end{document}


The problem is always the same: \foreach executes each step in the cycle inside a group, so an assignment such as \begin{lrbox}{\PMOne}...\end{lrbox} disappears.

You can define a global version of lrbox:

\documentclass{article}
\usepackage{tikz}

\newenvironment{glrbox}[1]
{\def\thisbox{#1}\begin{lrbox}{0}}
{\end{lrbox}\global\setbox\thisbox=\box0\relax}

\foreach [count=\xi] \x in {One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten}
{
\expandafter\newsavebox\csname PM\x\endcsname
\begin{glrbox}{\csname PM\x\endcsname}
\begin{tikzpicture}
\draw (1,0.5) node [right] {$+\xi$};
\draw (1,-0.5) node [right] {$-\xi$};
\draw[-] (0,0) -- node [above] {$+.5$} (1,0.5);
\draw[-] (0,0) -- node [below] {$-.5$} (1,-0.5);
\end{tikzpicture}
\end{glrbox}
}

\begin{document}

\foreach [count=\xi] \x in {One,Two,Three,Four,Five,Six,Seven,Eight,Nine,Ten}
{%
\expandafter\usebox\csname PM\x\endcsname\par
}
\end{document}