# Best way to part answers from solutions?

Now, if anyone could change the title into something better that would be great =)

I am writing a rather long document, which contains plenty of integrals. I have a big list of integrals sorted in a three column style, as pictured below

The image is a tad old. Now, I want to link both the soutions and the answers to each question using hyperref. (Where the answer means just the antiderivate, and the solution means all the small steps to compute the integral) My problem is that I do not know what the best way to do this is. Right now, I have turned every integral into a hyperlink, as the MWE shows.

\documentclass[10pt,a4paper]{article}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage{mathtools}
\usepackage[english]{babel}
\usepackage{changepage}

\newcommand{\dx}{\mathop{}\! \text{d} x}
\newcommand{\R}{ \mathbb{R} }
\newcommand{\CC}{\mathcal{C}}

\newenvironment{IntList}[1]{%
\centering
\setlength{\columnsep}{50pt}

\hypersetup{
citecolor=cyan,         %  color of links to bibliography
filecolor=magenta,      %  color of file links
urlcolor=cyan           %  color of external links
}
\newcommand{\IntExerc}[2]{\item \hyperref[#1]{\mbox{ $$\displaystyle #2$$ }}}

\begin{document}

\subsection{Huge ass list of problems}

\begin{IntList}{3}
\IntExerc{R1.1}{\int \frac{x^2+3x}{x^2} \dx }
\IntExerc{R1.22}{\int\sin(x)\,\mathrm{d}x}
\IntExerc{R1.2}{\int_0^1 \frac{x}{x^2+1} \dx}
\IntExerc{R1.3}{\int \frac{x}{x+1} \dx}
\IntExerc{R1.4}{\int_{-3}^{3} \frac{1}{1-x} \dx}
\IntExerc{R1.5}{\int \sqrt{4-x} \dx}
\IntExerc{R1.6}{\int_{0}^{1/2} \left( 2x - 1\right)^{50} \dx}
\IntExerc{R1.7}{\int \sqrt[n]{x} \dx}
\IntExerc{R1.8}{\int \frac{1}{x \ln x} \dx}
\IntExerc{R1.7}{\int \sin x \cos x \dx}
\IntExerc{R1.8}{\int_{1}^{\sqrt{e-1}} \ln\left( 1 + x^2 \right)x\dx}
\IntExerc{R1.9}{\int \frac{x}{e^x} \dx}
\IntExerc{R1.10}{\int \sin x \cos x \dx}
\IntExerc{R1.11}{\int_1^{e^n} \ln x \dx \ n \in \R}
\IntExerc{R1.12}{\int x \cdot a^x \dx}
\IntExerc{R1.13}{\int \log_{10} (x) \dx}
\IntExerc{R1.14}{\int_{\pi/6}^{\pi/3} \sin(x)^2 \dx}
\IntExerc{R1.15}{\int_1^e \int \frac{\pi}{-x^2} \dx \dx}
\IntExerc{R1.16}{\int_{0}^{\ln 2} x\ln(x+1) \dx}
\IntExerc{R1.17}{\int \frac{x e^x}{(x+1)^2} \dx}
\IntExerc{R1.18}{\int \frac{1}{1+\sqrt{x}}\dx}
\IntExerc{R1.19}{\int \frac{\ln x}{\sqrt{x}} \dx}
\IntExerc{R1.20}{\int_{\pi}^{\pi} \frac{\sin(1/x)e^{x^2}}{\sqrt{x!}} \dx}
\IntExerc{R1.21}{\int \frac{x-1}{\sqrt{x^2 + 2x + 7}} \dx}
\IntExerc{R1.22}{\int \frac{1 + e^x}{\sqrt{e^x + x}} \dx}
\IntExerc{R1.23}{\int \frac{e^x + 1}{e^x - 1} \dx}
\IntExerc{R1.24}{\int_{a}^{a+2\pi} \sin x+1 \dx}
\IntExerc{R1.25}{\int \frac{x^2 + 2x + 2}{x-1} \dx}
\IntExerc{R1.26}{\int_{0}^1 e^{-y^2}  \dx}
\IntExerc{R1.30}{\int_{-3/4}^{4} \frac{x+1}{(x+2)^4}\dx}
\IntExerc{R1.27}{\int x^3\sqrt{1-x^2} \dx}
\IntExerc{R1.40}{\int \ln\left(\frac{1}{\sqrt{x}}\right) \dx}
\IntExerc{R1.28}{\int \sin(2x)e^{\sin(x)^2} \dx}
\IntExerc{R1.29}{\int x \ln \left( \frac{1}{x}\right) \dx}
\IntExerc{R1.30}{\int 2^x e^x \dx}
\IntExerc{R1.30}{\int \binom{x+1}{x} \dx}
\IntExerc{R1.31}{\int \frac{1}{\sqrt{x}\left( 1 + \sqrt{x}\right)} \dx}
\IntExerc{R1.32}{\int_{1}^{\log_e(a)^2} e^{\sqrt{x}}\dx}
\IntExerc{R1.33}{\int \frac{x^2}{x^2 - 1} \dx}
\IntExerc{R1.34}{\int \frac{x}{1 - x^2 + \sqrt{1 - x^2}} \dx}
\IntExerc{R1.34}{\int_{1}^{\sqrt{e-1}} \ln\left( 1 + x^2 \right)x\dx}
\IntExerc{R1.36}{\int \frac{x^2}{x^2 + 4x + 8}\dx}
\IntExerc{R1.37}{\int \frac{1}{x \ln(x)^n} \dx}
\IntExerc{R1.38}{\int_{0}^{\infty} x^3 e^{-x}  \dx}
\IntExerc{R1.39}{\int e^{x + e^x} \dx}
\IntExerc{R1.40}{\int \ln(x)^3 \dx}
\IntExerc{R1.41}{\int \frac{x e^x}{\left( x + 1\right)^2}\dx}
\IntExerc{R1.42}{\int_n^m (m-x)(x-n)\dx}
\IntExerc{R1.41}{\int \frac{\pi}{\sqrt{e^2-16}}\dx}
\IntExerc{R1.40}{\int \frac{\sin(2x)}{\sin x} \dx}
\IntExerc{R1.40}{\int_0^{\pi/3n} \tan(nx) \dx}
\IntExerc{R1.40}{\int \frac{4a}{x^2-a^2} \dx}
\IntExerc{R1.40}{\int e^{\sin(x)^2}e^{\cos(x)^2}  \dx}
\IntExerc{R1.40}{\int \frac{e^{\ln\left(x^2 + 1 \right)}}{x+1} \dx}
\IntExerc{R1.40}{\int_{\pi/6}^{\pi/2} \frac{1}{\tan x} \dx}
\IntExerc{R1.40}{\int \frac{x^2+1}{x(x^2+3)} \dx}
\IntExerc{R1.40}{\int \frac{1}{\sqrt[3]{x} + x} \dx}
\IntExerc{R1.40}{\int \frac{1}{x \ln x - x} \dx}
\IntExerc{R1.40}{\int_{0}^{\pi} \frac{x^2 \sin(x)}{\pi - 2} \dx}
\IntExerc{R1.40}{\int \sqrt{\sqrt{x} + \sqrt{x}\,} \dx}
\IntExerc{R1.40}{\int 2^{\ln x} \dx}
\IntExerc{R1.40}{\int_{2}^{5} \frac{2 - 4}{x^4 - x^2} \dx}
\IntExerc{R1.40}{\int \cos( \sin(x) ) \cos x \dx}
\IntExerc{R1.40}{\int \left(x + x^2\right)e^{3(x+\ln 3)} \dx}
\IntExerc{R1.40}{\int \cos(x)\sin(2x) \dx}
\IntExerc{R1.40}{\int \frac{1}{2x \sqrt{\ln x}} \dx}
\IntExerc{R1.40}{\int \frac{\log_5(x)}{\log_{25}(x)} \dx}
\end{IntList}

\subsubsection{Word problems}

$$\int_{1/2}^{\sqrt{3}/2} \frac{x^3}{\left( \frac{5}{8} - x^4\right)\sqrt{\frac{5}{8} - x^4}}\quad$$

\newpage

\newpage

\begin{align}
\int \frac{x^2+3x}{x^2} \dx & = \int \frac{x^2}{x^2} + \frac{3x}{x^2} \dx = \int 1 \dx + 3\int \frac{dx}{x} = x + 3 \ln|x| + \CC  \label{R1.1}\\
\int\sin(x)\,\mathrm{d}x & = -\cos(x) + C \label{R1.22} \\
\int\sin(x)\,\mathrm{d}x & = -\cos(x) + C \label{R1.40}
\end{align}

First straight integration problems, then word problems

\end{document}


Question(s): What is the best way to separate the plain answer, from the solution? (Are any of the ideas below good?)

• Can one perhaps add a spoiler tag (Opens on click)?
• Make the numbers into separate links?
• Add seperate links next to each integral? (I think this would make it very cluttered)

Oh, and is there any way to make the hyperref go both ways? That I could click on the answer, and get back to the questions?

-

All of this can be achieved using the answers and hyperref package.

In the MWE below, I've linked the question number to the solution, and the solution number back to the question by using the hypertarget and hyperlink mechanism from the hyperref package.

The example I have shown is very simple, but you can get pretty sophisticated with it as I've shown in my answer to

Contest: Show Off Your Skillz in TeX & Friends [TeX.sx birthday]

The main thing to note is that I have renewenvironment for the solution file immediately before I have inputted it- you have to wait until this point to avoid messing up the process of writing to a file.

\documentclass{article}

\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{multicol}

\setlength{\parindent}{0.0mm}

% very useful during de-bugging!
%\usepackage[left]{showlabels}
%\showlabels{hypertarget}

% solutions file
\Opensolutionfile{mysolutions}
\Newassociation{mysolution}{mySoln}{mysolutions}

% new environment that sets up hypertargets both in the question
% section, and in the answer section
\newlist{myenum}{enumerate}{3}
\newcounter{question}
\newenvironment{question}%
{%
\refstepcounter{question}%
\hypertarget{question:{\thequestion}}{}%
\Writetofile{mysolutions}{\protect\hypertarget{soln:\thequestion}{}}%
\item%
}%
{%
\end{myenum}}

\begin{document}

\section{Questions}
\begin{multicols}{3}
\begin{question}
$\int\sqrt{4-x}\mathrm{d}x$
\begin{mysolution}
$-\frac{2}{3}(4-x)^{\frac{3}{2}}+C$
\end{mysolution}
\end{question}
\begin{question}
$\int(2x-1)^{50}\mathrm{d}x$
\begin{mysolution}
$\frac{1}{102}(2x-1)^{51}+C$
\end{mysolution}
\end{question}
\begin{question}
$\int\sqrt[n]{x}\mathrm{d}x$
\begin{mysolution}
$\frac{n}{n+1}x^{\frac{1+n}{n}}+C$
\end{mysolution}
\end{question}
\end{multicols}

% close solution file
\Closesolutionfile{mysolutions}

% renew the solution environment so that it hyperlinks back to
% the question
\renewenvironment{mySoln}[1]{%
\vskip .5cm plus 2cm minus 0.1cm%
}%
{%
}%

\clearpage
% input the file if it exists
\IfFileExists{mysolutions.tex}{\input{mysolutions.tex}}{}
\end{document}

-
I kind of like the idea of using the answer package, but I am feeling I need something a bit more specialized. But you are giving me some good ideas. Would the answer package be compatible with the multicolumn package? –  N3buchadnezzar Feb 22 '12 at 17:58
@N3buchadnezzar yes, absolutely! the answer package works environment by environment- you can put the question environment into multicols easily –  cmhughes Feb 22 '12 at 18:33
@N3buchadnezzar see my edit –  cmhughes Feb 22 '12 at 18:53
cmhughes - I will do some work and get back to this ok? =) It is near what I am looking for, all I need is to put the code into a own command. (I seem to always have problems with enviroments inside commands) and see if I can figure out how to add two answers to each question =) –  N3buchadnezzar Feb 22 '12 at 19:13
@N3buchadnezzar You can add two answers to each question using a newassociation. I've tried using a command approach before, but things work out much more easily using an environment –  cmhughes Feb 22 '12 at 19:39

You can define a new environment to write the answer verbatim into a seperate file, and then you can \input the answer file when necessary.

Here is a private package for my own book:

\ProvidesPackage{latexexercise}[2011/08/03 by Leo]

\RequirePackage{verbatim}

\newwrite\ltxex@ansfile

\def\ltxex@ansfilename{\jobname.ans}

\newcounter{exercise}
\ifdefined\c@chapter
\def\theexercise{\arabic{chapter}.\arabic{exercise}}
\else\ifdefined\c@section
\def\theexercise{\arabic{section}.\arabic{exercise}}
\else
\def\theexercise{\arabic{exercise}}
\fi\fi

\def\exercisename{Exercise}

\newcommand\startexercise{%
\immediate\openout\ltxex@ansfile=\ltxex@ansfilename
\newcommand\stopexercise{%
\immediate\closeout\ltxex@ansfile}
\InputIfFileExists{\ltxex@ansfilename}%
{\PackageInfo{latexexercise}{answer file \ltxex@ansfilename' inputed.}}%
{\PackageWarning{latexexercise}%
{answer file \ltxex@ansfilename' does not exsist.}}}

\newenvironment{exercise}%
{\list{\ltxex@labelstyle\theexercise}%
{\ltxex@listargs\normalfont\normalsize}%
\ltxex@pre}%
{\endlist}
{\immediate\write\ltxex@ansfile{\noexpand\item[\theexercise]}%
\ltxex@startwrite}%
{\ltxex@endwrite}
{\list{}{\ltxex@anslistargs\normalfont\normalsize}}%
{\endlist}
%

\def\ltxex@pre{\item[]\hskip-\labelsep {\large\bfseries \exercisename}}
\def\ltxex@listargs{%
\@nmbrlisttrue\def\@listctr{exercise}%
%\topsep=0pt
\partopsep=0pt
\itemsep=0pt
\leftmargin=0pt
\rightmargin=0pt
\parsep=\parskip
\listparindent=\parindent
\itemindent=\parindent
\labelwidth=0pt
\labelsep=0.5em}
\def\ltxex@labelstyle{\bfseries}

\def\ltxex@anslistargs{%
\def\makelabel##1{\ltxex@anslabelstyle ##1}
\topsep=0pt
\partopsep=0pt
\itemsep=0pt
\leftmargin=0pt
\rightmargin=0pt
\parsep=\parskip
\listparindent=\parindent
\itemindent=\parindent
\labelwidth=0pt
\labelsep=0.5em}
\def\ltxex@anslabelstyle{\bfseries}

\def\ltxex@startwrite{%
\begingroup
\@bsphack
\let\do\@makeother \dospecials
\catcode\^^M\active
\def\verbatim@processline{%
\immediate\write\ltxex@ansfile{\the\verbatim@line}}%
\verbatim@start}

\def\ltxex@endwrite{\@esphack\endgroup}

\endinput


Test code:

\documentclass{article}
\usepackage{latexexercise}
\begin{document}

\startexercise

\section{Foo}

\begin{exercise}
\item $1+1=?$
$1+1=2$.

\item $2\times 2 = ?$
That's 4.
\end{exercise}

\section{Bar}

\begin{exercise}
\item Foo
`