3

Use of task environment inside my moquestion environment (as below) changes the item-labelling (1., 2., 3., etc) of the solution. In the mymosolution environment, the item-labelling is replaced by (D) (resumed from the task environment) instead of 1. Please provide me a way to make the correct item-labelling of the solution as 1., 2., 3., etc., provided the hyper-linking (one-one linking) between each question and the corresponding answer is preserved.

\documentclass{article}
\usepackage[paperwidth=6in, paperheight=9in]{geometry}
\usepackage{answers}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{tasks}
\settasks{item-indent=8.75mm, column-sep=3mm, after-skip=1mm, label-format=\bfseries, counter-format=(tsk[A]), label-offset=1em, label-width=1.25em}

\hypersetup{colorlinks=true,
linkcolor=blue}

\Opensolutionfile{mymosolutions}
\Newassociation{mymosolution}{mymoSoln}{mymosolutions}
\newcounter{moquestion}
\newenvironment{moquestion}{%
\refstepcounter{moquestion}%
     \hypertarget{moquestion:{\themoquestion}}{}%
     \Writetofile{mymosolutions}{\protect\hypertarget{msoln:\themoquestion}{}}%
     \begin{enumerate}[leftmargin=*,label=\bfseries\protect\hyperlink{msoln:\themoquestion}{\themoquestion.},ref=\themoquestion]
     \item%
    }%
    {%
    \end{enumerate}}

\begin{document}

\section*{Questions}
\begin{moquestion}
a
\begin{tasks}[](4)
\task f.
\task g.
\task h.
\task i.
\end{tasks}
\begin{mymosolution}
b\end{mymosolution}
\end{moquestion}

\begin{moquestion}
c
\begin{mymosolution}
d
\end{mymosolution}
\end{moquestion}


\Closesolutionfile{mymosolutions}
\renewenvironment{mymoSoln}[1]{%
     \begin{enumerate}    [leftmargin=*,label=\bfseries\protect\hyperlink{moquestion:#1}{#1.}]
     \item%
}%
{%
    \end{enumerate}}

\clearpage
\section*{Answers!}
\IfFileExists{mymosolutions.tex}{\input{mymosolutions.tex}}{}
\end{document} 

The output is shown below.

enter image description here

enter image description here

1

I found a solution with your MWE (I'm writing a new answer because it's different from the previous one which could be useful for someone else).

I didn't understand why the task environment messes up the reference even if the moquestion counter is correct (you can display it before/after the answer using \themoquestion), but since you print your answers at the end, you could move the \ begin{mymosolution} ... \end{mymosolution} environment before the task one and it works, with the correct indentation.

\documentclass{article}
\usepackage[paperwidth=6in, paperheight=9in]{geometry}
\usepackage{answers}
\usepackage{enumitem}
\usepackage{hyperref}
\usepackage{tasks}
\settasks{item-indent=8.75mm, column-sep=3mm, after-skip=1mm, label-format=\bfseries, counter-format=(tsk[A]), label-offset=1em, label-width=1.25em}

\hypersetup{colorlinks=true,
    linkcolor=blue}

\Opensolutionfile{mymosolutions}
\Newassociation{mymosolution}{mymoSoln}{mymosolutions}
\newcounter{moquestion}
\newenvironment{moquestion}{%
    \refstepcounter{moquestion}%
    \hypertarget{moquestion:{\themoquestion}}{}%
    \Writetofile{mymosolutions}{\protect\hypertarget{msoln:\themoquestion}{}}%
    \begin{enumerate}[leftmargin=*,label=\bfseries\protect\hyperlink{msoln:\themoquestion}{\themoquestion.},ref=\themoquestion]
        \item%
    }%
    {%
\end{enumerate}}

\usepackage{mwe}% for testing purpose only

\begin{document}

    \section*{Questions}
    \begin{moquestion}
        a \blindtext
        \begin{mymosolution}
        b \blindtext
        \end{mymosolution}
        \begin{tasks}[](4)
            \task f.
            \task g.
            \task h.
            \task i.
        \end{tasks}
    \end{moquestion}

    \begin{moquestion}
        c \blindtext
        \begin{mymosolution}
            d \blindtext
        \end{mymosolution}
    \end{moquestion}

    \Closesolutionfile{mymosolutions}
    \renewenvironment{mymoSoln}[1]{%
        \begin{enumerate}    [leftmargin=*,label=\bfseries\protect\hyperlink{moquestion:#1}{#1.}]
            \item%
        }%
        {%
    \end{enumerate}}

    \clearpage
    \section*{Answers!}
    \IfFileExists{mymosolutions.tex}{\input{mymosolutions.tex}}{}
\end{document} 

enter image description here enter image description here

| improve this answer | |
1

Maybe a solution with exercise package could be acceptable, even though I can't get your indentation when the question text is longer than one line.

However, it's very simple to get the one-to-one link, using label={\the\value{Exercise}} as exercise option and ref=\ExerciseLabel as answer option.

In the following example, if you click on the exercise number, you'll get the answer and vice-versa.

Please note that, with Adobe Acrobat Reader DC (I don't know if it's the same with other readers), you have to enlarge the document to see the links working (at least at 60%, since the exercises in the example have very few text lines).

\documentclass{article}
\usepackage[paperwidth=6in, paperheight=9in]{geometry}
\usepackage{calc}
\usepackage{tasks}
\newlength{\mylen}
\setlength{\mylen}{\widthof{9.~}}% if the exercises are more than 9, set 99. If more than 99, set 999, and so on.
\settasks{item-indent=\mylen, column-sep=3mm, after-skip=-1ex,
    label-format=\bfseries, counter-format=(tsk[A]), label-offset=1em, label-width=2.5em}
\usepackage{hyperref}% you must load it before the exercise package
\hypersetup{colorlinks=true,
    linkcolor=blue}
\usepackage[answerdelayed]{exercise}
\renewcommand{\ExerciseListHeader}{\textbf{\color{blue}\refAnswer{\ExerciseLabel}.}~}
\renewcommand{\AnswerListHeader}{\textbf{\color{blue}\ExerciseHeaderNB.}~}
\newcommand{\myexer}{\Exercise[label={\the\value{Exercise}}]\setlength{\parskip}{1em}}
\newcommand{\myansw}{\Answer[ref=\ExerciseLabel]}

\begin{document}
    \section*{Questions}
    \begin{ExerciseList}
        \myexer a
        \begin{tasks}[](4)
            \task f.
            \task g.
            \task h.
            \task i.
        \end{tasks}
        \myansw b 
        \myexer c 
        \myansw d 
        \myexer Another exercise 
        \myansw Another answer
        \myexer Another exercise again
        \myansw Another answer again
    \end{ExerciseList}  

    \clearpage
    \section*{Answers!}\vspace{-1em}
    \shipoutAnswer
\end{document} 

enter image description here enter image description here

| improve this answer | |
  • One main point missing here is the hyper-linking each question to the corresponding answer and vice-versa. I wanted to make clickable question numbers to reach to the corresponding answers and also clickable answer numbers to reach back to the corresponding questions. When the questions and answers are many pages, say 400 pages away, it is better to link one to one (one-one correspondence). I have been looking for that. – Tammo Dec 18 '17 at 13:01
  • It works in a way. Thanks for the answer. But, I still have a problem, may be because I am not an expert. I tried but I could't find a way to correctly one-one link if there are two or more ExerciseList environments or setcounter Exercise to 0 at some point. Again, it would be great if a customisation of indentation (as you mentioned) when a question or an answer has two or more sentences is found. I tried the \Exeleftmargin command. But, it works only for the task listings. @CarLaTeX – Tammo Dec 19 '17 at 2:11
  • @Tammo Are your two lists contained in different sections? Otherwise how can it be possible to distinguish exercise 1 of the first list from exercise 1 of the second list? – CarLaTeX Dec 19 '17 at 5:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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