18

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

enter image description here

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}
\begin{adjustwidth}{-3em}{-2em}\begin{multicols}{#1}\begin{enumerate}[itemsep=5pt]}{\end{enumerate}\end{multicols}\end{adjustwidth}}

\usepackage[hidelinks]{hyperref}
\hypersetup{
    colorlinks=true,        %  false: boxed links; true: colored links
    linkcolor=black,        %  color of internal links
    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?

Cheers, thanks in advance for any suggestions, answers or comments =)

David Carlisle
  • 757,742
N3buchadnezzar
  • 11,348
  • 7
  • 55
  • 114
  • I would pay you some money for the answers of those integrals! ;-). No, kidding! But I could give you my code for a cheatsheet on Integration, in exchange. – Cragfelt Dec 30 '17 at 23:03

3 Answers3

11

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.

screenshot

\documentclass{article}

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

\setlength{\parindent}{0.0mm}

\hypersetup{colorlinks=true,
linkcolor=blue}

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

% 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}%
    %  hyperlink to solution
         \hypertarget{question:{\thequestion}}{}%
         \Writetofile{mysolutions}{\protect\hypertarget{soln:\thequestion}{}}%
         \begin{myenum}[label=\bfseries\protect\hyperlink{soln:\thequestion}{\thequestion},ref=\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]{%
         % add some glue
         \vskip .5cm plus 2cm minus 0.1cm%
         {\bfseries \hyperlink{question:#1}{#1.}}%
}%
{%
}%

\clearpage
\section{Answers!}
% input the file if it exists
\IfFileExists{mysolutions.tex}{\input{mysolutions.tex}}{}
\end{document}
cmhughes
  • 100,947
  • 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
  • 1
    @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
  • I think I will use your method =) Now, I have managed to make your solution better for my needs. I really appreaciate your help! – N3buchadnezzar Feb 24 '12 at 18:51
  • I think I will use your method =) Now, I have managed to make your solution better for my needs. I really appreaciate your help! Could you look at my update? I had problems when reseting the numbering for each section (automatically including sections in the answers) and how to add another answer that is hyperlinked to the answer. – N3buchadnezzar Feb 24 '12 at 18:57
  • @N3buchadnezzar glad it helped. As I said, my demonstrate is quite simplistic- if you plan to have multiple sections, you'll have to make the hypertarget mechanism a bit more sophisticated, perhaps along the lines of {question:\thesection\thequestion} etc – cmhughes Feb 24 '12 at 19:27
  • I think I will ask a new question about that, to try to fit into the QA model. Will of course accept your answer =) I am sorry I am not good enough to figure out these things myself, but I am at least trying. You are very helpsome – N3buchadnezzar Feb 24 '12 at 19:41
  • @N3buchadnezzar you're welcome! I'll watch for your question :) – cmhughes Feb 24 '12 at 19:56
  • @cmhughes: I like your method and have adopted it for writing homeworks. However, occasionally I want a footnote, and footnote #3 seems to link to question #3, even if they are unrelated. Any suggestion to fix this? – Chris Chudzicki Sep 15 '13 at 13:38
  • @mrc if you can create a MWE, a new question might be in order :) – cmhughes Sep 15 '13 at 16:48
  • @cmhughes please ignore me. It works perfectly. – Chris Chudzicki Sep 15 '13 at 19:29
3

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
  \@addtoreset{exercise}{chapter}
  \def\theexercise{\arabic{chapter}.\arabic{exercise}}
\else\ifdefined\c@section
  \@addtoreset{exercise}{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
  \immediate\write\ltxex@ansfile{\noexpand\begin{answerlist}}}
\newcommand\stopexercise{%
  \immediate\write\ltxex@ansfile{\noexpand\end{answerlist}}%
  \immediate\closeout\ltxex@ansfile}
\newcommand\printanswer{%
  \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}
\newenvironment{answer}%
  {\immediate\write\ltxex@ansfile{\noexpand\item[\theexercise]}%
   \ltxex@startwrite}%
  {\ltxex@endwrite}
\newenvironment{answerlist}%
  {\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=?$
\begin{answer}
$1+1=2$.
\end{answer}

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

\section{Bar}

\begin{exercise}
\item Foo
\begin{answer}
Bar
\end{answer}
\end{exercise}

\stopexercise

\section{Solutions}
\printanswer
\end{document}
Leo Liu
  • 77,365
  • Well, for updated question, this solution is not suitable. I leave it for reference. It is not difficult to add hyperlink, but I've no much time. – Leo Liu Feb 22 '12 at 16:49
1

Another approach could bring the eqexam package, working for the usual drivers like pdflatex, xelatextex, etc. - see documentation on CTAN for this package.

Here the code:

\documentclass{article}
\usepackage{amsmath}
\usepackage[%
links,        % to activate links between problem and solutions (within the solution set there is a \square symbol to link back to the problem)
%answerkey,   % to present the solutions right after the problem (no hyperlink then needed)
%nosolutions  % to not setup the solutions at all -- and erase the hyperlinks
]{eqexam}
\autotabOn    % to allow to setup the parts of the problem within a tabular environment

\begin{document}
\begin{exam}{myExam}
\begin{problem*}
Solve each of the integrals.

\begin{parts}[3] % Setting up 3 columns for the parts of the problem
\item $\int \frac{x^2+3x}{x^2}\,\text{d}x$
%
\begin{solution}
Solution to item 1 a
\end{solution}
%
\item $\int\sin(x)\,\text{d}x$
%
\begin{solution}
Solution to item 1 b
\end{solution}
%
\item $\int_0^1 \frac{x}{x^2+1}\,\text{d}x$
%
\begin{solution}
Solution to item 1 c
\end{solution}
%
\item $\int \frac{x}{x+1}\,\text{d}x$
%
\begin{solution}
Solution to item 1 d
\end{solution}
%
\item $\int_{-3}^{3} \frac{1}{1-x}\,\text{d}x$
%
\begin{solution}
Solution to item 1 e
\end{solution}
%
\item $\int \sqrt{4-x}\,\text{d}x$
%
\begin{solution}
Solution to item 1 f
\end{solution}
%
\item $\int_{0}^{1/2} \left( 2x - 1\right)^{50}\,\text{d}x$
%
\begin{solution}
Solution to item 1 g
\end{solution}
%
\item $\int \sqrt[n]{x}\,\text{d}x$
%
\begin{solution}
Solution to item 1 h
\end{solution}
%
\item $\int \frac{1}{x \ln x}\,\text{d}x$
%
\begin{solution}
Solution to item 1 i
\end{solution}
\end{parts}
\end{problem*}
\end{exam}
\end{document}

enter image description here

Note:

You can suppress the solutions with the option: nosolutions

You can add the solutions right after the problem with the option: answerkey

Maybe this could be a possible way to present your problems and solutions fully linked forth and back.