Compile two versions of a document from the same latex source

后端 未结 2 683
鱼传尺愫
鱼传尺愫 2020-12-21 09:45

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?

2条回答
  •  孤城傲影
    2020-12-21 10:14

    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}
    

提交回复
热议问题