write18使用一例:基于同一模板一次分开编译多个章节
试想如下使用场景, 我们需要对一门课程做备课讲义。假设这门课程分为8章。一个简单的办法是首先写一个公共的模板, 然后将每章独立的插入到该公共模板。但是这样一来得到的是所有章节合到一起的一个PDF.
我的问题是, 能不能够基于公共模板编译出来独立章节的各个PDF。
下面的代码利用write18可以实现上述功能。
%main.tex
\documentclass{article}
\gdef\chaplist{sec1,sec2}
\makeatletter
\@for\fname:=\chaplist\do{
\immediate\write18{%
pdflatex -jobname="\jobname-\fname"
\gdef\string\notinmain{1}
\noexpand\AtBeginDocument{\noexpand\input{\fname}}
\string\input\space\jobname%
}
}
\makeatother
\title{Compile each section based on the master file}
\author{Van Abel}
\date{\today}
\begin{document}
\ifx\notinmain\undefined
\maketitle
\tableofcontents
The value of \texttt{\string\notinmain} in \texttt{\jobname} is: \texttt{\meaning\notinmain}
\makeatletter
\@for\fname:=\chaplist\do{
\include{\fname}
}
\makeatother
\fi
\end{document}
%sec1.tex
\section{The titile of section 1}
The value of \texttt{\string\notinmain} in \texttt{\jobname} is:\texttt{\meaning\notinmain}
%sec2.tex
\ifdefined\notinmain
\ifnum\notinmain=1
\addtocounter{section}{1}
\fi
\fi
\section{The titile of section 2}
The value of \texttt{\string\notinmain} in \texttt{\jobname} is: \texttt{\meaning\notinmain}
然后利用xelatex --shell-escape main.tex 即可生成main.pdf, main-sec1.pdf, main-sec2.pdf.
本作品采用创作共用版权协议, 要求署名、非商业用途和保持一致. 转载本站内容必须也遵循署名-非商业用途-保持一致的创作共用协议.

发表回复