我正在尝试用 LaTeX 创建一个模板文档,其中包含我将在编译期间定义的文本占位符。例如:
\documentclass{article}
\begin{document}
\section{Introduction}
This is a placeholder document which contains [p1], [p2] and [p3].
\end{document}
这样我以后就可以编译latex mydoc.tex p1=First p2=Second p3=Third
并得到输出This is a placeholder document which contains First, Second and Third.
我想知道如何才能实现这一点(或一些类似的行为)?
答案1
你可以使用Heiko 的诡计。假设你的文件名为test.tex
。然后编译
\documentclass{article}
\begin{document}
\section{Introduction}
This is a placeholder document which contains \PlaceHolder1, \PlaceHolder2 and \PlaceHolder3.
\end{document}
和
pdflatex '\def\PlaceHolder#1{\ifcase#1\or blub\or bla\or pft\fi}\input{test}'
产量
或者,更花哨的版本:假设placeholders.txt
包含
p1=First,p2=Second,p3=Third
和 TeX 文件
\documentclass{article}
\usepackage{catchfile}
\usepackage{pgf}
\newcommand{\PlaceHolder}[1]{\pgfkeysvalueof{/placeholder/p#1}}
\CatchFileEdef{\PlaceHolderKeys}{placeholders.txt}{}
\edef\tmp{\noexpand\pgfkeys{/placeholder/.cd,p1/.initial={},p2/.initial={},p3/.initial={},%
\PlaceHolderKeys}}\tmp
\begin{document}
\section{Introduction}
This is a placeholder document which contains \PlaceHolder{1},
\PlaceHolder{2} and \PlaceHolder{3}.
\end{document}
产量