0

I am using enumerate environment to create a list of answers on tasks in my book where item label is a number of a task. I want to put a page number where the task is located immediately under label item.

This is what I have now:

\documentclass[a4paper,14pt,oneside]{extbook}

\usepackage{enumitem}
\usepackage[many]{tcolorbox}
\usepackage{hyperref}
\hypersetup{unicode = true,colorlinks=true,linkcolor={red},pageanchor = false}

% Defining custom tcolorbox environment
\newtcolorbox[auto counter,number within=chapter]{task}[1][]{%
    title=\thetcbcounter,#1
}

\begin{document}
    \begin{task}[label={task0.1}]
        Task condition
    \end{task}

    \begin{task}[label={task0.2}]
        Task condition
    \end{task}

    \begin{task}[label={task0.3}]
        Task condition
    \end{task}

    % List with tasks answers
    \begin{enumerate}
        \item [\textbf{0.1}\\{\small p.\pageref{task0.1}}] Task 0.1 answer
        \item [\textbf{0.2}\\{\small p.\pageref{task0.2}}] Task 0.2 answer
        \item [\textbf{0.3}\\{\small p.\pageref{task0.3}}] Task 0.3 answer
    \end{enumerate}

\end{document}

And how it looks:

enter image description here

As you can see double backslash \\ symbol and also \newline command do not work here.

How to move p.1 on the next line under task number?

And one more thing. Is there a way to get tcolobox "task" counter by it's label in order not to write task number explicitly?

2

To start with I changed your labels since it is never a good idea to have the number in them. Then to get the number use \ref{label}. To get the page mumber below the task number you can use \raisebox with appropriate settings. Here I just lowered it 0.8em and set the height and depth both to 0pt. Without suppressing the vertical space there will be a space between the first and the second row of the answer. Finally I set the page number in a \rlap command to not occupy and horizontal space. So actually the page number should not occupy any space at all.

\documentclass[a4paper,14pt,oneside]{extbook}

\usepackage{calc}
\usepackage{enumitem}
\usepackage[many]{tcolorbox}
\usepackage{hyperref}
\hypersetup{unicode = true,colorlinks=true,linkcolor={red},pageanchor = false}
% Defining custom tcolorbox environment
\newtcolorbox[auto counter,number within=chapter]{task}[1][]{%
    title=\thetcbcounter,#1
}
\begin{document}
    \begin{task}[label={easy}]
        Task condition
    \end{task}
    \begin{task}[label={hard}]
        Task condition
    \end{task}
    \begin{task}[label={impossible}]
        Task condition
    \end{task}
    % List with tasks answers
    \newcommand\specialitemlabel[1]{\raisebox{-0.8em}[0pt][0pt]{\rlap{\small p.\pageref{#1}}}\textbf{\ref{#1}}}
    \begin{enumerate}
        \item [\specialitemlabel{easy}] Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer  Task 0.1 answer
        \item [\specialitemlabel{hard}] Task 0.2 answer
        \item [\specialitemlabel{impossible}] Task 0.3 answer
    \end{enumerate}
\end{document}

enter image description here

2

To move p.1 onto the next line you can simply use \setlist[description]{style = multiline}.

Code:

\documentclass[a4paper,14pt,oneside]{extbook}
\usepackage{enumitem}

\setlist[description]{style = multiline}

\usepackage[many]{tcolorbox}
\usepackage{hyperref}
\hypersetup{unicode = true,colorlinks=true,linkcolor={red},pageanchor = false}
% Defining custom tcolorbox environment
\newtcolorbox[auto counter,number within=chapter]{task}[1][]{%
    title=\thetcbcounter,#1
}

\begin{document}
    \begin{task}[label={task0.1}]
        Task condition
    \end{task}

    \begin{task}[label={task0.2}]
        Task condition
    \end{task}

    \begin{task}[label={task0.3}]
        Task condition
    \end{task}

    % List with tasks answers
    \begin{description}
        \item [\textbf{0.1}\\{\small p.\pageref{task0.1}}] Task 0.1 answer
        \vspace{.5cm}
        \item [\textbf{0.2}\\{\small p.\pageref{task0.2}}] Task 0.2 answer
         \vspace{.5cm}
        \item [\textbf{0.3}\\{\small p.\pageref{task0.3}}] Task 0.3 answer
    \end{description}

\end{document}

This yields:

enter image description here

1

You could create a parbox and use \widthof provided by calc in order to achieve that:

\documentclass[a4paper,14pt,oneside]{extbook}

\usepackage{calc}
\usepackage{enumitem}
\usepackage[many]{tcolorbox}
\usepackage{hyperref}
\hypersetup{unicode = true,colorlinks=true,linkcolor={red},pageanchor = false}

% Defining custom tcolorbox environment
\newtcolorbox[auto counter,number within=chapter]{task}[1][]{%
    title=\thetcbcounter,#1
}

\newcommand{\myitem}[2]{%
    \item[\parbox{\widthof{\textbf{#1}\\{\small p.\pageref{#2}}}}{\textbf{#1}\\{\small p.\pageref{#2}}}]
}

\begin{document}
    \begin{task}[label={task0.1}]
        Task condition
    \end{task}

    \begin{task}[label={task0.2}]
        Task condition
    \end{task}

    \begin{task}[label={task0.3}]
        Task condition
    \end{task}

    % List with tasks answers
    \begin{enumerate}
        \myitem{0.1}{task0.1} Task 0.1 answer
        \myitem{0.2}{task0.2} Task 0.2 answer
        \myitem{0.3}{task0.3} Task 0.3 answer
    \end{enumerate}

\end{document}

I don't understand your second question about the "task". Could you elaborate?

  • Thank you, it is what I was looking for! Now about second question: \myitem takes two arguments: task number and task label (to get page number). It is not super necessary but is there a way to get task number by task label so it would look like this: \myitem{task0.1} (and it automatically get task number as a tcolobox counter value and page number). – CMTV Nov 12 '16 at 15:25
  • 1
    try \newcommand{\myitem}[1]{% \item[ \refstepcounter{enumi} \parbox{\widthof{\textbf{0.\theenumi}\\ {\small p.\pageref{#1}}}}{\textbf{0.\theenumi}\\{\small p.~\pageref{#1}}} ]} and use \myitem{task0.1} . – Zarko Nov 12 '16 at 15:43

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.