一次性构建考试和解决方案?

一次性构建考试和解决方案?

我想从一份考试文件中生成两个 PDF:

我目前正在做以下事情

  1. 添加answers到文档类

    \documentclass[french,a4paper,addpoints,11pt,answers]{exam}
    
  2. 构建并重命名

    latexmk -xelatex foo.tex && mv foo.pdf foo-solution.pdf
    
  3. answers从文档类中删除并重建

    latexmk -xelatex foo.tex
    

有没有更好的方法可以通过命令行完成所有操作?

答案1

将第一行更改为

\documentclass[french,a4paper,addpoints,11pt,\ANSWERS]{exam}

并使用

latex -jobname foo-without-answers '\def\ANSWERS{}\input{foo}'

latex -jobname foo-with-answers '\def\ANSWERS{answers}\input{foo}'

分别。

或者可能更灵活,定义一个可以在整个文档中使用的开关(在评论中采纳 Ulrich Diez 的建议)。

\ifANSWERS\PassOptionsToClass{answers}{exam}\fi
\documentclass[french,a4paper,addpoints,11pt]{exam}

编译使用:

latex -jobname foo-without-answers '\newif\ifANSWERS\input{foo}'

latex -jobname foo-with-answers '\newif\ifANSWERS\ANSWERStrue\input{foo}'

答案2

如果不需要将该选项answers作为全局选项在加载任何包时应用,而只在加载 documentclass 测试时应用,那么您可以创建一个 .tex 文件,我们称之为测试.tex,其中answers未指定 -option,但会创建辅助文件测试解决方案.tex内容如下:

\PassOptionsToClass{answers}{exam}%
\input test.tex

然后你可以先编译/构建测试.tex进而测试解决方案.tex

就像是:

%==========This snippet creates \jobname-solutions.tex only
%          in case both it does not already exist and
%          \jobname does not contain the phrase "-solutions".
%
%          This snippet could go into an input-file on its own
%          which is to be loaded via \input right before the
%          \documentclass-command.===============================================
\begingroup\makeatletter
\newcommand\GobbleToNamePostfix[1]{%
   \long\def\GobbleToNamePostfix##1#1{}%
   \expandafter\endgroup
   \ifcat$\detokenize\expandafter\expandafter\expandafter{%
                     \expandafter\GobbleToNamePostfix\jobname#1}$%
   \else\expandafter\@gobble\fi
   {%
     % \jobname does not contain the phrase #1:
     \IfFileExists{\jobname#1.tex}{}{%
       \begingroup
       \immediate\openout\@auxout\jobname#1.tex
       \immediate\write\@auxout{%
          \string\PassOptionsToClass{answers}{exam}\@percentchar
       }%
       \immediate\write\@auxout{\string\input\space\jobname.tex}%
       \immediate\closeout\@auxout
       \endgroup
     }%
     %%
     %% If \write18 is enabled, e.g., by calling the compiler
     %% with the --shell-escape-option, like
     %%     latexmk -shell-escape -xelatex test.tex
     %% , then here you can do a call to the shell for
     %% building \jobname#1.tex/test-solutions.tex:
     %%
     %\RequirePackage{shellesc, iftex}%
     %\ShellEscape{%
     %  latexmk
     %  -\ifluatex lua\fi\ifpdftex pdf\fi\ifxetex xe\fi latex \jobname#1.tex%
     %}%
   }%
}%
\begingroup
%-------------------------------------------------------------------------------
% Here goes the phrase to append to name of .tex-file also delivering solutions:
\renewcommand\GobbleToNamePostfix{-solutions}%
%-------------------------------------------------------------------------------
\@onelevel@sanitize\GobbleToNamePostfix
\expandafter\endgroup\expandafter
\GobbleToNamePostfix\expandafter{\GobbleToNamePostfix}%
%==================== end of snippet ===========================================
%
%\show\GobbleToNamePostfix %yields: > \GobbleToNamePostfix=undefined.
%
\documentclass[french,a4paper,addpoints,11pt]{exam}
\usepackage[T1]{fontenc}
\usepackage{babel}
\makeatletter
\@ifclasswith{exam}{answers}{\def\Answertext{With Answers.}}%
                            {\def\Answertext{Without Answers.}}%
\makeatother
\begin{document}
\Answertext
\begin{questions}
\question[10]Why is there air?
\begin{solution}
Because nobody breathed it away yet.
\end{solution}
\end{questions}
\end{document}

将其另存为测试.tex并对其进行编译,你将获得测试.pdf

在此处输入图片描述

测试解决方案.tex

\PassOptionsToClass{answers}{exam}%
\input test.tex

编译时测试解决方案.tex,然后你得到测试解决方案.pdf

在此处输入图片描述

或者取消注释/激活以下行

%\RequirePackage{shellesc, iftex}%
%\ShellEscape{%
%  latexmk
%  -\ifluatex lua\fi\ifpdftex pdf\fi\ifxetex xe\fi latex \jobname#1.tex%
%}%

成为

\RequirePackage{shellesc, iftex}%
\ShellEscape{%
  latexmk
  -\ifluatex lua\fi\ifpdftex pdf\fi\ifxetex xe\fi latex \jobname#1.tex%
}%

然后编译测试.tex通过
latexmk -shell-escape -xelatex test.tex

这边走测试.pdf测试解决方案.pdf应一次性创建。

答案3

如果您可以接受制作一个 PDF 并将不同版本放在不同的页面上,那么有一个简单的解决方案。

创建一个命令,仅在布尔开关为真时显示答案。然后将整个文档保存在调用该命令的宏中。现在您可以打印文档两次,一次包含答案,一次不包含答案,只需调用文档宏两次并在两次之间重置布尔开关即可。

\documentclass{article}
\newif\ifanswers
\NewDocumentCommand{\QA}{ m m }{#1 \ifanswers(#2)\fi}
\NewDocumentCommand{\doc}{}{%
  \begin{enumerate}
    \item \QA{Who?}{Him.}
    \item \QA{When?}{Then.}
  \end{enumerate}
}
\begin{document}
\answersfalse\doc{}
\clearpage
\answerstrue\doc{}
\end{document}

相关内容