Compile two versions of a document from the same latex source

ぐ巨炮叔叔 提交于 2020-01-21 19:39:28

问题


How to automatically compile two versions of the same document, for example version without answers for the students and another version with answers for myself?


回答1:


There are several packages that allow to conditionally exclude certain parts of the document, for example the exercise package.

With TeXstudio, the following magic comment can be used to automatically compile both versions at once (including repeated compilation for cross-references, bibliographies, indices etc.):

% !TeX program = latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=% -pretex="\newcommand{\version}{noanswer}" -usepretex % | latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=%_solution -pretex="\newcommand{\version}{}" -usepretex % | txs:///view-pdf "?am)_solution.pdf"

\documentclass{article}

% setting a default value in case it is compiled without the magic comment
\ifdefined\version
\else
\def\version{noanswer}
\fi

\usepackage[\version]{exercise}

\begin{document}

    \begin{Exercise}[title={Title},label=ex1]

        question text

    \end{Exercise}

    \begin{Answer}[ref={ex1}]

        solution

    \end{Answer}

\end{document}



回答2:


I have a small bash script to do a dual format.

function latex-ans () {
    n=$(basename $1 .tex)  # strip .tex in filename foo.tex -> foo
    ln -s $n.tex $n-ans.tex # create a soft link (for instance foo-ans.tex -> foo.tex)
    pdflatex '\def\withanswer{1} \input{'$n-ans'}' && pdflatex  $n
       % first format the version with answers and then the normal version
    rm $n-ans.tex $n-ans.log
       % remove useless files
}

If I have a file foo.tex, this commands formats both versions of the file and generates two pdf: foo.pdf and foo-ans.pdf. Thanks to the renaming of foo.tex through the ln -s, it also keeps separate foo.aux and foo-ans.aux to preserve useful information on both versions.

At the latex level, I basically do the same and use the macro \withanswers to configure my packages.



来源:https://stackoverflow.com/questions/57111009/compile-two-versions-of-a-document-from-the-same-latex-source

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!