我正在编写一些代码,利用\shape
在代码过程中经过多次调整的维度(如下所述)。\shape
代码完成时的值是\shape
调用时使用的内容。(请参阅我之前的问题让代码在后面的代码中检索维度值?)
有没有办法稍微调整一下下面的代码,这样它的不同部分就可以与其余部分隔离开来,在这些部分中,\shape
进行本地调整,而不是全局调整,并且\shape
在这样的部分中调用时,它的值就是它在代码这部分结束时的值(而不是在整个代码的末尾)?
\documentclass{article}
\usepackage{tabularx}
\usepackage{showframe}
\setlength{\parindent}{0pt}
% The following ~dozen lines are taken from egreg's answer (https://tex.stackexchange.com/a/694439/277990) to a previous question of mine:
\newdimen\shape
\global\shape=0pt
\makeatletter
\AtEndDocument{\write\@auxout{\global\shape=\the\shape}}
\makeatother
\newcommand{\shifter}[1]{%
\settowidth{\dimen0}{#1}%
\ifdim\shape<\dimen0
\global\shape=\dimen0
\fi
}
\newcommand{\proofpiece}[1]{%
\begin{tabularx}{\linewidth}{r >$r<$ @{} >{\raggedright${}}X<{$} p{\shape}}
#1
\end{tabularx}
}%
\newcommand{\rcc}[1]{% ''rightmost column contents''
#1 \shifter{#1}
}
\begin{document}
The following has the output I desire, and the source code is satisfactory:
\proofpiece{
1. & \multicolumn{2}{l}{Math}
& \rcc{Words}\\
2. & m & = 9/3 & \rcc{Words}\\
3. & & = 3 & \rcc{Words}\\
}
\proofpiece{
4. & 34x+2 & = 30/2 & \rcc{Wider words}\\
5. & & = 15 & \rcc{Words}\\
}
\vspace{2cm}
However, suppose we write a second proof in the same document:
\proofpiece{
1. & \multicolumn{2}{l}{Math}
& \rcc{Words}\\
2. & m & = 2+2 & \rcc{Words}\\
3. & & = 4 & \rcc{Words}\\
}
\proofpiece{
4. & 34x+2 & = 16/2 & \rcc{Words}\\
5. & & = 8 & \rcc{Words}\\
}
Unfortunately, the width of the rightmost column is equal to the width of the widest entry in the {\em previous} proof (namely, ``Wider words'').
\end{document}
注意:我怀疑这是否相关,但 David Carlisle 的回答(https://tex.stackexchange.com/a/693585/277990) 对我之前的一个问题提供了为什么\proofpiece
会这样的原因。
答案1
为每个组使用不同的 dimen 寄存器,还要注意丢失未满的盒子警告%
\documentclass{article}
\usepackage{tabularx}
\usepackage{showframe}
\setlength{\parindent}{0pt}
% The following ~dozen lines are taken from egreg's answer (https://tex.stackexchange.com/a/694439/277990) to a previous question of mine:
\makeatletter
\newdimen\shapea
\AtEndDocument{\immediate\write\@auxout{\global\shapea=\the\shapea}}
\newdimen\shapeb
\AtEndDocument{\immediate\write\@auxout{\global\shapeb=\the\shapeb}}
\makeatother
\newcommand{\shifter}[1]{%
\settowidth{\dimen0}{#1}%
\ifdim\shape<\dimen0
\global\shape=\dimen0
\fi
}
\newcommand{\proofpiece}[2]{%
\let\shape#1%
\begin{tabularx}{\linewidth}{r >$r<$ @{} >{\raggedright${}}X<{$} p{#1}}
#2
\end{tabularx}%
}%
\newcommand{\rcc}[1]{% ''rightmost column contents''
#1 \shifter{#1}
}
\begin{document}
The following has the output I desire, and the source code is satisfactory:
\proofpiece\shapea{
1. & \multicolumn{2}{l}{Math}
& \rcc{Words}\\
2. & m & = 9/3 & \rcc{Words}\\
3. & & = 3 & \rcc{Words}\\
}
\proofpiece\shapea{
4. & 34x+2 & = 30/2 & \rcc{Wider words}\\
5. & & = 15 & \rcc{Words}\\
}
\vspace{2cm}
However, suppose we write a second proof in the same document:
\proofpiece\shapeb{
1. & \multicolumn{2}{l}{Math}
& \rcc{Words}\\
2. & m & = 2+2 & \rcc{Words}\\
3. & & = 4 & \rcc{Words}\\
}
\proofpiece\shapeb{
4. & 34x+2 & = 16/2 & \rcc{Words}\\
5. & & = 8 & \rcc{Words}\\
}
Unfortunately, the width of the rightmost column is equal to the width of the widest entry in the {\em previous} proof (namely, ``Wider words'').
\end{document}