8

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}
David Carlisle
  • 757,742
N3buchadnezzar
  • 11,348
  • 7
  • 55
  • 114

1 Answers1

9

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}
cmhughes
  • 100,947
  • 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 http://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
  • Ah! You missinterpreted my question! English is so hard! Please look at my edit, most things there are working now. I added another assosiation to be able to have sections for the answers. I wanted to know if there were a more automatic approach to this than mine. Secondly I was wondering, if it was possible to input files between the questions and answers. at the moment the hyoerref breaks when doing so. Like in my edit questions first then answers ^^ Thanks – N3buchadnezzar Feb 25 '12 at 18:24
  • @N3buchadnezzar I still don't really understand :) However, one thing that stands out is that you have tried to open a solutions file within your IntList environment- this could very well be the source of your problems, as it probably won't be global – cmhughes Feb 25 '12 at 19:07
  • @N3buchadnezzar thank you very much for the bounty, you are too kind :) – cmhughes Feb 29 '12 at 17:57
  • Hello! Sorry for bumping this, but the hyperref seems to fail when having multiple sections. Eh, mind me asking a new question or helping me? I feel so silly for not being able to make this work. – N3buchadnezzar Mar 16 '12 at 18:50
  • @N3buchadnezzar probably no need for a new question :) it could be that the hypertargets are being repeated- are you seeing a message in your log file, something like, '... duplicate ignored'. You should be able to fix it by using \thesection infront of each \theproblem- if this doesn't make sense, let me know and I'll post an edit – cmhughes Mar 17 '12 at 04:42
  • Like I said, I did add \thesection to all the links, bu then the numbering system fails. :/ Here is my attempt http://pastebin.com/YFMUNHCw – N3buchadnezzar Mar 17 '12 at 12:38
  • @N3buchadnezzar I'll take a look and post an edit today – cmhughes Mar 17 '12 at 18:42
  • @N3buchadnezzar have been working on your problem- see http://spot.pcc.edu/~chughes/buzzard/ for solution – cmhughes Mar 17 '12 at 23:13
  • Thanks a bunch as usual for your (tremendous!) work, it almost works now. The problem seems to be that I reset the numbering two times during one section. This messes up the numbering. (I have one section for problems, and one for word problems). The backwards linking does not account for this. If I have no subsections your solution works. (I hope you understood this.) subsections breaks your solution – N3buchadnezzar Mar 17 '12 at 23:39
  • @N3buchadnezzar I thought my solution worked with subsections (demoed in the link); I noticed that some of your code was incrementing counters somewhat suspiciously :) You'll need to come up with a way to distinguish between the problems- perhaps a global counter? Just remember that the links & targets need to be accounted in both the questions and the solutions – cmhughes Mar 18 '12 at 00:19
  • I will look more into this tomorrow. I see now your example does use subsections. I just can not see why it fails in my document, nor can I see where I "suspicous incremment counters",but that is a problem for future me I guess =) – N3buchadnezzar Mar 18 '12 at 00:51