存储快速变化的命令的快照

存储快速变化的命令的快照

我的文档使用一个命令,该命令每章更新一次。对于某些章节,我需要将其扩展存储为另一个变量(命令)。我需要实际的扩展值,而不是命令。

为了说明这一点,我们使用了 MWE。

\documentclass{book}
\usepackage{etoolbox,ifthen,xspace}

\newcommand{\subjectshort}{Compsci} % this command is used several times in the document
\newcommand{\subjectshortl}{\MakeLowercase{\subjectshort}} % this command is an workaround just for the next command
\newcommand{\abracadabra}[1]{abracadabra:#1}
\newcommand{\chaplabel}[1]{
    \ifthenelse{\isundefined{\thischaplabeltmp}}{
        \newcommand{\thischaplabeltmp}{\abracadabra{#1}}
    } {
    \renewcommand{\thischaplabeltmp}{\abracadabra{#1}}
}
\ifthenelse{\isundefined{\thischaplabel}}{
    \newcommand{\thischaplabel}{\subjectshortl:mod:\thischaplabeltmp\xspace}
} {
\renewcommand{\thischaplabel}{\subjectshortl:mod:\thischaplabeltmp\xspace}
}
\thischaplabel
}

\begin{document}
\chapter{First chapter}
some content of first chapter\\
\chaplabel{Text specific to first chapter}\\
some more contents of first chapter\\
current thischaplabel = \thischaplabel\\
\expandafter\def\csname tmpstore\endcsname{\thischaplabel\xspace}
current tmpstore = \tmpstore\\ % this is the value I want to be avaibale till I redefine \tmpstore 

\chapter{Second chapter}
some content of second chapter\\
\chaplabel{Text specific to second chapter}\\
some more contents of second chapter\\
current thischaplabel = \thischaplabel\\
current tmpstore = \tmpstore\\ % i wanted the earlier expanded value avaibale here too
% writing to an auxiliary file does not produce the expected outcome as shown by the next line
%\expandafter\label{\tmpstore} % this breaks compliing

\chapter{Third chapter}
some content of third chapter\\
\chaplabel{Text specific to third chapter}\\
some more contents of third chapter\\

\chapter{Fourth chapter}
some content of fourth chapter\\
\chaplabel{Text specific to fourth chapter}\\
some more contents of fourth chapter\\
\end{document}

当我第一次在章节中调用时\chaplabel{Text specific to the chapter},它会创建或更新命令\thischaplabel,然后输出扩展。到目前为止一切顺利。但是在某些章节中,我需要存储当前的扩展值\thischaplabel以便更长时间可用,也许在接下来的 4 个章节中。我尝试在相关章节中调用以下内容

\expandafter\def\csname tmpstore\endcsname{\thischaplabel\xspace}

但是它似乎从不存储扩展值。相反, \tmpstore 获取最新的值\thischaplabel。实际上它只是 的一个别名\thischaplabel

我如何使用\tmpstore像保存常量字符串(其本身是另一个命令的扩展形式)的临时变量?

原因:我还需要将 /tmpstore 值写入辅助文件(稍后处理)。我在此辅助文件中看到的输出是立即展开的命令,而不是最终展开的(字符串)值。这可以通过在\expandafter\label{\tmpstore}任何地方发出指令来看到。除此之外,它也无法正确编译。

答案1

使用\protected@edef而不是\def

需要受保护的\edef是因为保存的数量\tmpstore,即\thischapterlabel包含形式为\MakeLowercase和的不可扩展宏\xspace

\documentclass{book}
\usepackage{etoolbox,ifthen,xspace}

\newcommand{\subjectshort}{Compsci} % this command is used several times in the document
\newcommand{\subjectshortl}{\MakeLowercase{\subjectshort}} % this command is an workaround just for the next command
\newcommand{\abracadabra}[1]{abracadabra:#1}
\newcommand{\chaplabel}[1]{
    \ifthenelse{\isundefined{\thischaplabeltmp}}{
        \newcommand{\thischaplabeltmp}{\abracadabra{#1}}
    } {
    \renewcommand{\thischaplabeltmp}{\abracadabra{#1}}
}
\ifthenelse{\isundefined{\thischaplabel}}{
    \newcommand{\thischaplabel}{\subjectshortl:mod:\thischaplabeltmp\xspace}
} {
\renewcommand{\thischaplabel}{\subjectshortl:mod:\thischaplabeltmp\xspace}
}
\thischaplabel
}

\begin{document}
\chapter{First chapter}
some content of first chapter\\
\chaplabel{Text specific to first chapter}\\
some more contents of first chapter\\
current thischaplabel = \thischaplabel\\
\makeatletter%
\expandafter\protected@edef\csname tmpstore\endcsname{\thischaplabel\xspace}
\makeatother%
current tmpstore = \tmpstore\\ % this is the value I want to be avaibale till I redefine \tmpstore 

\chapter{Second chapter}
some content of second chapter\\
\chaplabel{Text specific to second chapter}\\
some more contents of second chapter\\
current thischaplabel = \thischaplabel\\
current tmpstore = \tmpstore\\ % i wanted the earlier expanded value available here too

\chapter{Third chapter}
some content of third chapter\\
\chaplabel{Text specific to third chapter}\\
some more contents of third chapter\\

\chapter{Fourth chapter}
some content of fourth chapter\\
\chaplabel{Text specific to fourth chapter}\\
some more contents of fourth chapter\\
\end{document}

在此处输入图片描述


楼主编辑了这个问题,并指出他/她想用它\tmpstore作为标签。正如我所解释的,标签中不能使用不可膨胀的材料。不过,我在这里做了两件事。我\xspace

\def\xspace{}% GETS RID OF ONE UNEXPANDABILITY (DISABLES \xspace}

(显然,正确的方法是不使用它,而不是禁用它;我在这里这样做只是为了演示),然后我想出一个更可扩展的形式,\MakeLowercase形式为

\usepackage{expl3}
\ExplSyntaxOn
\cs_set_eq:NN \explower \tl_lower_case:n% GETS RID OF SECOND UNEXPANDABILITY
\ExplSyntaxOff

我使用

\newcommand{\subjectshortl}{\explower{\subjectshort}} % this command is an workaround just for the next command

通过这些更改,我可以让它与简单的一起工作\edef,然后我可以显示它其中没有未扩展的材料(使用\detokenize)并将其用作参数\label

底线是\xspace\MakeLowercase不可扩展,因此不是论点中可接受的组成部分\label

\documentclass{book}
\usepackage{etoolbox,ifthen,xspace}

\newcommand{\subjectshort}{Compsci} % this command is used several times in the document
\newcommand{\subjectshortl}{\explower{\subjectshort}} % this command is an workaround just for the next command
\newcommand{\abracadabra}[1]{abracadabra:#1}
\newcommand{\chaplabel}[1]{
    \ifthenelse{\isundefined{\thischaplabeltmp}}{
        \newcommand{\thischaplabeltmp}{\abracadabra{#1}}
    } {
    \renewcommand{\thischaplabeltmp}{\abracadabra{#1}}
}
\ifthenelse{\isundefined{\thischaplabel}}{
    \newcommand{\thischaplabel}{\subjectshortl:mod:\thischaplabeltmp\xspace}
} {
\renewcommand{\thischaplabel}{\subjectshortl:mod:\thischaplabeltmp\xspace}
}
\thischaplabel
}

\def\xspace{}% GETS RID OF ONE UNEXPANDABILITY (DISABLES \xspace}

\usepackage{expl3}
\ExplSyntaxOn
\cs_set_eq:NN \explower \tl_lower_case:n% GETS RID OF SECOND UNEXPANDABILITY
\ExplSyntaxOff

\begin{document}
\chapter{First chapter}
some content of first chapter\\
\chaplabel{Text specific to first chapter}\\
some more contents of first chapter\\
current thischaplabel = \thischaplabel\\
\makeatletter%
\expandafter\edef\csname tmpstore\endcsname{\thischaplabel\xspace}
\makeatother%
current tmpstore = \tmpstore\\ % this is the value I want to be avaibale till I redefine \tmpstore 

\chapter{Second chapter}
some content of second chapter\\
\chaplabel{Text specific to second chapter}\\
some more contents of second chapter\\
current thischaplabel = \thischaplabel\\
current tmpstore = \tmpstore\\ % i wanted the earlier expanded value avaibale here too

Tokens of \textbackslash tmpstore:\\
\detokenize\expandafter{\tmpstore} % this breaks compliing

Using it as a label:
\expandafter\label\expandafter{\tmpstore} % this breaks compliing

\chapter{Third chapter}
some content of third chapter\\
\chaplabel{Text specific to third chapter}\\
some more contents of third chapter\\

\chapter{Fourth chapter}
some content of fourth chapter\\
\chaplabel{Text specific to fourth chapter}\\
some more contents of fourth chapter\\
\end{document}

在此处输入图片描述

相关内容