7

Using the enumitem package, I'm trying to cross-reference an item in an enumerate list.

  1. When I use \ref to cross-reference an item, I get the same, satisfactory, result no matter whether I create the enumerate list directly in the body of the source, on the one hand, or use a \newlist / \setlist definition of the environment in the preamble. But why do I not get the same results in those two situations when I use \cref instead of \ref?

  2. Why don't the \cref results work in either situation?

This example, showing all 4 combinations of \ref, \cref, preamble-defined enumerate, and directly-defined enumerate, uses just the * versions of the commands; but similar results occur with the unstarred versions.

Situation — \newlist \setlist with \ref* :

\documentclass[12pt]{memoir}
\usepackage{enumitem}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}

\newlist{romcases}{enumerate}{3}
\setlist[romcases,1]{%
    label=\sffamily{Case (\roman*):},
    ref=\normalfont{Case (\roman*)},
    wide,itemsep=0pt,topsep=0pt
}    
\crefname{romcases}{Case}{Case}
\Crefname{romcases}{Case}{Case}

\begin{document}

We distinguish two cases.

\begin{romcases}

\item \label{case:abequal} 
$a = b$. Now we see that\dots 

\item\label{case:abnotequal}
 $a \neq b$. In contrast to \ref*{case:abequal}, in this case \dots 

\end{romcases}

\bigskip

\textbf{\emph{NOTE:} Output as expected.}

\end{document}

newlist & \ref*

Situation — direct \enumeratewith \ref* :

\documentclass[12pt]{memoir}
\usepackage{enumitem}   
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}

\begin{document}

We distinguish two cases.

\begin{enumerate}[%
  label=\textsf{Case (\alph*):},ref=\normalfont{Case (\roman*)},
  wide,itemsep=0pt,topsep=0pt]

\item \label{case:abequal} 
$a = b$.

Now we see that\dots 

\item\label{case:abnotequal}
 $a \neq b$. 
 In contrast to \ref*{case:abequal}, in this case \dots 

\end{enumerate}

\bigskip

\textbf{\emph{NOTE:} Output \emph{OK}.}

\end{document}

direct enumerate & \ref*

Situation — \newlist \setlist with \cref* :

\documentclass[12pt]{memoir}
\usepackage{enumitem
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}

\newlist{romcases}{enumerate}{3}
\setlist[romcases,1]{%
    label=\sffamily{Case (\roman*):},
    ref=\normalfont{Case (\roman*)},
    wide,itemsep=0pt,topsep=0pt
}    
\crefname{romcases}{Case}{Case}
\Crefname{romcases}{Case}{Case}

\begin{document}

We distinguish two cases.

\begin{romcases}

\item \label{case:abequal} 
$a = b$. Now we see that\dots 

\item\label{case:abnotequal}
 $a \neq b$.  In contrast to \cref*{case:abequal}, in this case \dots 

\end{romcases}

\bigskip

\textbf{\emph{NOTE:} Output \emph{NOT} as expected.}

\end{document}

newlist & \cref*

Situation — direct \enumeratewith \cref* :

\documentclass[12pt]{memoir}
\usepackage{enumitem}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}

\begin{document}

We distinguish two cases.

\begin{enumerate}[%
  label=\textsf{Case (\alph*):},ref=\normalfont{Case (\roman*)},
  wide,itemsep=0pt,topsep=0pt]

\item \label{case:abequal} 
$a = b$. Now we see that\dots 

\item\label{case:abnotequal}
 $a \neq b$.  In contrast to \cref*{case:abequal}, in this case \dots 

\end{enumerate}

\bigskip

\textbf{\emph{NOTE:} Output \emph{NOT} as expected.}

\end{document}

direct enumerate & \cref*

2
  • 1
    In the romcases case, the level-1 counter variable is named romcasesi, not romcases. Correct the first argument of \crefname and \Crefname and you'll be fine.
    – Mico
    Commented Dec 8, 2016 at 17:53
  • In your fourth example, the counter that's used for cross-referencing level-1 enumerated items is called enumi. The cleveref package prefixes the string "Item" to objects associated with the enumi counter. Because you also have the instruction ref=\normalfont{Case (\roman*)}, it is actually fully expected to get "Item Case (i)" as the output of \cref{case:abequal}. If you want to keep ref=\normalfont{Case (\roman*)} and wish to use \cref, you should run \crefname{enumi}{}{} to override the default.
    – Mico
    Commented Dec 8, 2016 at 18:22

1 Answer 1

8

When you define a new enumerated list environment called romcases via a \newlist instruction, the counter associated with level-1 items is called romcasesi. Similarly, the counter associated with level-2 romcases items is called romcasesii, etc.

If you're going to use the macros of the cleveref package to create "clever" cross-references, I suggest you specify just ref=\roman* in argument of \setlist[romcases,1]; note: no string label "Case", and no parentheses. Then, use \creflabelformat, \crefname and \Crefname directives to specify how \cref and friends should format and typeset the cross-references. This approach will give you maximum flexibility -- which will come in handy when you start including multiple objects in a single \cref statement and expect cleveref to handle the sorting of arguments automatically.

enter image description here

\documentclass[12pt]{memoir}
\usepackage{enumitem}
\usepackage[colorlinks=true,linkcolor=red]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}
\newlist{romcases}{enumerate}{1}
\setlist[romcases,1]{
    label={\sffamily Case (\roman*):},
    ref=\roman*, % if to be used with \cref, don't provide label string or parentheses
    wide,itemsep=0pt,topsep=0pt} 
\creflabelformat{romcasesi}{#2\textup{(#1)}#3} % note presence of parentheses
\crefname{romcasesi}{Case}{Cases} % singular and plural forms of text labels
\Crefname{romcasesi}{Case}{Cases}

\begin{document}
We distinguish two cases:
\begin{romcases}
\item \label{case:abequal} $a = b$. Now we see that\dots 
\item\label{case:abnotequal} $a \neq b$.  In contrast to \cref{case:abequal}, \dots 
\end{romcases}
\end{document}
3
  • I don't understand the comment "if to be used with \cref, don't provide a label string": if I don't include a label string, I get an immediate "Undefined error" when running latex on the source. did you mean, rather, not to provide a ref in the setlist? But then I get output for the link as "Case Case (i)".
    – murray
    Commented Dec 8, 2016 at 20:44
  • @murray - The comment means: If you're going to create cross-references with \cref, don't write ref=\normalfont{Case (\roman*)}, (as you did in your example code), i.e. omit the "Case" label string: ref=(\roman*). (\normalfont` is the default.) \cref provides a label string of its own -- the one specified in the \crefname instruction. I recommend providing just a counter number because it allows \cref to take multiple arguments, which will be sorted and compressed if they're numeric; if you pass general strings, \cref can't provide the sorting and compressing functionality.
    – Mico
    Commented Dec 8, 2016 at 21:07
  • @murray - I've updated my answer slightly. If you're going to use the macros of the cleveref package to create cross-references, I now suggest that you provide a pure (roman) counter for the ref= part in the argument of \setlist. Then, use \creflabelformat{romcasesi}{...} and \crefname{romcasesi}{...}{...} statements. This will give you maximum flexibility.
    – Mico
    Commented Dec 8, 2016 at 22:57

You must log in to answer this question.

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