3

Suppose I have the following code. I want to cross reference the numbers between the 2 lists. Basically, by clicking on number 1 in question section the document should automatically bring you to the number 1 of the answer section.

\documentclass[12pt, a4]{article}
\usepackage{listings}
\usepackage{enumitem}

\begin{document}
\textbf{Questions}
\begin{enumerate}
\item \label{question-01} What colour was Napoleon's white horse?
\end{enumerate}
\textbf{Answers}
\begin{enumerate}%[label= ...]
\item White
\end{enumerate}
\end{document}

Tried with \label and \ref, but the reference creates another number 1, while I want the cross reference to be inside the number of the list.

Maybe I should work on the \label command after \begin{enumerate}.

Hope to have been enough clear.

  • 2
    Cross references are not the same as hyperlinks (things that you can click on). You need hyperref for that. Btw: loading the enumerate package is redundant here as it is overwritten by enumitem. Plus where are your refs in this doc? – daleif Oct 14 '16 at 19:04
3

The following provides an automated way of numbering questions and have it hyperlinked to answers.

enter image description here

\documentclass{article}

\usepackage{hyperref}

\newcounter{questions}% To keep track of the questions environment
\newcounter{answers}% To keep track of the answers environment

\newenvironment{questions}
  {\stepcounter{questions}% New questions environment
   \renewcommand{\theHenumi}{q-\thequestions-\arabic{enumi}}% To make hyperref happy
   \section*{Questions}% Question title
   \begin{enumerate}}
  {\end{enumerate}}
\newcommand{\qitem}{%
  \refstepcounter{enumi}% Step enumeration counter
  \item[{\hyperref[a-\thequestions-\labelenumi]{\labelenumi}}]\label{q-\thequestions-\labelenumi}% Set \item and \label it
  \ignorespaces
}

\newenvironment{answers}
  {\stepcounter{answers}% New answers environment
   \renewcommand{\theHenumi}{a-\theanswers-\arabic{enumi}}% To make hyperref happy
   \section*{Answers}% Answer title
   \begin{enumerate}}
  {\end{enumerate}}
\newcommand{\aitem}{%
  \refstepcounter{enumi}% Step enumeration item
  \item[{\hyperref[q-\theanswers-\labelenumi]{\labelenumi}}]\label{a-\theanswers-\labelenumi}% Set \item and \label it
  \ignorespaces
}


\begin{document}

\begin{questions}
  \qitem What colour was Napoleon's white horse?
\end{questions}

\begin{answers}
  \aitem White
\end{answers}

\end{document}

The questions environment sets up questions, with each question enumerated using \qitem. This sets a label for the question as well as a hyperlink jump from the enumeration to the corresponding answer. An analygous answers environment provides hyperlinks in the opposite direction.

The above should work for multiple sets of Questions and Answers, each sequentially linked to the corresponding set.

  • Great! Is there a way to automatically let the link between Q-A also functioning in the opposite directions? I am reading the question and I want to directly go to the answer while reading the answer I want to have a look back to data contained in the question. – Francesco Bianchi Oct 14 '16 at 19:45
  • 1
    @FrancescoBianchi: That's how it was built... you should try it. You can click on the question number and it jumps to the corresponding answer. Then, clicking on the answer number will jump you to the corresponding question. – Werner Oct 14 '16 at 19:46
  • Exactly what I was looking for. Unfortunately, I am not yet able to produce my own code to satisfy my own ideas. Thanks a lot! – Francesco Bianchi Oct 14 '16 at 19:58
1

To achieve your objective, you need to load the hyperref package; neither the enumitem nor the enumerate package is required. (Aside: do not load both packages simultaneously.) The hyperref package provides the commands hypertarget, to create a "target", and \hyperlink, to create a link to the "target" defined by \hypertarget.

Here's a simple MWE (mininum working example). Note that each \item -- in both enumerate environments -- is associated with both a \hypertarget and a \hyperlink instruction. The "link button" is just a \textbullet; obviously, you free to come up with something fancier. For the simple example at hand, I've chosen to leave the second argument of both \hypertarget directives empty.

\documentclass[12pt,a4paper]{article}
\usepackage[colorlinks,linkcolor=cyan]{hyperref}
\begin{document}

\subsubsection*{Questions}
\begin{enumerate}
\item\hypertarget{q:nap}{}\hyperlink{a:nap}{\textbullet} 
What colour was Napoleon's white horse?
\end{enumerate}

\clearpage  % just to make jumping between the links non-trivial

\subsubsection*{Answers}
\begin{enumerate}
\item\hypertarget{a:nap}{}\hyperlink{q:nap}{\textbullet} 
White.
\end{enumerate}

\end{document}

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.