21

How can I reference multiple items/equations in a single \eqref call? I want, e.g., "In (1,2)..." instead of "In (1) and (2)...". I'd think I could do something like

\begin{enumerate} 
  \item blah \label{it:1} 
  \item blah \label{it:2} 
\end{enumerate}

In \eqref{it:1, it:2}...

but that doesn't work.

  • 2
    In (\ref{it:1}, \ref{it:2}) blah; don't overuse \eqref. – egreg Apr 8 '12 at 17:50
  • @egreg Good idea. Thanks. Why do you say "overuse"? – Quinn Culver Apr 8 '12 at 18:19
  • \eqref is not a general substitute for \ref. – egreg Apr 8 '12 at 19:09
  • @egreg What, exactly, do you mean? Feel free to provide a reference to something I can read that explains what you mean. – Quinn Culver Apr 8 '12 at 20:11
  • 2
    The purpose of \eqref is to print a reference to an equation, with the same format as the equation numbers (and always upright). This format is not, in general, the one to use for other cross references. – egreg Apr 8 '12 at 20:17
16

The command \eqref is meant for printing references to equations in the same format as used for numbering the equations (and always in upright type).

This is not always appropriate for other cross references. For example, items are usually numbered without parentheses and it could be difficult for the reader to interpret the cross reference "In (1, 2)". But this is a matter of opinion and yours is as good as mine.

So (\ref{it:1},~\ref{it:2}) can be the solution when only a few of these cross references are used in the document. If you need them more than a few times, then probably a macro can be useful.

The easiest way is to use LaTeX3 facilities; the macro can be tailored to other formats, if need arises.

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\mref}{m}{\quinn_mref:n {#1}}
\seq_new:N \l_quinn_mref_seq
\cs_new:Npn \quinn_mref:n #1
 {
  \seq_set_split:Nnn \l_quinn_mref_seq { , } { #1 }
  \seq_pop_right:NN \l_quinn_mref_seq \l_tmpa_tl
  ( % print the left parenthesis
  \seq_map_inline:Nn \l_quinn_mref_seq
    { \ref{##1},\nobreakspace } % print the first references
  \exp_args:NV \ref \l_tmpa_tl % print the last or only one
  ) % print the right parenthesis
 }
\ExplSyntaxOff

If you want to ensure that the references are always printed in upright shape, then you can change the first definition into

\NewDocumentCommand{\mref}{m}{\textup{\quinn_mref:n {#1}}}

Complete example

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\mref}{m}{\quinn_mref:n {#1}}
\seq_new:N \l_quinn_mref_seq
\cs_new:Npn \quinn_mref:n #1
 {
  \seq_set_split:Nnn \l_quinn_mref_seq { , } { #1 }
  \seq_pop_right:NN \l_quinn_mref_seq \l_tmpa_tl
  ( % print the left parenthesis
  \seq_map_inline:Nn \l_quinn_mref_seq
    { \ref{##1},\nobreakspace } % print the first references
  \exp_args:NV \ref \l_tmpa_tl % print the last or only one
  ) % print the right parenthesis
 }
\ExplSyntaxOff

\begin{document}
\begin{enumerate}
  \item\label{it:1}  blah
  \item\label{it:2}  blah
\end{enumerate}

In \mref{it:1,it:2} and this \emph{does} work!

In \mref{it:1} there's only one!
\end{document}

enter image description here

  • I know this is an old post, but can I ask, is there a way of doing this so that my LaTeX editor (TeXstudio) recognises this command, and gives me the auto-fill options when I start typing the reference? At the moment, it gives me an unrecognized command warning (but still compiles), and no autofill, and the references don't turn green, as they do with \eqref{equation} for example. (This may not be possible, of course) – Sam T Aug 23 '18 at 17:09
3

For referencing multiple equations (not items, though), consider the suggestions at https://stackoverflow.com/q/3328806 where you can, for example, group multiple (related) equations using subequations and align and then reference either the group of equations or the individual ones.

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.