如何使用辅助文件中的字符串来更新标题?

如何使用辅助文件中的字符串来更新标题?

一份文档(许多文档中的典型)有一个标题,其中以文本字符串的形式显示时间戳(“2013 年 12 月 25 日”)。时间戳字符串源自"update"加载的文件\input

我尝试构建一个最小的工作示例,但是该示例(三个文件)并没有像我预期的那样工作。

% this is a listing of example.cls
\ProvidesPackage{example}
\LoadClassWithOptions{book}
\newcommand{\timestamp}[1]{#1}
\newcommand{\revision}{ \timestamp}
% this is a listing of example.tex
\documentclass[letterpaper]{example}
\begin{document}
This line represents the document header: \revision
\input{update} % update data from external file
Now the header should reflect the update: \revision
\end{document}
% this is a listing of "update"
% the date string should update the document header:
\timestamp{25 December 2013}
This line represents update text, which typically consists of dozens of lines.

经过大量的搜索和阅读,我仍然不明白哪种 LaTeX 机制(例如\savebox\renewcommand)适合这项任务。另外,我对\timestamp和定义的正确位置感到困惑\revision——它们应该在类文件(example.cls)中定义,在主文档文件(example.tex)中定义,还是在定期更新主文档文件的文件(update)中定义?我倾向于在类文件中定义尽可能多的内容,因为有超过一千个文档文件。

答案1

我认为这满足了您的要求。您不需要新.cls文件。

\documentclass{book}

\newcommand{\revision}{dummy value}

\newcommand{\timestamp}[1]{%
\renewcommand{\revision}{#1}
}

\begin{document}
This line represents the document header: \revision

\input{update} % update data from external file, type .tex

Now the header should reflect the update: \revision

\end{document}

在此处输入图片描述

如果在输入文本之前的文档标题中出现的修订行需要新的时间戳值,那么您将需要更加努力 - 请参阅如何将文本复制到文档的另一部分?

相关内容