3

For a Question bank, which is a pdf-only CC edition, I defined hyperlinks and hypertargets as explained in Adding another answer hyperlinked to the question itself.

To typeset more chapters as mentioned in the comments of this answer I tried adding \thechapter\thesection\thequestion. I was successful in linking Problems to Shortsolutions and Longsolutions. But reverse linking is failing.

Can somebody help me, I am posting my MWE.

\documentclass{memoir}

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

\usepackage{pgffor}                            




\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}[chapter]
\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}

\chapter{Questions and Answers}


%\Opensolutionfile{ans}[\jobname ans\thechapter]


\Opensolutionfile{shortsolutions}[\jobname key\thechapter]
\Opensolutionfile{longsolutions}[\jobname sol\thechapter]

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






\chapter{Two and Second Similar One}



%\Opensolutionfile{ans}[\jobname ans\thechapter]


\Opensolutionfile{shortsolutions}[\jobname key\thechapter]
\Opensolutionfile{longsolutions}[\jobname sol\thechapter]

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


\chapter{Answers to the Problems}
% 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}}{}




 \foreach \i in {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...,15} {%
    \edef\FileName{\jobname key\i}%     The % here are necessary to eliminate any
    \IfFileExists{\FileName}{%  spurious spaces that may get inserted
        \input{\FileName}%       at these points
    }
 }






\clearpage
\chapter{Solutions}
% 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}}{}

%Input all Solutions files as they appear Chapter-wise properly hyperlinked. 

 \foreach \i in {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...,15} {%
    \edef\FileName{\jobname sol\i}%     The % here are necessary to eliminate any
    \IfFileExists{\FileName}{%  spurious spaces that may get inserted
        \input{\FileName}%       at these points
    }
 }


\end{document}
  • This looks like a very long MWE. – Matsmath May 05 '16 at 07:35
  • Just to show links using two chapters and 3 problems each. Please bear with me. – Rama Krishna Majety May 05 '16 at 08:17
  • Well, this compiles for me just fine and I can jump back-and-forth between questions/solutions/long solutions. Are you sure your pdf viewer supports hyperlinks? I am using Adobe. – Matsmath May 05 '16 at 08:22
  • It was compiling, compilation was not a problem. Please Test the links of Shortsolutions of second chapter they are pointing towards Problems of First Chapter, they should point to problems of second chapter instead. Thanks for the help. I am also using Adobe reader but in CentOS. My question is how to point solutions (Short and Long) to questions of corresponding chapter. – Rama Krishna Majety May 05 '16 at 15:56
  • All right, my mistake. I just realized that you have the exact same stuff on two different pages. That's extremely confusing. You have duplicate labels everywhere, so, contrary what you might claim, even the forward linking is broken (just you don't realize it as all solutions are on the same page whereas the questions are on two different pages). So, your problem is -- as shown in the warnings, and in your pdf -- destination with same identifier has already been used". – Matsmath May 05 '16 at 16:16
  • Thanks for the reply. How to specify targets differently by adding chapter,section and question details. The counter question is numbered within chapter. – Rama Krishna Majety May 05 '16 at 16:22
  • Maybe of some help? http://tex.stackexchange.com/questions/306260/hyperref-and-answers-package-how-to-put-the-link-which-goes-back-to-the-questio – Steven B. Segletes May 06 '16 at 02:15

0 Answers0