在 \newsavebox 中调用宏

在 \newsavebox 中调用宏

有人能向我解释如何将其包含\begin{lrbox}在循环中吗?我的问题是我不知道如何调用宏\PMOne\PMTwolrbox环境中调用。一个最小的例子:

\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}

答案1

我不确定 a 是否lrbox是适合在循环中使用的正确框类型foreach。我将其更改为普通的\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}

答案2

问题始终是相同的:\foreach在组内循环执行每一步,因此诸如此类的分配\begin{lrbox}{\PMOne}...\end{lrbox}就会消失。

您可以定义一个全局版本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}

相关内容