3

Consider the code

\documentclass{article}
\usepackage{listings}
\usepackage{cleveref}
\crefname{lstnumber}{line}{lines}
\Crefname{lstnumber}{Line}{Lines}
\begin{document}
\begin{lstlisting}[escapechar=@,numbers=left]
{A}@\label{lineref1}@
x:=1@\label{lineref2}@
{B}@\label{lineref3}@
y:=2@\label{lineref4}@
{C}@\label{lineref5}@
\end{lstlisting}
See the Hoare-triples at \namecrefs{lineref1} \ref{lineref1}--\ref{lineref3}, \ref{lineref3}--\ref{lineref5}.
%\labelcrefrange{lineref1}{lineref3}, \labelcrefrange{lineref3}{lineref5}
\end{document}

The output is output

With the (non-existing) \labelcrefrange, the input could have been a little bit more abstract: one would have to provide the format for the ranges only once per document (as opposed to spelling out the double hyphen without surrounding spaces on each use). Does such a command exist in cleveref (or some other standard packages)?

  • Did you try \crefrange? To get the dash you'll also need to add \newcommand{\crefrangeconjunction}{--} to your preamble. See this answer. – Circumscribe Sep 29 '18 at 22:49
4

There is little need for \labelcrefrange (to exist), when what you want can be produced by simply using \cref. This is because compression of cross-references can be prevented by providing suitable “empty labels” via extra commas (see Section 5 of the cleveref manual).

\documentclass{article}
\usepackage{listings}
\usepackage{cleveref}
\crefname{lstnumber}{line}{lines}
\Crefname{lstnumber}{Line}{Lines}
\newcommand{\crefrangeconjunction}{--}
%\newcommand{\creflastconjunction}{, }
\begin{document}
\begin{lstlisting}[escapechar=@,numbers=left]
{A}@\label{lineref1}@
x:=1@\label{lineref2}@
{B}@\label{lineref3}@
y:=2@\label{lineref4}@
{C}@\label{lineref5}@
z:=3@\label{lineref6}@
{D}@\label{lineref7}@
\end{lstlisting}
See the Hoare-triples at \namecrefs{lineref1} \ref{lineref1}--\ref{lineref3}, \ref{lineref3}--\ref{lineref5}.
%\labelcrefrange{lineref1}{lineref3}, \labelcrefrange{lineref3}{lineref5}

\noindent
See the Hoare-triples at
\cref{lineref1,lineref2,lineref3,,lineref3,lineref4,lineref5,,lineref5,lineref6,lineref7}.
\end{document}

references

To get rid of the “and”, uncomment the line \newcommand{\creflastconjunction}{, }.

  • 1
    @user49915 Line breaks are allowed in two places: 1) Between the first comma and the following 3; 2) Between the 5 and the and. For Oxford comma, you should use \newcommand{\creflastconjunction}{, and~}. – Ruixi Zhang Sep 30 '18 at 15:47

This site is temporarily in read only mode and not accepting new answers.

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