条件编译失败

条件编译失败

我想你只要读一下下面的代码就能理解其场景。

% compile with pdflatex -shell-escape

\documentclass{article}
\usepackage{filecontents}

\begin{filecontents*}{filename.tex}
\documentclass{article}
\newif\ifsecret
\begin{document}
common 1

\ifsecret
I have a top secret message here.
\fi

common 2
\end{document}
\end{filecontents*}


\begin{document}
\immediate\write18{pdflatex -jobname=secret-incuded "\AtBeginDocument{\secrettrue} \input{filename}"}
\immediate\write18{pdflatex -jobname=secret-excluded "\AtBeginDocument{\secretfalse} \input{filename}"}
Done, secret-included.pdf and secret-excluded.pdf have been generated!
\end{document}

我的上面的代码有什么错误?

答案1

\write扩展其内容。您不想扩展\AtBeginDocument,对吗?

\begin{filecontents*}{mozartsecretfile.tex}
\documentclass{article}
\newif\ifsecret
\begin{document}
common 1

\ifsecret
I have a top secret message here.
\fi

common 2
\end{document}
\end{filecontents*}

\documentclass{article}

\begin{document}

\immediate\write18{pdflatex -jobname=mozartsecretincluded
  \unexpanded{"\AtBeginDocument{\secrettrue} \input{mozartsecretfile}"}}
\immediate\write18{pdflatex -jobname=mozartsecretexcluded
  \unexpanded{"\AtBeginDocument{\secretfalse} \input{mozartsecretfile}"}}
Done, secret-included.pdf and secret-excluded.pdf have been generated!
\end{document}

相关内容