# Combining Latex files with different preambles

I am in the process of writing a Python program that scrapes exam questions in tex files from a website and combines them to create a new paper. The questions are picked by random from a spreadsheet, so that questions from various years (and therefore from different latex files) may be combined. Here is an example of such a paper (1987), and here is an example from a much newer paper (2018).

The preambles are considerably distinct and this makes it difficult to simply include them into the same latex file. How could I, for example, take a particular question from the older paper and another question from the newer paper and produce a single pdf file? I have attempted to use the subfiles and standalone packages but I get bombarded with errors. I have also considered compiling each latex file separately and concatenating them using pdfpages or a similar command line utility but I would like to avoid having only a single question per page. I have thought about programmatically retrieving the "union" of the preambles from the two (or more) files and simply substituting in each \question in a single latex file, but this is a last resort.

I have opted to use the combine document class and structure the main document similar to below:

\documentclass{combine}

\begin{document}

\begin{papers}

\let\clearpage\relax

\import{question-file-1}

\import{question-file-2}

\end{papers}

\end{document}


The use of the papers environment proved necessary, as did the line \let\clearpage\relax to ensure that the documents do not simply start at the next page. Compiling the main document produces a consistent pdf file, but there are many "X already defined!" errors which thankfully do not disrupt the compilation of the pdf (and especially so after passing the interaction=nonstopmode argument to latexmk).