1

I'm creating exams, so I need to produce (almost) identical versions of the exam, that would differ in a few characters. For instance

\documentclass{article}

\begin{document}

Here I use the variable $x$

\newpage

Here I use the variable $y$

\end{document}

To avoid mistakes when trying to replicate edits along different pages, what I would like to do would be something like

\documentclass{article}

\begin{document}

\page{Here I use the variable #1}

\pages{$x$,$y$}

\end{document}

Any suggestions on how to do this efficiently? I know how to create a macro and then I could repeat that macro on every page. The concrete question is how to do a command with variable arguments that iterates over the arguments and applies the macro \page to each of them.

  • 3
    Provide some samples of what your input might be, together with the corresponding output. At the moment, the input seems very elementary, and I'm sure there are some more complicated things you'll eventually be after. – Werner Oct 17 '17 at 00:48
  • https://www.auto-multiple-choice.net/ – Fran Oct 17 '17 at 02:22
  • You can create a macro for the page with the arguments for the changes. – John Kormylo Oct 17 '17 at 03:34
  • @JohnKormylo: thanks, I should have been more specific. The concrete question I have is how to make a macro iterate over the arguments so that I do it with a single macro like in the code I wrote above. – Martin Argerami Oct 17 '17 at 04:14
  • Your problem seems to be an XY problem at the moment. There are plenty of ways to iterate over a list, but from your question it's hard to see how they would actually help solve your problem. – Alan Munn Oct 17 '17 at 04:19

1 Answers1

3

Based on details from How to iterate over a comma separated list?, you can define your versions of \page and \pages in the following way:

enter image description here

\documentclass{article}

\usepackage[paper=a6paper]{geometry}% Just for this example

\usepackage{etoolbox}

\makeatletter
\newcommand{\page}{\long\def\@page##1}% \page defines \@page
\newcommand{\pages}[1]{%
  \renewcommand{\do}{\@page}% Each element will be passed to \@page
  \docsvlist{#1}% Iterate over CSV list
}
\makeatother

\begin{document}

\page{%
  \newpage
  Here I use the variable #1?%
}

\pages{$x$,$y$}

% ====================

\newcounter{mycounter}
\page{%
  \newpage
  \stepcounter{mycounter}\themycounter: \quad Solve #1
}

\pages{
  $a^2 + b^2 ={} ?$,
  $\displaystyle \sum_{i=1}^\infty \frac{1}{i} ={} ?$,
  $1 + (-1) ={} ?$
}

\end{document}

\page passes its argument on to making a \definition called \@page. \pages iterates over the CSV list, passing every item to \@page for execution.

Werner
  • 603,163