44

Simple question: Is there a simple way to get cleveref to use the Oxford comma?

\documentclass{article}
\usepackage[standard]{ntheorem}
\usepackage{cleveref}

\begin{document}

\begin{proposition}\label{thm:roses}
  Roses are red.
\end{proposition}

\begin{proposition}\label{thm:violets}
  Violets are blue.
\end{proposition}

\begin{proposition}
  42.
\end{proposition}

\begin{proposition} \label{thm:orchids}
  Orchids are orchid.
\end{proposition}

\begin{theorem}
  There exist flowers in at least three different colors.
\end{theorem}
\begin{proof}
  Immediate from \cref{thm:roses,thm:violets,thm:orchids}.
\end{proof}

\end{document}

Output

Should say “propositions 1, 2, and 4.”

2
  • 7
    Just wanted to make a note that the Oxford Comma is more generally known as a Serial Comma. Mostly noted for search engine indexing.
    – Patrick
    Feb 20, 2014 at 16:12
  • Well, it looks like Google knows they're synonyms, but thanks :-) Feb 20, 2014 at 20:06

3 Answers 3

51

If you define this new command

\newcommand{\creflastconjunction}{, and\nobreakspace}

you will get Oxford comma everywhere you use multiple references like those.

MWE:

\documentclass{article}
\usepackage[standard]{ntheorem}
\usepackage{cleveref}

\newcommand{\creflastconjunction}{, and\nobreakspace}

\begin{document}

\begin{proposition}\label{thm:roses}
  Roses are red.
\end{proposition}

\begin{proposition}\label{thm:violets}
  Violets are blue.
\end{proposition}

\begin{proposition}
  42.
\end{proposition}

\begin{proposition} \label{thm:orchids}
  Orchids are orchid.
\end{proposition}

\begin{theorem}
  There exist flowers in at least three different colors.
\end{theorem}
\begin{proof}
  Immediate from \cref{thm:roses,thm:violets,thm:orchids}.
\end{proof}

\end{document} 

Output:

enter image description here


ADDENDUM

If you were wondering why you have to use \newcommand instead of renewcommand, this is why.

cleveref defines, at the beginning of the document, a lot of commands depending on the language. If you don't specify any language, english is loaded. This is the relevant part of cleveref.sty:

\DeclareOption{english}{%
  \AtBeginDocument{%
    ....
    \def\creflastconjunction@preamble{ and\nobreakspace}%
    ....

Also, you can find the following lines

\AtBeginDocument{%
  ....
  \@ifundefined{creflastconjunction}{%
    \let\creflastconjunction\creflastconjunction@preamble%
  }{%
  ....
  }%

which, at the beginning of the document, assign to \creflastconjunction the meaning of \creflastconjunction@preamble when it has not been defined yet.

In other words, \creflastconjunction gets defined only after \begin{document}. In fact, if you try to put the line

\newcommand{\creflastconjunction}{, and\nobreakspace}

inside the document, you will get an error. In this case, you would have written

\renewcommand{\creflastconjunction}{, and\nobreakspace}
4
  • 1
    Does \newcommand{\creflastconjunction}{, and~} work the same? In other words, can you replace the \nobreakspace with a ~?
    – MSC
    Jan 24, 2017 at 22:22
  • 1
    @MSC No problems with your substitution. Jan 26, 2017 at 17:55
  • Is it possible to restrict this change of last conjunction just to English language (and do something else when \cref is issued in the context where the language is, say, German)?
    – user203414
    Jan 31, 2020 at 22:01
  • As I wondered about this: with just two references (\cref{thm:roses,thm:violets}) this correctly expands to propositions 1 and 2, without the extra comma. Nov 30, 2022 at 13:25
11

Simple answer: You can use

\newcommand{\creflastconjunction}{, and~}

From the manual:

\creflastconjunction is used between the penultimate and final cross-reference in a list of more than two [pg. 12, at least for the 2012-03-07 version of the manual I have]


Redoing your example:

\documentclass{article}
\usepackage\[standard\]{ntheorem}
\usepackage{cleveref}

\newcommand{\creflastconjunction}{, and~}

\begin{document}

\begin{proposition}\label{thm:roses}
  Roses are red.
\end{proposition}

\begin{proposition}\label{thm:violets}
  Violets are blue.
\end{proposition}

\begin{proposition}
  42.
\end{proposition}

\begin{proposition} \label{thm:orchids}
  Orchids are orchid.
\end{proposition}

\begin{theorem}
  There exist flowers in at least three different colors.
\end{theorem}
\begin{proof}
  Immediate from \cref{thm:roses,thm:violets,thm:orchids}.
\end{proof}

\end{document}

The compiled example, saying "Immediate from propositions 1, 2, and 4."

8
  • 1
    This is already in my answer... Feb 19, 2014 at 20:22
  • Shouldn't that be \renewcommand? Feb 19, 2014 at 20:23
  • @LukeMaurer: You'd think so, but surprisingly not. Feb 19, 2014 at 20:23
  • So cleveref is actually checking for the existence of that macro? Weird. *shrug* Feb 19, 2014 at 20:24
  • @karlkoeller: So it is! I guess great minds think alike; I must have started writing my answer while you were finishing writing yours. Feb 19, 2014 at 20:25
8

You can use \crefmultiformat:

\documentclass{article}
\usepackage[standard]{ntheorem}
\usepackage{cleveref}

\crefmultiformat{proposition}{propositions~#2#1#3}%
  { and~#2#1#3}{, #2#1#3}{, and~#2#1#3}

\begin{document}

\begin{proposition}\label{thm:roses}
  Roses are red.
\end{proposition}

\begin{proposition}\label{thm:violets}
  Violets are blue.
\end{proposition}

\begin{proposition}
  42.
\end{proposition}

\begin{proposition} \label{thm:orchids}
  Orchids are orchid.
\end{proposition}

\begin{theorem}
  There exist flowers in at least three different colors.
\end{theorem}
\begin{proof}
  Immediate from \cref{thm:roses,thm:violets,thm:orchids}.
  Immediate from \cref{thm:roses,thm:violets}.
\end{proof}

\end{document}

enter image description here

4
  • Aha! It's a little piecemeal (will need to repeat for different theorem sets), but it should work. Thanks. Feb 19, 2014 at 20:14
  • 1
    @LukeMaurer see karlkoeller's answer for a solution affecting all reference types. Feb 19, 2014 at 20:23
  • Out of curiosity, why the #2#1#3 instead of #1#2#3? Feb 20, 2014 at 4:14
  • Is it possible to restrict this change of last conjunction just to English language (and do something else when \cref is issued in the context where the language is, say, German)?
    – user203414
    Jan 31, 2020 at 22:03

You must log in to answer this question.

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