3

I have a series of Questions saved as separate tex files and want to loop over them to place them in the document using the exercise package.

If I loop over the Questions I get a compile error.

However, If I put a Question in first and then loop over the Questions there is no error. This seems a bit of a hack - is there a reason that this is happening? Is there a way I can change the Exercise package so that it behaves?

NB: I'm working in overleaf.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{exercise}
\usepackage{tikz}


\begin{document}

\begin{Exercise}
\input{Q1} %if I comment out this line then compilation error

\foreach \i in {1,...,5}
{%
\input{Q\i} 
%Q1,...Q5 are separate text files that all look like \Question this is a question
}%
\end{Exercise}

\end{document}
4

\foreach groups the loop body and so it not suited here: exercise wants to identify the first question.

You can use \pgfplotsforeachungrouped from the pgfplots package instead. Or some other loop command which doesn't group:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{exercise}
\usepackage{expl3}
\ExplSyntaxOn
\newcommand\intstepinline{\int_step_inline:nn}
\ExplSyntaxOff

\begin{document}

\begin{Exercise}

\intstepinline{5} %starts from 1 by default. 
{%
\input{Q#1}
}%
\end{Exercise}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.