4

I've looked for an answer to my LaTeX question, but even though there are some that are similar, none addresses my issue. I'm trying to label and cross-reference the first and last items of a set to make reference to the entire set. For example, imagine we have the following case:

(1) a. This is sentence one.

b. This is sentence two.

c. This is sentence three.

I'm trying to write in text something like "... if we look at examples (1a-c), we find that..."

The only way I know how to cross-reference in LaTeX is this way:

\documentclass{article}
\begin{document}
    \begin{enumerate}
        \item \begin{enumerate}
            \item One \label{ex1}
            \item Two \label{ex2}
            \item Three \label{ex3}
            \end{enumerate}
    \end{enumerate}

    We can see in examples (\ref{ex1}-\ref{ex3}) that...

\end{document}

The code above produces the following output: enter image description here

How can I change my code so that it shows as "...in examples (1a-c) we find that..." I don't want the number 1 to be repeated again. The way I want the output is a convention in my field, and the way it shows on LaTeX is considered "awkward." That's why I would like to find a way that works while still being able to cross-reference my labeled items.

  • 1
    Welcome to TeX.SX! What should happen if, for example, there were more labels and you wanted to reference items a,b,c, e,f,i? Should the label be (1a-c,e-f,i) or (1a-c,1e-f,1i) , or (1a-c), (1e-f), (1i) or ... ? What about mixed labels like (1a-c,2e-f)...? – Andrew Dec 17 '18 at 3:15
  • Good question! If we're inserting a comma to indicate a break in the references, I'd like to begin again with the number of the examples, that is, (1a-c, 1e-f, 1i)--your second option. – Ernesto Dec 17 '18 at 3:34
  • Is it essential to place parentheses around the (group of) cross-references? In my opinion, they do not aid the readability. And, would you like an "and" conjunction before the final list entry, i.e., 1a-c, 1e-g, and 1i rather than just 1a-c, 1e-f, 1i? – Mico Dec 17 '18 at 5:36
  • 1
    Up until now I've been adding the parentheses around the (\ref{label}) manually. If there's an easier way to do it in a way that I have to add the parentheses manually, that's fine. And I'd like the "and" prior to the last item. But if I can find a way to call, for example, 1a-c, I would be happy just repeating the command for the rest of the items, e.g. (\ref{label_a-c}, \ref{label_e-f}, \ref{label{i}) – Ernesto Dec 17 '18 at 5:53
6

The cross-referencing commands of the cleveref package -- specifically, \cref and \crefrange -- may be your friend. Note that in the solution shown below, there are no parentheses around the cross-referenced items. I don't believe that these parentheses are either necessary or helpful.

enter image description here

The \crefrange command, which takes two arguments, is designed for cross-references to a contiguous range of items. The \cref command takes one argument, a comma-separated list of labels. The \cref command automatically sorts the contents of its argument; it's not necessary to enter them in ascending order.

Optionally, you could load the hyperref package. Doing so will make the cross-references generated by \cref and \crefrange into hyperlinks to the associated "targets". If you load the hyperref package, be sure to load it immediately before cleveref. Basically, hyperref and cleveref should be the last two packages loaded in the preamble.

\documentclass{article}
\usepackage[inline]{enumitem} 
\usepackage[colorlinks]{hyperref} % optional
\prepackage{cleveref}
\crefname{enumi}{example}{examples}
\newcommand{\crefrangeconjunction}{--}
\newcommand{\creflastconjunction}{, and } % optional, for "Oxford comma"

\begin{document}
\begin{enumerate}
\item \begin{enumerate*}  % inline list saves space
  \item One   \label{ex1}    \item Two   \label{ex2}    \item Three \label{ex3}    
  \item Four  \label{ex4}    \item Five  \label{ex5}    \item Six   \label{ex6}
  \item Seven \label{ex7}    \item Eight \label{ex8}    \item Nine  \label{ex9}   
  \end{enumerate*}
\end{enumerate}

We can see in \crefrange{ex1}{ex5} that \dots

We can see in \cref{ex9,ex3,ex7,ex5,ex6,ex1,ex2} that \dots
\end{document}


Addendum: The cleveref package provides some very powerful utility macros. For instance, as explained in section of package's user guide, one can strip the common prefix -- here, "1" -- from ranges of cross-references.

enter image description here

\documentclass{article}
\usepackage[inline]{enumitem} 
\usepackage[colorlinks]{hyperref} 

\usepackage{cleveref}
\crefname{enumi}{example}{examples}
%\newcommand{\crefrangeconjunction}{--}
\newcommand{\creflastconjunction}{, and } % optional, for "Oxford comma"
\crefrangelabelformat{enumi}{#3#1#4--#5\crefstripprefix{#1}{#2}#6}

\begin{document}
\begin{enumerate}
\item \begin{enumerate*}  % inline list saves space
  \item One   \label{ex1}   \item Two   \label{ex2}   \item Three \label{ex3}   
  \item Four  \label{ex4}   \item Five  \label{ex5}   \item Six   \label{ex6}
  \item Seven \label{ex7}   \item Eight \label{ex8}   \item Nine  \label{ex9}
  \end{enumerate*}
\end{enumerate}

We can see in \crefrange{ex1}{ex5} that \dots

We can see in \cref{ex9,ex3,ex7,ex5,ex6,ex1,ex2} that \dots
\end{document}
  • 1
    Thanks for your answer! I was wondering if there's any way to implement this code with the hyperref package, either by linking to the number "1" or the specific letter (i.e. "a" or "e" in the first example). If not, is there another package that works with the cleveref package to create links to the examples? – Ernesto Dec 17 '18 at 18:00
  • 1
    @Ernesto - to make the cross-references into hyperlinks, all you need to do is load the hyperref package before cleveref. I will post an update to my answer to show how that's done. As you'll notice, "1a" is a hyperlink to item 1.(a), etc. – Mico Dec 17 '18 at 18:32
  • 1
    @Ernesto - I've gone ahead and posted a second example, this time to demonstrate that the macros of the cleveref package can be programmed to "strip" a common prefix from ranges to composite labels. – Mico Dec 17 '18 at 20:36
  • the first time I tried adding the hyperref package before the enumitem package. That causes an error. The packages must be listed in the other order for it to work. Thanks! – Ernesto Dec 17 '18 at 21:52
  • 1
    @ernesto - My apologies for not being sufficiently clear earlier: hyperref has to be loaded immediately before cleveref, not just anywhere before. – Mico Dec 17 '18 at 22:27
3

With enumitem you can set separate label and ref values. Below I've used that with an additional \label as part of the higher level enumerate to set three separate elements for \reference:

enter image description here

\documentclass{article}

\usepackage{enumitem}

\begin{document}

\begin{enumerate}[label={\arabic*.},ref={\arabic*}]
  \item \label{ex0}
    \begin{enumerate}[label={(\alph*)},ref={\alph*}]
      \item One \label{ex1}
      \item Two \label{ex2}
      \item Three \label{ex3}
    \end{enumerate}
\end{enumerate}

We can see in examples (\ref{ex0}\ref{ex1}-\ref{ex3}) that...

\end{document}
  • Thanks for your answer! I was wondering if there's any way to implement this code with the hyperref package, either by linking to the number "1" or the specific letter (i.e. "a" or "c" in your example). If not, is there another package that works with the enumitem package to create links to the examples? – Ernesto Dec 17 '18 at 18:04
  • 2
    @Ernesto: This should work seamlessly with hyperref, since it's dealing with regular \label-\ref. You will have separate links to 1 and a though. Are you requesting that 1a link to only one \ref, not two? – Werner Dec 17 '18 at 18:38
  • yes, I just realized that it does work. The first time I tried it, I loaded the hyperref package before the enumitem package, and that caused an error. But if I list the former after the latter in the preamble, it works just fine. Thanks! – Ernesto Dec 17 '18 at 21:49
  • 1
    @Ernesto: Yes. See Which packages should be loaded after hyperref instead of before? There are only a few packages that require special treatment with hyperref. In general, hyperref loading should be delayed until the very end, since it interacts with so many other packages. – Werner Dec 17 '18 at 22:05
  • 1
    @Ernesto: Use (\hyperref[ex1]{\ref*{ex0}\ref*{ex1}}-\ref{ex3}). \ref* under hyperref removes the hyperlink, so we use that inside a \hyperref[<label>]{<stuff>}. – Werner Dec 17 '18 at 22:21

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.