2

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 '15 at 11:17
  • I don't think you wan't to have the \begin{lrbox} ... in the loop? – user31729 Sep 18 '15 at 11:24
  • Sorry, I do mean the \begin{lrbox} loop since I will need \PMOne tow \PMTen. Thanks! – Sebastian Sep 21 '15 at 8:19
  • It's very unclear what loop you're referring to. – egreg Sep 21 '15 at 8:26
  • I want to define lrboxes \PMOne to \PMTen without typing \begin{lrbox} ten times – Sebastian Sep 22 '15 at 8:55
2

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}
|improve this answer|||||
1

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}
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.