1

Is there a way to make cleveref abbreviate "listing" into "lis.", except at the beginning of a sentence? In other words, make it behave like "equation"?

\documentclass{book}
\usepackage{cleveref}
\usepackage[listings]{tcolorbox}

\newtcblisting[auto counter,number within=chapter,crefname={listing}{listings}]{labelboxcode}[3][]{%
    listing options={language=Java,#1},
    listing and comment,
    comment={Listing~\thetcbcounter: #3},
    label=#2
}

\begin{document}
\chapter{A chapter}
\begin{labelboxcode}{foo}{Class \texttt{Foo}.}
class Foo {}
\end{labelboxcode}

\Cref{foo} is class \verb;Foo; (\cref{foo}).
\end{document}

I expected \cref{foo} to produce lis. 1.1 instead of listing 1.1.

4
  • 2
    What's the name of the counter that's used in the cross-reference, and how is it displayed at present by \cref? Do you employ the listings package?
    – Mico
    May 24, 2021 at 15:00
  • I'm actually using tcolorbox, which has a mechanism for cleveref labels. They have type listing and are currently displayed as listing, with proper capitalization and plural forms. May 24, 2021 at 20:42
  • 1
    Please provide a minimal working example of your current setup.
    – Mico
    May 24, 2021 at 21:23
  • 1
    Added example to main post. May 25, 2021 at 12:31

1 Answer 1

3

All you need to do is replace the single option

crefname={listing}{listings}

with the following pair of options:

crefname={lis.}{listings}
Crefname={Listing}{Listings}

This works because the crefname={lis.}{listings} option generates LaTeX code to execute \crefname{listing}{lis.}{listings} for use by the cleveref package. By default, if a \crefname but no corresponding \Crefname macro is run, cleveref builds the form of the label to be used by \Cref by uppercasing the first letters in the two arguments of \crefname. The default mechanism can be overridden by providing the tcbcolorbox option Crefname={Listing}{Listings}.


A full MWE; the hyperref package and cleveref's nameinlink option are used merely to highlight the respective outputs of \Cref and \cref:

enter image description here

\documentclass{book}
\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[nameinlink]{cleveref}
\usepackage[listings]{tcolorbox}

\newtcblisting[auto counter,
               number within=chapter,
               crefname={lis.}{listings},  % was: {listing}{listings}
               Crefname={Listing}{Listings}% <-- new
              ]
              {labelboxcode}[3][]{%
               listing options={language=Java,#1},
               listing and comment,
               comment={Listing~\thetcbcounter: #3},
               label=#2}

\begin{document}

\chapter{A chapter}

\begin{labelboxcode}{foo}{Class \texttt{Foo}.}
   class Foo {}
\end{labelboxcode}

\noindent
\Cref{foo} is class \verb;Foo; (\cref{foo}).

\end{document}
4
  • Beautifully simple! Thanks a lot. May 27, 2021 at 13:53
  • I still have one minor problem someone might be able to help me with. Unfortunately, the demo code is getting longer; I doubt it'll fit in a comment. Do I need to start a new question? Jun 7, 2021 at 18:17
  • @MichelCharpentier - If the MWE (minimum working example) code is getting quite long, it's probably a good idea to post a new question. (Of course, for reference, you can always post a link to an earlier query in a new query.)
    – Mico
    Jun 7, 2021 at 18:23
  • Ok. Discussion continues in tex.stackexchange.com/q/600330/171866... Jun 7, 2021 at 18:53

You must log in to answer this question.

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