![如何存储/附加代码以供日后使用](https://linux22.com/image/445923/%E5%A6%82%E4%BD%95%E5%AD%98%E5%82%A8%2F%E9%99%84%E5%8A%A0%E4%BB%A3%E7%A0%81%E4%BB%A5%E4%BE%9B%E6%97%A5%E5%90%8E%E4%BD%BF%E7%94%A8.png)
为了存储一些代码以供以后使用,我能想到两种方法:\appto
frometoolbox
和\sbox
。它们在使用中都存在一些问题。
对于\appto
,稍后调用宏将覆盖前者的值,如\ww
“测试 1”中所示。
对于\sbox
,代码中的所有宏都会立即展开。当宏的值必须稍后给出(如\parboxheightb
测试 4 中所示)时,这很不方便。
那么,如何解决上述问题?以及如何\appto
在嵌套宏中使用(请参见测试 5 中的示例)。另一个问题:为什么\\
测试 3 中的代码不起作用?
梅威瑟:
\documentclass[a4paper]{article}
\usepackage{xcolor,pgf,etoolbox}
\usepackage{geometry}
\geometry{showframe}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
\parindent0pt
\begin{document}
Test 1: Later invoke of \verb|\ww| covers the value of the former invoke.\par
\newcommand\testa{
\def\do{}
\def\ww{2pt}
\appto\do{\rule{1in}{\ww}\\}
\def\ww{10pt}
\appto\do{\rule{1in}{\ww}}
}
\testa\do
Test 2: In this way, the value of \verb|\parboxheighta| can be given after \verb|appto|\par
\newdimen\parboxheighta
\def\do{}
\newcommand\testc[1]{
\appto\do{\fbox{\parbox[t][\parboxheighta]{#1}{first}}}
}
\testc{1in}\testc{2in}
\setlength{\parboxheighta}{2in}
\do
Test 3: Why does \verb|\\| in code not work?\par
\newcommand\testb{
\sbox0{}
\def\ww{2pt}
\sbox0{\usebox0\rule{1in}{\ww}\\}
\def\ww{10pt}
\sbox0{\usebox0\rule{1in}{\ww}}
}
\testb\usebox0
\iffalse
Test 4: In this way, the value of \verb|\parboxheightb| is given after \verb|appto| which causes error.\par
\newdimen\paboxheightb
\sbox0{}
\newcommand\testd[1]{
\sbox0{\usebox0\parbox[t][\paboxheightb]{#1}{second}}
}
\setlength{\parboxheightb}{2in}
\testb{1in}\testb{2in}\usebox0
\fi
\iffalse
Test 5:How to use \verb|\appto| in nested macro?
\def\mycolor{red}
\def\mylength{1in}
\def\myexe{}
\appto\myexe{\colorbox}
\eappto\myexe{{\mycolor}}
\eappto\myexe{\{}
\appto\myexe{\parbox}
\eappto\myexe{{\mylength}}
\eappto\myexe{{xxx}}
\eappto\myexe{\}}
\myexe
\fi
\end{document}
答案1
平均能量损失
\documentclass[a4paper]{article}
\usepackage{xcolor,pgf,etoolbox}
\usepackage{geometry}
\geometry{showframe}
\geometry{left=1cm,right=1cm,top=1cm,bottom=1cm}
\parindent0pt
\begin{document}
Test 1: Later invoke of \verb|\ww| covers the value of the former invoke.\par
Use \verb|\eappto|:\par
\newcommand\testa{
\def\do{}
\def\ww{2pt}
\appto\do{\rule{1in}}
\eappto\do{{\ww}\\}
\def\ww{10pt}
\appto\do{\rule{1in}{\ww}}
}
\testa\do
%\show\ww
Test 2: In this way, the value of \verb|\parboxheighta| can be given after \verb|appto|\par
Do \verb|\def\do{}| inside \verb|\testc|:\par
\newdimen\parboxheighta
\newcommand\testc[1]{
\def\do{}
\appto\do{\fbox{\parbox[t][\parboxheighta]{#1}{xxx}}}
}
\setlength{\parboxheighta}{1in}
\testc{1in}\do
\setlength{\parboxheighta}{2in}
\testc{1in}\do
Test 3: Why does \verb|\\| in code not work?\par
\newcommand\testb{
\sbox0{}
\def\ww{2pt}
\sbox0{\rule{1in}{\ww}}
\def\ww{10pt}
\sbox0{\begin{minipage}{\linewidth}\usebox0\\\rule{1in}{\ww}\end{minipage}}
}
\testb\usebox0
\verb|\sbox| uses LR mode (no linebreaks): insert a minipage: see \texttt{texdoc latex2e}.
%\iffalse
%Test 4: In this way, the value of \verb|\parboxheightb| is given after \verb|appto| which causes error.\par
%\newdimen\paboxheightb
%\sbox0{}
%\newcommand\testd[1]{
% \sbox0{\usebox0\parbox[t][\paboxheightb]{#1}{second}}
%}
%\setlength{\parboxheightb}{2in}
%\testb{1in}\testb{2in}\usebox0
%\fi
\end{document}