# How to change the appearance of \ref depending on where it's called relative to the \label

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

\documentclass{article}

{0pt}{3.5ex plus 1ex minus .2ex}{0ex plus .2ex}

\usepackage{enumitem}

format=\normalfont\large\bfseries,%
itemsep=1.5ex plus 1ex minus .2ex,%
after=\vspace{0.5ex}}

\begin{document}

\end{document}


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?

• Possible duplicate of: tex.stackexchange.com/questions/94318/… – TheExplosiveSheep Oct 25 '14 at 11:44
• @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 '14 at 13:19
• You might want to take a look at tex.stackexchange.com/questions/187105/… – John Kormylo Oct 25 '14 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. – John Kormylo Oct 26 '14 at 18:46

\documentclass{article}

{0pt}{3.5ex plus 1ex minus .2ex}{0ex plus .2ex}

\usepackage{enumitem}

format=\normalfont\large\bfseries,%
itemsep=1.5ex plus 1ex minus .2ex,%
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}}%
\endgroup}}
\makeatother

\begin{document}