平均能量损失

平均能量损失

如果我有一个名为的文档,是否可以通过在文档中附加单词my exam.tex来使其输出的 PDF 文件具有名称my exam [solved].pdf[solved] 仅有的answers启用类别选项时?

\documentclass[answers]{exam}
\begin{document}
    text
\end{document}

答案1

以下是我对这个问题的解决方案。

如果您在编译时不使用-shell-escape,它只会正常运行(即,如果您使用该answers选项,它将输出答案,否则不会;\jobname使用默认值。)

如果使用 进行编译-shell-escape,则它会输出两个文件:\jobname.pdf问题文件和\jobname-solved.pdf答案文件。无论answers选项是否传递给类,它都会这样做。

我已经使用过,latexmk这样就可以自动考虑所需的多个编译。

平均能量损失

\documentclass{exam}

\usepackage{pdftexcmds}
\usepackage{iftex}

\makeatletter

\ltx@IfUndefined{pdf@shellescape}
  {}
  {\ifnum\pdf@shellescape=1
     \ifpdftex
       \def\latexmkengine{-pdf}%
     \fi
     \ifluatex
       \def\latexmkengine{-lualatex}%
     \fi
     \ifxetex
       \def\latexmkengine{-xelatex}%
     \fi
     \pdf@system{%
       latexmk \latexmkengine\space -jobname="\jobname-solved"
         -usepretex="\string\AtBeginDocument{\string\printanswerstrue}"
         "\jobname"
     }%
     \pdf@system{%
       latexmk \latexmkengine\space
         -usepretex="\string\AtBeginDocument{\string\printanswersfalse}"
         "\jobname"
     }%
     \expandafter\stop
   \fi}

\makeatother

\begin{document}
\begin{questions}
  \question One of these things is not like the others; one of these things is
  not the same. Which one is different?
  \begin{oneparchoices}
    \choice John
    \choice Paul
    \choice George
    \choice Ringo
    \CorrectChoice Socrates
  \end{oneparchoices}
\end{questions}
\end{document}

输出

\jobname.pdf

输出

\jobname-solved.pdf

输出


如果您希望一次只输出一个文件,您可以稍微修改上面的答案,以便仅在选项实际传递给类latexmk时才调用。这回答了您提出的问题。它仍然需要用 进行编译。answersexam-shell-escape

\documentclass[answers]{exam}

\usepackage{pdftexcmds}
\usepackage{iftex}

\makeatletter

\ltx@IfUndefined{pdf@shellescape}
  {}
  {\ifnum\pdf@shellescape=1
     \ifpdftex
       \def\latexmkengine{-pdf}%
     \fi
     \ifluatex
       \def\latexmkengine{-lualatex}%
     \fi
     \ifxetex
       \def\latexmkengine{-xelatex}%
     \fi
     \ifprintanswers
       \pdf@system{%
         latexmk \latexmkengine\space -jobname="\jobname-solved" "\jobname"
       }%
       \expandafter\expandafter\expandafter\stop
     \fi
   \fi}

\makeatother

\begin{document}
\begin{questions}
  \question One of these things is not like the others; one of these things is
  not the same. Which one is different?
  \begin{oneparchoices}
    \choice John
    \choice Paul
    \choice George
    \choice Ringo
    \CorrectChoice Socrates
  \end{oneparchoices}
\end{questions}
\end{document}

还有一个expl3处理带空格的文件名的解决方案。-shell-escape当然还是需要的。

输出两个文件

\documentclass{exam}

\usepackage{expl3}

\ExplSyntaxOn

\str_new:N \l__diaa_latexmk_engine_str
\str_new:N \g__diaa_solved_jobname_str
\str_const:Nn \l__diaa_latexmk { latexmk }

\cs_new:Nn \__diaa_build_solved_jobname:
  {
    \str_gset:Nx \g__diaa_solved_jobname_str { \c_sys_jobname_str }
    \str_gremove_all:Nn \g__diaa_solved_jobname_str { " }
    \str_gput_left:Nn \g__diaa_solved_jobname_str { " }
    \str_gput_right:Nn \g__diaa_solved_jobname_str { ~[solved]" }
  }

\sys_if_shell_unrestricted:T
  {
    \sys_if_engine_luatex:T
      { \str_set:Nn \l__diaa_latexmk_engine_str { -lualatex } }
    \sys_if_engine_pdftex:T
      { \str_set:Nn \l__diaa_latexmk_engine_str { -pdf } }
    \sys_if_engine_xetex:T
      { \str_set:Nn \l__diaa_latexmk_engine_str { -xelatex } }
    \__diaa_build_solved_jobname:
    \sys_shell_now:x
      {
        \l__diaa_latexmk \c_space_tl
        \l__diaa_latexmk_engine_str \c_space_tl
        -usepretex="\string\AtBeginDocument{\string\printanswerstrue}" \c_space_tl
        -jobname=\g__diaa_solved_jobname_str \c_space_tl
        \c_sys_jobname_str
      }
    \sys_shell_now:x
      {
        \l__diaa_latexmk \c_space_tl
        \l__diaa_latexmk_engine_str \c_space_tl
        -usepretex="\string\AtBeginDocument{\string\printanswersfalse}" \c_space_tl
        \c_sys_jobname_str
      }
    \stop
  }

\ExplSyntaxOff

\begin{document}
\begin{questions}
  \question One of these things is not like the others; one of these things is
  not the same. Which one is different?
  \begin{oneparchoices}
    \choice John
    \choice Paul
    \choice George
    \choice Ringo
    \CorrectChoice Socrates
  \end{oneparchoices}
\end{questions}
\end{document}

输出一个文件

\documentclass[answers]{exam}

\usepackage{expl3}

\ExplSyntaxOn

\str_new:N \l__diaa_latexmk_engine_str
\str_new:N \g__diaa_solved_jobname_str
\str_const:Nn \l__diaa_latexmk { latexmk }

\cs_new:Nn \__diaa_build_solved_jobname:
  {
    \str_gset:Nx \g__diaa_solved_jobname_str { \c_sys_jobname_str }
    \str_gremove_all:Nn \g__diaa_solved_jobname_str { " }
    \str_gput_left:Nn \g__diaa_solved_jobname_str { " }
    \str_gput_right:Nn \g__diaa_solved_jobname_str { ~[solved]" }
  }

\sys_if_shell_unrestricted:T
  {
    \sys_if_engine_luatex:T
      { \str_set:Nn \l__diaa_latexmk_engine_str { -lualatex } }
    \sys_if_engine_pdftex:T
      { \str_set:Nn \l__diaa_latexmk_engine_str { -pdf } }
    \sys_if_engine_xetex:T
      { \str_set:Nn \l__diaa_latexmk_engine_str { -xelatex } }
    \legacy_if:nT { printanswers }
      {
        \__diaa_build_solved_jobname:
        \sys_shell_now:x
          {
            \l__diaa_latexmk \c_space_tl
            \l__diaa_latexmk_engine_str \c_space_tl
            -jobname=\g__diaa_solved_jobname_str \c_space_tl
            \c_sys_jobname_str
          }
        \stop
      }
  }

\ExplSyntaxOff

\begin{document}
\begin{questions}
  \question One of these things is not like the others; one of these things is
  not the same. Which one is different?
  \begin{oneparchoices}
    \choice John
    \choice Paul
    \choice George
    \choice Ringo
    \CorrectChoice Socrates
  \end{oneparchoices}
\end{questions}
\end{document}

直接调用lualatex而不是 来输出两个文件latexmk

\documentclass{exam}

\usepackage{expl3}

\ExplSyntaxOn

\str_new:N \g__diaa_solved_jobname_str
\str_const:Nn \l__diaa_latex_cmd { lualatex }
\str_const:Nn \l__diaa_latex_options { -synctex=1 ~ -interaction=nonstopmode }

\cs_new:Nn \__diaa_build_solved_jobname:
  {
    \str_gset:Nx \g__diaa_solved_jobname_str { \c_sys_jobname_str }
    \str_gremove_all:Nn \g__diaa_solved_jobname_str { " }
    \str_gput_left:Nn \g__diaa_solved_jobname_str { " }
    \str_gput_right:Nn \g__diaa_solved_jobname_str { ~[solved]" }
  }

\sys_if_shell_unrestricted:T
  {
    \__diaa_build_solved_jobname:
    \sys_shell_now:x
      {
        \l__diaa_latex_cmd \c_space_tl
        -jobname=\g__diaa_solved_jobname_str \c_space_tl
        \l__diaa_latex_options \c_space_tl
        "\string\AtBeginDocument{\string\printanswerstrue}" \c_space_tl
        "\string\input{\c_sys_jobname_str}"
      }
    \sys_shell_now:x
      {
        \l__diaa_latex_cmd \c_space_tl
        -jobname=\c_sys_jobname_str \c_space_tl
        \l__diaa_latex_options \c_space_tl
        "\string\AtBeginDocument{\string\printanswersfalse}" \c_space_tl
        "\string\input{\c_sys_jobname_str}"
      }
    \stop
  }

\ExplSyntaxOff

\begin{document}
\begin{questions}
  \question One of these things is not like the others; one of these things is
  not the same. Which one is different?
  \begin{oneparchoices}
    \choice John
    \choice Paul
    \choice George
    \choice Ringo
    \CorrectChoice Socrates
  \end{oneparchoices}
\end{questions}
\end{document}

相关内容