Une solution avec l'extension `exercise` :
\documentclass{article}
\usepackage[
%noanswer % modifier ici pour choisir correction ou pas correction
]{exercise}
\begin{document}
\begin{Exercise}[title={Title},label=ex1]
question text
\end{Exercise}
\begin{Answer}[ref={ex1}]
solution
\end{Answer}
\end{document}
Pour créer les deux documents automatiquement, compilez avec :
pdflatex -jobname=document_sol "\newcommand{\version}{}\input{document}" latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=document -pretex="\newcommand{\version}{noanswer}" -usepretex document ; pdflatex -jobname=document_sol "\newcommand{\version}{}\input{document}" ; pdflatex "\newcommand{\version}{noanswer}\input{document}" ; pdflatex "\newcommand{\version}{noanswer}\input{document}"
latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=document_solution -pretex="\newcommand{\version}{}" -usepretex document
(en remplaçant « document » par le nom de votre fichier).
----
\ifdefined\version
\else
\def\version{noanswer} % without answers
%\def\version{} % with answers
\fi
\documentclass{article}
\usepackage[\version]{exercise}
\begin{document}
\begin{Exercise}[title={Title},label=ex1]
question text
\end{Exercise}
\begin{Answer}[ref={ex1}]
solution
\end{Answer}
\end{document}
Le même chose avec un « magic comment » pour TeXstudio :
% !TeX TS-program program = pdflatex -jobname=%_sol "\newcommand{\version}{}\input{%}" latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=% -pretex="\newcommand{\version}{noanswer}" -usepretex % | pdflatex -jobname=%_sol "\newcommand{\version}{}\input{%}" | pdflatex "\newcommand{\version}{noanswer}\input{%}" | pdflatex "\newcommand{\version}{noanswer}\input{%}" latexmk -pdf -pdflatex="pdflatex -synctex=1 -interaction=nonstopmode -shell-escape" -jobname=%_solution -pretex="\newcommand{\version}{}" -usepretex % | txs:///view-pdf "?am)_sol.pdf"
"?am)_solution.pdf"
\documentclass{article}
% setting a default value in case it is compilled differently
\ifdefined\version
\else
\def\version{noanswer} % without answers
%\def\version{} % with answers
\def\version{noanswer}
\fi
\documentclass{article}
\usepackage[\version]{exercise}
\begin{document}
\begin{Exercise}[title={Title},label=ex1]
question text
\end{Exercise}
\begin{Answer}[ref={ex1}]
solution
\end{Answer}
\end{document}