1

My target is to have an output Solved Exam.pdf when \ifSolution is true, and The Exam.pdf when \ifSolution is false.

However, I don't know if this answer is relevant here to change the \jobname and thus all the output files since it only affects the name of the aux file.

P.S. I am using lualatex to compile.

\newif\ifSolution

\ifSolution % https://tex.stackexchange.com/a/252346/2288 \edef\TeXjobname{\jobname} \edef\jobname{\detokenize{Solved Exam}}% <<< the output file is not "Solved Exam.pdf" \documentclass[answers]{exam} \else % https://tex.stackexchange.com/a/252346/2288 \edef\TeXjobname{\jobname} \edef\jobname{\detokenize{Exam}}% <<< the output file is not "Exam.pdf" \documentclass{exam} \fi

\begin{document} \begin{questions} \question a question \begin{solution} the solution \end{solution} \end{questions} \end{document}

Diaa
  • 9,599
  • Could setting the values at compilation time help? See https://tex.stackexchange.com/questions/152716/passing-a-lot-of-parameters-to-a-file-at-compilation-time – Uwe Ziegenhagen Dec 07 '20 at 13:51
  • How to influence the name of the pdf file created with pdfLaTeX (from within the source code)?. I think what you are trying to do is simply not possible. I've always used bash scripts for that. – campa Dec 07 '20 at 14:03
  • @campa would you please provide an answer to my question with such approach? – Diaa Dec 07 '20 at 15:16
  • Uhm, I'm not sure this is really in topic here. In fact my scripts always run the file twice to generate both versions; and they are based on a class I've written, so they look for an optional argument to \documentclass. – campa Dec 07 '20 at 15:19
  • @campa I am working on Win 10. So, if you have some CMD script to share and apply to my question, I would be grateful. – Diaa Dec 07 '20 at 15:26
  • Sorry, Linux scripts. But I can give it a shot. However now I'm on smartphone, so it'll take a while... – campa Dec 07 '20 at 15:29
  • LuaTeX has a finish_pdffile callback. Maybe you can rename the output with Lua callback. It could be as simple as os.rename(tex.jobname..".pdf", "new_name.pdf"). – Alan Xiang Dec 07 '20 at 15:59
  • @AlanXiang I don't know how to do it. If you are willing to provide a relevant answer to my question, I would be grateful. – Diaa Dec 07 '20 at 16:12
  • @Diaa I just did some research and realized it is impossible for this approach to work (https://tex.stackexchange.com/questions/55775/possible-to-run-lua-script-from-lualatex-after-main-compilation-has-been-done). You could either try to see if redefining \jobname is possible, or try to wrap lualatex call around with other tools. One way I can think of is that you can write the value of \ifSolution to an external file and have another program inspect it and rename (copy) the output of LuaTeX. – Alan Xiang Dec 07 '20 at 16:38
  • @AlanXiang I can do it by calling a CMD file by texstudio that has texfot lualatex -jobname="[Exam] <file name>" -synctex=1 -interaction=nonstopmode "\AtBeginDocument{\printanswersfalse}\input{<file name>}". However, I am not sure about if possible by the document itself without external help. – Diaa Dec 07 '20 at 16:49

1 Answers1

1

I am assuming you are using TeXStudio on Windows. This is what I would do.

  1. Save the following file to compile.cmd in project folder. A VERY IMPORTANT REMINDER: BACK UP NECESSARY FILES BEFORE COMPILING YOUR DOCUMENT! This script will automatically delete "Solved Exam.pdf" and "The Exam.pdf" before compiling your TeX document. Make sure you back everything up!
@echo off
echo cleaning old pdf files
del /Q "Solved Exam.pdf" "The Exam.pdf"

echo compiling new pdf file lualatex.exe -synctex=1 -interaction=nonstopmode %1.tex

echo reading file mode set filename="%1-mode.txt" echo reading %filename% set /p mode=<%filename% echo the output indicates %mode% mode

IF "%mode%" EQU "exam" ( copy %1.pdf "The Exam.pdf" echo output copied to "The Exam.pdf" ) IF "%mode%" EQU "solution" ( copy %1.pdf "Solved Exam.pdf" echo output copied to "Solved Exam.pdf" )

  1. In TeXStudio, open settings page and make sure "Show Advanced Options" check box is ticked. In the "Build" tab, add a user command that writes cmd /c compile.cmd %.

  2. Restructure your document as follows. What I did is basically write the value of \ifSolution to \jobname-mode.txt. The cmd script will read this information and behave accordingly.

\documentclass{exam}
\usepackage{expl3}
\usepackage{etoolbox}

\newif\ifSolution

\ExplSyntaxOn

\AtEndDocument{ \iow_open:Nn \g_tmpa_iow {\jobname-mode.txt} \ifSolution \iow_now:Nn \g_tmpa_iow {solution} \else \iow_now:Nn \g_tmpa_iow {exam} \fi \iow_close:N \g_tmpa_iow }

\ExplSyntaxOff

\Solutionfalse

\begin{document} \begin{questions} \question a question \begin{solution} the solution \end{solution} \end{questions} \end{document}

  1. Now you can compile the document by either clicking the menu item Tools->User-><YOUR COMMAND NAME> or using its corresponding hotkey.
Alan Xiang
  • 5,227
  • Thanks for the help but I think you miss something here. When I set \Solutiontrue I need the condition \printanswerstrue to be set in \AtBeginDocument to print the solution and vice versa for the exam mode.So, my program flow should be (1) check the state of \Solution (2) based on the mode, compile by lualatex -jobname="[Exam] <\jobname>" -synctex=1 -interaction=nonstopmode "\AtBeginDocument{\printanswersfalse}\input{<\jobname>}" or lualatex -jobname="[Solution] <\jobname>" -synctex=1 -interaction=nonstopmode "\AtBeginDocument{\printanswerstrue}\input{<\jobname>}" – Diaa Dec 08 '20 at 08:02