分类
LaTeX

write18使用一例:基于同一模板一次分开编译多个章节

试想如下使用场景, 我们需要对一门课程做备课讲义。假设这门课程分为8章。一个简单的办法是首先写一个公共的模板, 然后将每章独立的插入到该公共模板。但是这样一来得到的是所有章节合到一起的一个PDF.

我的问题是, 能不能够基于公共模板编译出来独立章节的各个PDF。

下面的代码利用write18可以实现上述功能。

%main.tex


\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
Compile each section based on the master file
Van Abel
07/18/2018


\ifx\notinmain\undefined
  
  目录
Contents
 1.  The titile of section 1
 2.  The titile of section 2
The value of \texttt{\string\notinmain} in \texttt{\jobname} is: \texttt{\meaning\notinmain} \makeatletter \@for\fname:=\chaplist\do{ \include{\fname} } \makeatother \fi

%sec1.tex
1. 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
2. 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.

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据