[Tex/LaTex] Commands to duplicate a page

macros

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.

Best Answer

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.