后续行动这个答案对于我之前的问题,我有以下问题需要解决:
1-如果answers
禁用该选项,我需要设置twofilestrue
仅生成考试而不生成解决方案。
2- 使用lualatex
(即\latexmkiffalse
)并answers
启用该选项时,我无法生成任何 pdf 文件。它进入无限编译循环。
\documentclass{exam}
\usepackage{expl3}
\newif\iftwofiles
\twofilesfalse% false to print one file , true for both at a time
\newif\iflatexmkif
\latexmkiffalse% true to use latexmk , false to use lualatex
\ExplSyntaxOn
\str_new:N \g__diaa_solved_jobname_str
\iflatexmkif
\str_new:N \l__diaa_latexmk_engine_str
\str_const:Nn \l__diaa_latexmk { latexmk }
\else
\str_const:Nn \l__diaa_latex_cmd { lualatex }
\str_const:Nn \l__diaa_latex_options { -synctex=1 ~ -interaction=nonstopmode ~ -shell-escape }
\fi
\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 { "[solved]~ }
\str_gput_right:Nn \g__diaa_solved_jobname_str { " }
}
\sys_if_shell_unrestricted:T
{
\iflatexmkif
\sys_if_engine_luatex:T
{ \str_set:Nn \l__diaa_latexmk_engine_str { -lualatex ~ -g ~ -interaction=nonstopmode } }
\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 } }
\iftwofiles
\__diaa_build_solved_jobname:
\sys_shell_now:x
{% printing the solution
\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
{% printing the exam
\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
\else % one file
\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
}
\fi
\else % lualatex
\iftwofiles
\__diaa_build_solved_jobname:
\sys_shell_now:x
{% printing the solution
\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
{% printing the exam
\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
\else % one file
\legacy_if:nT { printanswers } % if answers is enabled
{
\__diaa_build_solved_jobname:
\sys_shell_now:x
{
\l__diaa_latex_cmd \c_space_tl
\l__diaa_latex_options \c_space_tl
-jobname=\g__diaa_solved_jobname_str \c_space_tl
\c_sys_jobname_str
}
\stop
}
\fi
\fi
}
\ExplSyntaxOff
\begin{document}
\begin{questions}
\question some question
\begin{solution}
the solution
\end{solution}
\end{questions}
\end{document}
答案1
由于在构建过程中-shell-escape
传递了 ,因此出现了无限循环。因此测试通过,您再次运行。lualatex
\sys_shell_now:x
\sys_if_shell_unrestricted:T
lualatex
如果我理解正确,以下 MWE 将会满足您的要求。
如果未指定 classanswers
选项,则无论是否使用 调用初始构建,都只会生成问题-shell-escape
。您可以通过取消注释 来覆盖此设置\bool_gset_true:N \g__diaa_build_two_files_bool
,在这种情况下,将始终生成问题和答案。
当使用 进行编译时-shell-escape
,构建将使用您启动编译的任何引擎(pdflatex
,lualatex
,xelatex
)。
您可以latexmk
通过取消注释来使用它进行构建\bool_gset_true:N \g__diaa_use_latexmk_bool
(假设您也使用该标志启动构建-shell-escape
)。
更新
你现在可以:
- 使用 控制问题的输出
\g__diaa_build_questions_bool
。 - 使用 控制答案的输出
\g__diaa_build_solved_bool
。 \g__diaa_latex_options_str
分别使用和指定 LaTeX 和 Latexmk 的选项\g__diaa_latexmk_options_str
。- 控制是否使用 Latexmk
\g__diaa_use_latexmk_bool
。
为了确保 synctex 数据正确写入,您将需要选项-synctex=1 -output-directory=out
。这可确保文件不会被主编译过程覆盖。
平均能量损失
\documentclass[answers]{exam}
\usepackage{expl3}
\ExplSyntaxOn
% Flag to enable Latexmk
\bool_new:N \g__diaa_use_latexmk_bool
% Uncomment the following line to use Latexmk
%\bool_gset_true:N \g__diaa_use_latexmk_bool
% Flag to output questions and/or answers
\bool_new:N \g__diaa_build_questions_bool
\bool_new:N \g__diaa_build_solved_bool
% Always build questions
\bool_gset_true:N \g__diaa_build_questions_bool
% Build answers if class answers option is set
\legacy_if:nT { printanswers }
{
\bool_gset_true:N \g__diaa_build_solved_bool
}
% Uncomment the following line to not build questions
%\bool_gset_false:N \g__diaa_build_questions_bool
% Uncomment the following line to always build answers even if class answers option is not set
%\bool_gset_true:N \g__diaa_build_solved_bool
% Answers jobname variable
\str_new:N \g__diaa_solved_jobname_str
% LaTeX engine and options variables
\str_new:N \g__diaa_latex_cmd_str
\str_const:Nn \g__diaa_latex_options_str { -synctex=1 ~ -output-directory=out ~ -interaction=nonstopmode }
% Latexmk engine and options variables
\str_const:Nn \g__diaa_latexmk_str { latexmk }
\str_const:Nn \g__diaa_latexmk_options_str { -synctex=1 ~ -output-directory=out }
\str_new:N \g__diaa_latexmk_engine_str
% Final build commands
\str_new:N \g__diaa_cmd_questions_str
\str_new:N \g__diaa_cmd_solved_str
% Build the answer jobname
\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]" }
}
% Set engines based on current engine
\cs_new:Nn \__diaa_set_engine:
{
\sys_if_engine_luatex:T
{
\str_gset:Nn \g__diaa_latex_cmd_str { lualatex }
\str_gset:Nn \g__diaa_latexmk_engine_str { -lualatex }
}
\sys_if_engine_pdftex:T
{
\str_gset:Nn \g__diaa_latex_cmd_str { pdflatex }
\str_gset:Nn \g__diaa_latexmk_engine_str { -pdf }
}
\sys_if_engine_xetex:T
{
\str_gset:Nn \g__diaa_latex_cmd_str { xelatex }
\str_gset:Nn \g__diaa_latexmk_engine_str { -xelatex }
}
}
% Build LaTeX questions command
\cs_new:Nn \__diaa_build_latex_questions_cmd:
{
\str_gset:Nx \g__diaa_cmd_questions_str
{
\g__diaa_latex_cmd_str \c_space_tl
-jobname=\c_sys_jobname_str \c_space_tl
\g__diaa_latex_options_str \c_space_tl
"\string\AtBeginDocument{\string\printanswersfalse}" \c_space_tl
"\string\input{\c_sys_jobname_str}"
}
}
% Build LaTeX answers command
\cs_new:Nn \__diaa_build_latex_solved_cmd:
{
\str_gset:Nx \g__diaa_cmd_solved_str
{
\g__diaa_latex_cmd_str \c_space_tl
-jobname=\g__diaa_solved_jobname_str \c_space_tl
\g__diaa_latex_options_str \c_space_tl
"\string\AtBeginDocument{\string\printanswerstrue}" \c_space_tl
"\string\input{\c_sys_jobname_str}"
}
}
% Build Latexmk questions command
\cs_new:Nn \__diaa_build_latexmk_questions_cmd:
{
\str_gset:Nx \g__diaa_cmd_questions_str
{
\g__diaa_latexmk_str \c_space_tl
\g__diaa_latexmk_engine_str \c_space_tl
\g__diaa_latexmk_options_str \c_space_tl
-usepretex="\string\AtBeginDocument{\string\printanswersfalse}" \c_space_tl
\c_sys_jobname_str
}
}
% Build Latexmk answers command
\cs_new:Nn \__diaa_build_latexmk_solved_cmd:
{
\str_gset:Nx \g__diaa_cmd_solved_str
{
\g__diaa_latexmk_str \c_space_tl
\g__diaa_latexmk_engine_str \c_space_tl
\g__diaa_latexmk_options_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
}
}
% Build the files
\sys_if_shell_unrestricted:T
{
% Set up engines
\__diaa_set_engine:
% Build answers jobname
\__diaa_build_solved_jobname:
% Set up compile commands
\bool_if:NTF \g__diaa_use_latexmk_bool
{
\__diaa_build_latexmk_questions_cmd:
\__diaa_build_latexmk_solved_cmd:
}
{
\__diaa_build_latex_questions_cmd:
\__diaa_build_latex_solved_cmd:
}
% Build questions
\bool_if:NT \g__diaa_build_questions_bool
{
\sys_shell_now:x
{
\str_use:N \g__diaa_cmd_questions_str
}
}
% Build answers
\bool_if:NT \g__diaa_build_solved_bool
{
\sys_shell_now:x
{
\str_use:N \g__diaa_cmd_solved_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}