When on my system I save the following example as test.tex and on the shell (via chdir or cd or whatever) change the active directory to the directory where test.tex is stored, and then compile test.tex by calling latexmk from the shell/command-line via
latexmk -cd -gg -pdflatex="lualatex --shell-escape %O %S" -pdf test.tex
(maybe on your platform you need --enable-write18 instead of --shell-escape)
, then during the first lualatex-run initiated by latexmk, where aux-files don't exist in the beginning, another latexmk-run is initiated where the -jobname-option is used for attaching the phrase _WithAnswers to the current expansion of \jobname.
In any case it is checked whether the expansion of the \jobname-primitive contains the phrase _WithAnswers.
If so, the switch \ifWithAnswers is set to "true".
If not so, the switch \ifWithAnswers is set to "false".
As a result you have one latexmk-run on test.tex which exactly once initiates another latexmk-run on test.tex with -jobname="test_WithAnswers".
In the end you get test.pdf which is without answers and test_WithAnswers.pdf which is with answers.
%=======================================================================================================
\ExplSyntaxOn
\cs_new:Npn \CheckWhetherJobnameContainsPhrase #1 {
\exp_args:Nne \use:nn {\str_if_in:nnTF} {{\jobname} {#1}}
}
\ExplSyntaxOff
\newif\ifWithAnswers
\CheckWhetherJobnameContainsPhrase{_WithAnswers}{%
%----------------------------------------------------------------------------
% Stuff in case the document is to be created with answers
%............................................................................
\WithAnswerstrue
}{%
%----------------------------------------------------------------------------
% Stuff in case the document is to be created without answers
%............................................................................
% - Let's call another instance of latexmk for creating the document-variant
% with answers:
%............................................................................
% (Let's do things when the.aux-files are already read.)
\AtBeginDocument{%
\immediate\write\csname @mainaux\endcsname{\string\providecommand\string\DoOnlyInFirstLaTeXRun[1]{}}%
\providecommand\DoOnlyInFirstLaTeXRun[1]{#1}%
\DoOnlyInFirstLaTeXRun{%
\RequirePackage{shellesc}% more recent releases of LuaTeX don't provide
% \write18 any more. This package makes sure
% under such LuaTeX \write18 is emulated by
% calls to \directlua.
\ShellEscape{%
latexmk -cd
-jobname="\jobname\string_WithAnswers"
-pdflatex="lualatex \csname @percentchar\endcsname O \csname @percentchar\endcsname S"
-pdf
\jobname.tex
}%
}%
}%
%............................................................................
\WithAnswersfalse
}
%=======================================================================================================
\documentclass{article}
\begin{document}
This is a text.
\ifWithAnswers
It comes with answers.
\else
It comes without answers.
\fi
\end{document}
\jobname-primitive you can detect the variant of your document which LaTeX is about to create. I did his in my answer to How to automatically create multiple versions of a document with different paper sizes & reflecting this in the generated PDF filename? – Ulrich Diez Nov 17 '23 at 20:49