Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I asked this question here Best way to part answers from solutions? And obtained a very good answer. Though I now have a new problem.

I want to type the question, then have one answer linked to the number. And one answer linked to the question itself. (See my MWE in the inked thread for an example. Clicking on the number next to the problem should send you one place, clicking on the equation itself, should send you elsewhere)

Here is an MWE, for my progress so far

\documentclass{article}

\usepackage{answers}                            
\usepackage{enumitem}                       
\usepackage{hyperref}
\usepackage{multicol}
\usepackage{changepage}
\usepackage{mathtools}
\newcommand{\dx}{\mathop{}\! \text{d} x}
\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}[section]
\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,itemsep=5pt]
         \item%
        }%
        {%
        \end{myenum}}

\newenvironment{IntList}[1]{%
\Opensolutionfile{mysolutions}
\centering
\setlength{\columnsep}{50pt}
\begin{adjustwidth}{-3em}{-2em}\begin{multicols}{#1}}{\end{multicols}\end{adjustwidth}\Closesolutionfile{mysolutions}}

\begin{document}

\begin{IntList}{3}
    \begin{question}
        $\displaystyle \int\sqrt{4-x}\dx$
            \begin{mysolution}
                $2+3+4$
            \end{mysolution}
    \end{question}
\end{IntList}

\section{helo}

\begin{IntList}{3}
    \begin{question}
        $\displaystyle \int\sqrt{4-x}\dx$
            \begin{mysolution}
                $ \int x^3 + 2$
            \end{mysolution}        
    \end{question}
    \begin{question}
        $\displaystyle \int \sqrt{\frac{1}{x^2+1}}\dx$
            \begin{mysolution}
                $-\frac{2}{3}(4-x)^{\frac{3}{2}}+C$
            \end{mysolution}        
    \end{question}    
\end{IntList}

% 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}

So I want to add another answer below each question, for an example like below

\begin{IntList}{3}
    \begin{question}
        $\displaystyle \int\sqrt{4-x}\dx$
            \begin{mysolution}
                $2+3+4$
            \end{mysolution}
             \begin{myanswer}
                To solve this problem one must first...
            \end{mysanswer}
    \end{question}
\end{IntList}

Where my answer is linked to the whole question and the solution is linked to the number. Prefferably the linked equation should work the same way as the answer does. Eg the hyperref works both ways. Any ideas?

Edit: Here is a more minimal example

\documentclass[10pt,a4paper]{article}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsmath}

\usepackage{enumitem}
\usepackage{multicol}
\usepackage{mathtools}

\newenvironment{IntList}[1]{%
\centering
\setlength{\columnsep}{50pt}
\begin{multicols}{#1}\begin{enumerate}[itemsep=5pt]}{\end{enumerate}\end{multicols}}

\usepackage[hidelinks]{hyperref}
\newcommand{\IntExerc}[2]{\item \hyperref[#1]{\mbox{ \( \displaystyle #2 \) }}}

\begin{document}

\begin{IntList}{3}
\IntExerc{R1.1}{\int \frac{x^2+3x}{x^2} \,\mathrm{d}x}
\IntExerc{R1.22}{\int\sin(x)\,\mathrm{d}x}
\IntExerc{R1.2}{\int_0^1 \frac{x}{x^2+1} \,\mathrm{d}x}
\end{IntList}

\newpage

\newpage

\begin{align}
\int \frac{x^2+3x}{x^2} \, \mathrm{d}x & = \int \frac{x^2}{x^2} + \frac{3x}{x^2} \, \mathrm{d}x = \int 1 \mathrm{d}x + 3\int \frac{\mathrm{d}x}{x} = x + 3 \ln|x| + \mathcal{C}  \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.2}
\end{align}

\end{document}

This is closer to the output I want from the answers, package. Note that I have to type the answers seperately, add labels. And have no hyperlinks attached to the numbers.

( I would also prefer to have the syntax below, but I guess this is not possible. )

\begin{Questions}[PartI]
\question
\solution
\answer

\question
\solution
\answer
\end{Questions}

\display{solution}{PartI}

\display{answer}{PartI}

Now, any comments and help would be great.


EDIT:

I made some progress! Is there any way to make the \long and \short "answers" to be incuded at the beginning? It seems cumbersome to type them over and over again.

\documentclass{article}

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

\newcommand\getcurrentref[1]{%
 \ifnumequal{\value{#1}}{0}
  {??}
  {\the\value{#1}}%
}    

\hypersetup{colorlinks=true}

\setlength{\parindent}{0.0mm}


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

% solutions files
% short solutions
\Opensolutionfile{shortsolutions}
\Newassociation{shortsolution}{shortSoln}{shortsolutions}
\Newassociation{short}{shortSolns}{shortsolutions}
% long solutions
\Opensolutionfile{longsolutions}
\Newassociation{longsolution}{longSoln}{longsolutions}
\Newassociation{longs}{longsSolns}{longsolutions}

% new environment that sets up hypertargets both in the question
% section, and in the answer section
\newlist{myenum}{enumerate}{3}
\newcounter{question}[subsection]
\newenvironment{question}[1]%
    {%
    \refstepcounter{question}%
    %  hyperlink to solution
         \hypertarget{question:{\thesubsection\thequestion}}{}%
         \Writetofile{shortsolutions}{\protect\hypertarget{shortsoln:\thesubsection\thequestion}{}}%
         \Writetofile{longsolutions}{\protect\hypertarget{longsoln:\thesubsection\thequestion}{}}%
         \begin{myenum}[label=\bfseries\protect\hyperlink{shortsoln:\thesubsection\thequestion}{\thequestion.},ref=\thequestion]
         \item%
            \hypersetup{linkcolor=black}%
            \hyperlink{longsoln:\thesubsection\thequestion}{#1}%
        }%
        {%
        \end{myenum}}

\newenvironment{IntList}[1]{%
\Opensolutionfile{mysolutions}
\centering
\setlength{\columnsep}{50pt}
\begin{adjustwidth}{-3em}{-2em}\begin{multicols}{#1}}{\end{multicols}\end{adjustwidth}\Closesolutionfile{mysolutions}}


\begin{document}

\section{Problems}

\subsection{Questions}
\begin{multicols}{3}

                 \begin{short}
                \vspace*{\baselineskip}
                \color{white} hello
                \vspace*{-\baselineskip}
            \end{short}
            \begin{longs}
                 \vspace*{\baselineskip}
                \color{white} hello
                \vspace*{-\baselineskip}
            \end{longs}

    \begin{question}{$\int\sqrt{4-x}\mathrm{d}x$}
            \begin{shortsolution}
                $-\frac{2}{3}(4-x)^{\frac{3}{2}}+C$
            \end{shortsolution}
            \begin{longsolution}
                In order to do this, we must first make a substitution\ldots
            \end{longsolution}
    \end{question}
    \begin{question}{$\int(2x-1)^{50}\mathrm{d}x$}
            \begin{shortsolution}
                $\frac{1}{102}(2x-1)^{51}+C$
            \end{shortsolution}
            \begin{longsolution}
                A very long solution| much longer than the shorter solution
            \end{longsolution}
    \end{question}
    \begin{question}{$\int\sqrt[n]{x}\mathrm{d}x$}
            \begin{shortsolution}
                $\frac{n}{n+1}x^{\frac{1+n}{n}}+C$
            \end{shortsolution}
            \begin{longsolution}
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
            \end{longsolution}
    \end{question}
\end{multicols}

\subsection{Some more questions}

\begin{multicols}{3}

                 \begin{short}
                \vspace*{\baselineskip}
                \color{white} hello
                \vspace*{-\baselineskip}
            \end{short}
            \begin{longs}
                 \vspace*{\baselineskip}
                \color{white} hello
                \vspace*{-\baselineskip}
            \end{longs}

    \begin{question}{$\int\sqrt{4-x}\mathrm{d}x$}
            \begin{shortsolution}
                $-\frac{2}{3}(4-x)^{\frac{3}{2}}+C$
            \end{shortsolution}
            \begin{longsolution}
                hi
            \end{longsolution}
    \end{question}
    \begin{question}{$\int(2x-1)^{50}\mathrm{d}x$}
            \begin{shortsolution}
                $\frac{1}{102}(2x-1)^{51}+C$
            \end{shortsolution}
            \begin{longsolution}
                ho
            \end{longsolution}
    \end{question}
    \begin{question}{$\int\sqrt[n]{x}\mathrm{d}x$}
            \begin{shortsolution}
                $\frac{n}{n+1}x^{\frac{1+n}{n}}+C$
            \end{shortsolution}
            \begin{longsolution}
               lets go
            \end{longsolution}
    \end{question}

\end{multicols}

% close solution files
\Closesolutionfile{shortsolutions}
\Closesolutionfile{longsolutions}


\clearpage
\section{SHORT answers}
% renew the SHORT solution environment so that it hyperlinks back to 
% the question
\renewenvironment{shortSoln}[1]{%
         % add some glue
         \vskip .5cm plus 2cm minus 0.1cm%
         {\bfseries \hyperlink{question:#1}{#1.}}%
}%
{%
}%
% input the file if it exists
\IfFileExists{shortsolutions.tex}{\input{shortsolutions.tex}}{}

\clearpage
\section{LONG answers}
% renew the LONG solution environment so that it hyperlinks back to 
% the question
\renewenvironment{longSoln}[1]{%
         % add some glue
         \vskip .5cm plus 2cm minus 0.1cm%
         {\bfseries \hypersetup{linkcolor=blue}\hyperlink{question:#1}{#1.}}%
}%
{%
}%
% input the file if it exists
\IfFileExists{longsolutions.tex}{\input{longsolutions.tex}}{}
\end{document}
share|improve this question
4  
Is this a Maximum Working Example? Can you try and cut it down a bit please? –  Yiannis Lazarides Feb 24 '12 at 20:11
    
I can add another more mininmal example, it is in the other thread. Reason for adding all the "junk" is to make sure everything works together.. –  N3buchadnezzar Feb 24 '12 at 20:14
    
I guess is fine; It's not that the earth will be swallowed in a black hole;) –  Yiannis Lazarides Feb 24 '12 at 20:21
    
@YiannisLazarides: Well don't be too sure, the only way to know is to check the current status: has the large hadron collider destroyed the world yet :-) –  Peter Grill Feb 24 '12 at 20:48
    
@PeterGrill Appropriate link for a question and answers MWE:) –  Yiannis Lazarides Feb 24 '12 at 20:50
show 1 more comment

1 Answer

up vote 8 down vote accepted
+300

So it sounds like you want both a shortsolution environment and a longsolution environment. Here's a solution that does it- it's very similar to my solution to your previous question, hope it's satisfactory, let me know if not.

I've colour-coded the links such that

  • red links go to the shortsolutions
  • blue links go to the longsolutions

enter image description here

enter image description here

\documentclass{article}

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

\hypersetup{colorlinks=true}

\setlength{\parindent}{0.0mm}


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

% solutions files
% short solutions
\Opensolutionfile{shortsolutions}
\Newassociation{shortsolution}{shortSoln}{shortsolutions}
% long solutions
\Opensolutionfile{longsolutions}
\Newassociation{longsolution}{longSoln}{longsolutions}

% new environment that sets up hypertargets both in the question
% section, and in the answer section
\newlist{myenum}{enumerate}{3}
\newcounter{question}
\newenvironment{question}[1]%
    {%
    \refstepcounter{question}%
    %  hyperlink to solution
         \hypertarget{question:{\thequestion}}{}%
         \Writetofile{shortsolutions}{\protect\hypertarget{shortsoln:\thequestion}{}}%
         \Writetofile{longsolutions}{\protect\hypertarget{longsoln:\thequestion}{}}%
         \begin{myenum}[label=\bfseries\protect\hyperlink{shortsoln:\thequestion}{\thequestion},ref=\thequestion]
         \item%
            \hypersetup{linkcolor=blue}%
            \hyperlink{longsoln:\thequestion}{#1}%
        }%
        {%
        \end{myenum}}

\begin{document}

\section{Questions}
\begin{multicols}{3}
    \begin{question}{$\int\sqrt{4-x}\mathrm{d}x$}
            \begin{shortsolution}
                $-\frac{2}{3}(4-x)^{\frac{3}{2}}+C$
            \end{shortsolution}
            \begin{longsolution}
                In order to do this, we must first make a substitution\ldots
            \end{longsolution}
    \end{question}
    \begin{question}{$\int(2x-1)^{50}\mathrm{d}x$}
            \begin{shortsolution}
                $\frac{1}{102}(2x-1)^{51}+C$
            \end{shortsolution}
            \begin{longsolution}
                A very long solution| much longer than the shorter solution
            \end{longsolution}
    \end{question}
    \begin{question}{$\int\sqrt[n]{x}\mathrm{d}x$}
            \begin{shortsolution}
                $\frac{n}{n+1}x^{\frac{1+n}{n}}+C$
            \end{shortsolution}
            \begin{longsolution}
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
            \end{longsolution}
    \end{question}
\end{multicols}

% close solution files
\Closesolutionfile{shortsolutions}
\Closesolutionfile{longsolutions}


\clearpage
\section{SHORT answers}
% renew the SHORT solution environment so that it hyperlinks back to 
% the question
\renewenvironment{shortSoln}[1]{%
         % add some glue
         \vskip .5cm plus 2cm minus 0.1cm%
         {\bfseries \hyperlink{question:#1}{#1.}}%
}%
{%
}%
% input the file if it exists
\IfFileExists{shortsolutions.tex}{\input{shortsolutions.tex}}{}

\clearpage
\section{LONG answers}
% renew the LONG solution environment so that it hyperlinks back to 
% the question
\renewenvironment{longSoln}[1]{%
         % add some glue
         \vskip .5cm plus 2cm minus 0.1cm%
         {\bfseries \hypersetup{linkcolor=blue}\hyperlink{question:#1}{#1.}}%
}%
{%
}%
% input the file if it exists
\IfFileExists{longsolutions.tex}{\input{longsolutions.tex}}{}
\end{document}

Following the question update, and in particular, is it possible to include the answers at the beginning?

Yes, this is possible, but remember that your solution files (shortsolutions.tex and longsolutions.tex) are updated every time you compile. If you input them at the beginning of the document, you'll actually be viewing a previous version of them- that's why it works so well when you include them at the end of your document.

Here's a MWE demonstrating how this works- note that you'll need to compile twice after editing any of the solutions.

\documentclass{article}

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

\hypersetup{colorlinks=true}

\setlength{\parindent}{0.0mm}


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

% solutions files
% short solutions
\Newassociation{shortsolution}{shortSoln}{shortsolutions}
% long solutions
\Newassociation{longsolution}{longSoln}{longsolutions}

% new environment that sets up hypertargets both in the question
% section, and in the answer section
\newlist{myenum}{enumerate}{3}
\newcounter{question}
\newenvironment{question}[1]%
    {%
    \refstepcounter{question}%
    %  hyperlink to solution
         \hypertarget{question:{\thequestion}}{}%
         \Writetofile{shortsolutions}{\protect\hypertarget{shortsoln:\thequestion}{}}%
         \Writetofile{longsolutions}{\protect\hypertarget{longsoln:\thequestion}{}}%
         \begin{myenum}[label=\bfseries\protect\hyperlink{shortsoln:\thequestion}{\thequestion},ref=\thequestion]
         \item%
            \hypersetup{linkcolor=blue}%
            \hyperlink{longsoln:\thequestion}{#1}%
        }%
        {%
        \end{myenum}}

\begin{document}

\section{SHORT answers}
% renew the SHORT solution environment so that it hyperlinks back to 
% the question
\renewenvironment{shortSoln}[1]{%
         % add some glue
         \vskip .5cm plus 2cm minus 0.1cm%
         {\bfseries \hyperlink{question:#1}{#1.}}%
}%
{%
}%
% input the file if it exists
\IfFileExists{shortsolutions.tex}{\input{shortsolutions.tex}}{}

\clearpage
\section{LONG answers}
% renew the LONG solution environment so that it hyperlinks back to 
% the question
\renewenvironment{longSoln}[1]{%
         % add some glue
         \vskip .5cm plus 2cm minus 0.1cm%
         {\bfseries \hypersetup{linkcolor=blue}\hyperlink{question:#1}{#1.}}%
}%
{%
}%
% input the file if it exists
\IfFileExists{longsolutions.tex}{\input{longsolutions.tex}}{}

\clearpage

\Opensolutionfile{shortsolutions}
\Opensolutionfile{longsolutions}

\section{Questions}
\begin{multicols}{3}
    \begin{question}{$\int\sqrt{4-x}\mathrm{d}x$}
            \begin{shortsolution}
                $-\frac{2}{3}(4-x)^{\frac{3}{2}}+C$
            \end{shortsolution}
            \begin{longsolution}
                In order to do this, we must first make a substitution\ldots
            \end{longsolution}
    \end{question}
    \begin{question}{$\int(2x-1)^{50}\mathrm{d}x$}
            \begin{shortsolution}
                $\frac{1}{102}(2x-1)^{51}+C$
            \end{shortsolution}
            \begin{longsolution}
                A very long solution| much longer than the shorter solution
            \end{longsolution}
    \end{question}
    \begin{question}{$\int\sqrt[n]{x}\mathrm{d}x$}
            \begin{shortsolution}
                $\frac{n}{n+1}x^{\frac{1+n}{n}}+C$
            \end{shortsolution}
            \begin{longsolution}
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
                Even longer!
            \end{longsolution}
    \end{question}
\end{multicols}

% close solution files
\Closesolutionfile{shortsolutions}
\Closesolutionfile{longsolutions}

\end{document}
share|improve this answer
    
Perfect =) I can not believe how awesome this is. I am writing a book on integration, and this will be extremely helpful. Now the only thing is how to making it work across several sections. Eg the numbers reset after each section. Section 1 Q1 Q2 Q3 Section 2 Q1 Q2 Q2 ... ... Answers short section 1 Q1 Q2 Q3 section 2 Q1 Q2 Q3. Will give you an bounty, when I can. To show how good of an answer this is =) –  N3buchadnezzar Feb 24 '12 at 22:16
    
@N3buchadnezzar glad you like it. To make it work across several sections, use ...\thesection\thequestion... in all of the hyperlinks and hypertargets. Oh, and don't forget to upvote my answer :) –  cmhughes Feb 24 '12 at 22:26
    
Will add bounty when I can. I tried doing that, bellow is the result i.imgur.com/7Tgo5.png . The problem here is simply, that the numeration gets to narrow once the problem reach into the 10ens and 100dreds. Etc 122 looks messy, and takes up a tad too much space =( I therefore wanted to completely reset the numbers. And also add something to split them at the long asnd short answer section. Something like Answers section 1 ... section 2 ... and so on. Not sure if this is possible though –  N3buchadnezzar Feb 24 '12 at 22:50
    
I updated with my progress =) But the reverse hyperlinks seems to break when I input files between the questions and solutions. Since this is a book, all the questions are stored in one file, then inserted into the master document. While the solutions are inserted later. Almost everything works, except the reverse links. Any suggestions? =) Cheers –  N3buchadnezzar Feb 25 '12 at 13:45
    
@N3buchadnezzar see my edit –  cmhughes Feb 25 '12 at 16:52
show 12 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.