2

I have defined a \task sectioning command and a subtasks-list as in this MWE:

\documentclass{article}

\usepackage[hidelinks]{hyperref}

\usepackage[loadonly, explicit]{titlesec}

\titleclass{\task}[1]{straight}
\newcounter{task}
\titleformat{\task}
    [hang]{\normalfont\Large\bfseries}{Task \thetask:}{1ex}{#1}
\titlespacing*{\task}
    {0pt}{3.5ex plus 1ex minus .2ex}{0ex plus .2ex}

\usepackage{enumitem}

\newlist{subtasks}{enumerate}{1}
\setlist[subtasks]{label=\alph*),%
    ref=\thetask\,\alph*),%
    format=\normalfont\large\bfseries,%
    itemsep=1.5ex plus 1ex minus .2ex,%
    before=\newcommand{\subtask}{\item},%
    after=\vspace{0.5ex}}

\begin{document}


\task{first task}
\begin{subtasks}

    \subtask\label{some_subtask}
    Some subtask

    \subtask
    I am referencing \ref{some_subtask}.

\end{subtasks}

\task{another task}
I am referencing \ref{some_subtask} again.


\end{document}

The output of the MWE above

What I would like to do is to have the \ref appear differently depending on if it is called in the same \task as the \label (just as "a)") or not (as "1 a)" as above). It should also be compatible to hyperref.

Is this possible?

4
  • Possible duplicate of: tex.stackexchange.com/questions/94318/… Oct 25, 2014 at 11:44
  • 1
    @TheExplosiveSheep, I don't believe it is a duplicate. The OP wants to change the output depending if the \ref is inside/outside the environment. Only changing the format of the labels is not enough.
    – Sigur
    Oct 25, 2014 at 13:19
  • You might want to take a look at tex.stackexchange.com/questions/187105/… Oct 25, 2014 at 14:32
  • I thought I could use xstring to decompose 1\,a), but even something as simple as \edef\temp{\ref{some_subtask}} causes an error. This is not normal behavior! (later) Aha! hyperref is the culprit. Even ref* doesn't work. Oct 26, 2014 at 18:46

1 Answer 1

5

refsub

\documentclass{article}

\usepackage[hidelinks]{hyperref}

\usepackage[loadonly, explicit]{titlesec}

\titleclass{\task}[1]{straight}
\newcounter{task}
\titleformat{\task}
    [hang]{\normalfont\Large\bfseries}{Task \thetask:}{1ex}{#1}
\titlespacing*{\task}
    {0pt}{3.5ex plus 1ex minus .2ex}{0ex plus .2ex}

\usepackage{enumitem}

\newlist{subtasks}{enumerate}{1}
\setlist[subtasks]{label=\alph*),%
    ref=\thetask.\alph*,%
    format=\normalfont\large\bfseries,%
    itemsep=1.5ex plus 1ex minus .2ex,%
    before=\newcommand{\subtask}{\item},%
    after=\vspace{0.5ex}}

\usepackage{xstring}
\makeatletter
\newcommand{\refsub}[1]% #1 = label name for subtasks
{\@ifundefined{r@#1}{??}{\begingroup%
  \edef\temp{\expandafter\detokenize\getrefnumber{#1}}%
  \StrCut{\temp}{.}\temptask\tempsub%
  \edef\templink{\getrefbykeydefault{#1}{anchor}{}}%
  \if\thetask\temptask\hyperlink{\templink}{\tempsub)}%
  \else\hyperlink{\templink}{\temptask\,\tempsub)}\fi
\endgroup}}
\makeatother

\begin{document}

\task{first task}
\begin{subtasks}

    \subtask\label{some_subtask}
    Some subtask

    \subtask
    I am referencing \refsub{some_subtask}.

\end{subtasks}

\task{another task}
I am referencing \refsub{some_subtask} again.

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .