# Duplicate text in multiple places in different environments (lstlisting?)?

folks---

I got a question I'm not sure how to solve. I'm putting together a compendium of my exam questions in a single pdf that I'd like to share with other teachers who also use LaTeX. The pickle is that while LaTeX puts out a beautiful pdf, it can be pretty difficult to copy/paste from the pdf into a different TeX file, if someone else wanted to use my problem. I'd of course provide the source code for each problem in its own little problem file, but digging through an entire folder or single file to find a particular problem can be a bit of a pain.

I know---these seem like such small complaints as to be not worthwhile, and are mostly the product of laziness. But if they can be solved, it'd make working on this with others a bit less error-prone and a bit smoother to review, copy, and modify problems going into the future.

What I'm hoping for is to be able to do something like:

\documentclass[options]{exam}

\usepackage{graphicx}
\usepackage{tikz}
\tikzlibrary{blahblah}
\usepackage{listings}
\usepackages{any other necessary packages to make this work}

\begin{document}

\begin{questions}

\some-command-to-duplicate-question,but-in-a-lstlistings-sort-of-environment{%
\question A question?

\begin{solution}
The solution
\end{solution}
}

\same-command-as-before{%
\includegraphics{diagramforquestion}%could also be for Tikz drawings, tables, other figures, etc

\question Another question?

\begin{solution}
Another solution.
\end{solution}
}

\end{questions}

\end{document}


That way, in the pdf, there would be:

• A list of questions, complete with diagrams/etc that are typeset properly by LaTeX;

• some lstlistings or similar environment, showing the source code for that problem.

Obviously, I could just copy/paste the text of the question into a lstlisting environment, but if any changes are made, the person collaborating with me that makes changes to a question (or, more likely, I) will have to remember to copy all the changes into the second environment, whereas if there was some way of issuing some command or redefining some environment for this compendium where it would typeset and output the source code for easy copying, with only one place needed to modify, that would make the individual problem files a lot cleaner and easier to maintain over time.

Both input and lstinputlisting are your friends here.

You can modify the \question command to include files for solutions too, the only thing is if they're used as \input they have to be valid TeX.

\documentclass[10pt]{article}

\usepackage{graphicx}
\usepackage{tikz}
\usepackage{listings}
\lstset{basicstyle=\footnotesize\ttfamily,breaklines=true}

\newcommand{\question}[4]{
#1

The question is

\begin{center}
\input{#2}
\end{center}

For copy-pasting, the question is

\lstinputlisting{#2}

The solution is here:
\begin{center}
#3
\end{center}

#4
}

\begin{document}

Introduction

\begin{enumerate}
A figure to help explain

\begin{tikzpicture}
\draw (0,0) -- (2,0) -- (2,2) -- (0,0);
\end{tikzpicture}
}

To make this a true MWE, I have a file called q1.tex in the same directory that is this:
If $n = 3$, $x^n + y^n = z^n$ has no integer solutions. If you have extra time, please prove this for all other $n > 2$.