ムウェ

ムウェ

という名前を持つ文書がある場合、単語を追加することでmy exam.tex出力PDFファイルに という名前を付けることは可能ですか?my exam [solved].pdf[solved] のみクラス オプションが有効になっている場合はどうなりますanswersか?

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

答え1

これがこの問題に対する私の解決策です。

を使用せずにコンパイルすると-shell-escape、通常どおり実行されます (つまり、answersオプションを使用している場合は回答が出力されますが、そうでない場合は出力されず、デフォルト\jobnameが使用されます)。

でコンパイルすると、質問と回答の-shell-escape2 つのファイルが出力されます。これは、オプションがクラスに渡されるかどうかに関係なく行われます。\jobname.pdf\jobname-solved.pdfanswers

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

出力


一度に 1 つのファイルのみを出力したい場合は、上記の回答を少し変更して、オプションが実際にクラスに渡された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

2つのファイルを出力する

\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}

1つのファイルを出力する

\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の代わりにを直接呼び出して 2 つのファイルを出力します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}

関連情報