Advertisement
Guest User

Untitled

a guest
Apr 22nd, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.58 KB | None | 0 0
  1. \documentclass[answers]{exam}
  2.  
  3. \usepackage{expl3}
  4.  
  5. \ExplSyntaxOn
  6.  
  7. % Flag to enable Latexmk
  8. \bool_new:N \g__diaa_use_latexmk_bool
  9.  
  10. % Uncomment the following line to use Latexmk
  11. % \bool_gset_true:N \g__diaa_use_latexmk_bool
  12.  
  13.  
  14. % Flag to output two files
  15. \bool_new:N \g__diaa_build_two_files_bool
  16.  
  17. \legacy_if:nT { printanswers }
  18. {
  19. \bool_gset_true:N \g__diaa_build_two_files_bool
  20. }
  21.  
  22. % Uncomment the following line to force build two files
  23. % \bool_gset_true:N \g__diaa_build_two_files_bool
  24.  
  25.  
  26. % Answers jobname variable
  27. \str_new:N \g__diaa_solved_jobname_str
  28.  
  29. % LaTeX engine and options variables
  30. \str_new:N \g__diaa_latex_cmd_str
  31. \str_const:Nn \g__diaa_latex_options {-interaction=nonstopmode }
  32.  
  33. % Latexmk engine and options variables
  34. \str_const:Nn \g__diaa_latexmk_str { latexmk }
  35. \str_new:N \g__diaa_latexmk_engine_str
  36.  
  37. % Final build commands
  38. \str_new:N \g__diaa_cmd_questions_str
  39. \str_new:N \g__diaa_cmd_solved_str
  40.  
  41.  
  42. % Build the answer jobname
  43. \cs_new:Nn \__diaa_build_solved_jobname:
  44. {
  45. \str_gset:Nx \g__diaa_solved_jobname_str { \c_sys_jobname_str }
  46. \str_gremove_all:Nn \g__diaa_solved_jobname_str { " }
  47. \str_gput_left:Nn \g__diaa_solved_jobname_str { "[solved]~ }
  48. \str_gput_right:Nn \g__diaa_solved_jobname_str { " }
  49. }
  50.  
  51. % Set engines based on current engine
  52. \cs_new:Nn \__diaa_set_engine:
  53. {
  54. \sys_if_engine_luatex:T
  55. {
  56. \str_gset:Nn \g__diaa_latex_cmd_str { lualatex }
  57. \str_gset:Nn \g__diaa_latexmk_engine_str { -lualatex ~ -g ~ -interaction=nonstopmode }
  58. }
  59. \sys_if_engine_pdftex:T
  60. {
  61. \str_gset:Nn \g__diaa_latex_cmd_str { pdflatex }
  62. \str_gset:Nn \g__diaa_latexmk_engine_str { -pdf ~ -g ~ -interaction=nonstopmode }
  63. }
  64. \sys_if_engine_xetex:T
  65. {
  66. \str_gset:Nn \g__diaa_latex_cmd_str { xelatex }
  67. \str_gset:Nn \g__diaa_latexmk_engine_str { -xelatex }
  68. }
  69. }
  70.  
  71. % Build LaTeX questions command
  72. \cs_new:Nn \__diaa_build_latex_questions_cmd:
  73. {
  74. \str_gset:Nx \g__diaa_cmd_questions_str
  75. {
  76. \g__diaa_latex_cmd_str \c_space_tl
  77. -jobname=\c_sys_jobname_str \c_space_tl
  78. \g__diaa_latex_options \c_space_tl
  79. "\string\AtBeginDocument{\string\printanswersfalse}" \c_space_tl
  80. "\string\input{\c_sys_jobname_str}"
  81. }
  82. }
  83.  
  84. % Build LaTeX answers command
  85. \cs_new:Nn \__diaa_build_latex_solved_cmd:
  86. {
  87. \str_gset:Nx \g__diaa_cmd_solved_str
  88. {
  89. \g__diaa_latex_cmd_str \c_space_tl
  90. -jobname=\g__diaa_solved_jobname_str \c_space_tl
  91. \g__diaa_latex_options \c_space_tl
  92. "\string\AtBeginDocument{\string\printanswerstrue}" \c_space_tl
  93. "\string\input{\c_sys_jobname_str}"
  94. }
  95. }
  96.  
  97. % Build Latexmk questions command
  98. \cs_new:Nn \__diaa_build_latexmk_questions_cmd:
  99. {
  100. \str_gset:Nx \g__diaa_cmd_questions_str
  101. {
  102. \g__diaa_latexmk_str \c_space_tl
  103. \g__diaa_latexmk_engine_str \c_space_tl
  104. -usepretex="\string\AtBeginDocument{\string\printanswersfalse}" \c_space_tl
  105. \c_sys_jobname_str
  106. }
  107. }
  108.  
  109. % Build Latexmk answers command
  110. \cs_new:Nn \__diaa_build_latexmk_solved_cmd:
  111. {
  112. \str_gset:Nx \g__diaa_cmd_solved_str
  113. {
  114. \g__diaa_latexmk_str \c_space_tl
  115. \g__diaa_latexmk_engine_str \c_space_tl
  116. -usepretex="\string\AtBeginDocument{\string\printanswerstrue}" \c_space_tl
  117. -jobname=\g__diaa_solved_jobname_str \c_space_tl
  118. \c_sys_jobname_str
  119. }
  120. }
  121.  
  122.  
  123. % Build the files
  124. \sys_if_shell_unrestricted:T
  125. {
  126. % Set up engines
  127. \__diaa_set_engine:
  128.  
  129. % Build answers jobname
  130. \__diaa_build_solved_jobname:
  131.  
  132. % Set up compile commands
  133. \bool_if:NTF \g__diaa_use_latexmk_bool
  134. {
  135. \__diaa_build_latexmk_questions_cmd:
  136. \__diaa_build_latexmk_solved_cmd:
  137. }
  138. {
  139. \__diaa_build_latex_questions_cmd:
  140. \__diaa_build_latex_solved_cmd:
  141. }
  142.  
  143. % Build questions
  144. \sys_shell_now:x
  145. {
  146. \str_use:N \g__diaa_cmd_questions_str
  147. }
  148.  
  149. % Build answers
  150. \bool_if:NT \g__diaa_build_two_files_bool
  151. {
  152. \sys_shell_now:x
  153. {
  154. \str_use:N \g__diaa_cmd_solved_str
  155. }
  156. }
  157. \stop
  158. }
  159.  
  160. \ExplSyntaxOff
  161.  
  162. \begin{document}
  163. \begin{questions}
  164. \question One of these things is not like the others; one of these things is
  165. not the same. Which one is different?
  166. \begin{oneparchoices}
  167. \choice John
  168. \choice Paul
  169. \choice George
  170. \choice Ringo
  171. \end{oneparchoices}
  172. \begin{solution}
  173. the solution
  174. \end{solution}
  175. \end{questions}
  176. \end{document}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement