4

How can I get cleveref to work with \linelabel from lineno?

\documentclass{article}



\usepackage{lineno}
\usepackage{cleveref}



\begin{document}
\linenumbers

Bla bla bla  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla \linelabel{foo}  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

\cref{foo} doesn't work because the cleveref line doesn't get written to the aux file, but \ref{foo} works.
\end{document}
3
  • 1
    \linelabel is not using \label and that's why cleveref can't hook into it
    – user31729
    Commented Sep 18, 2017 at 16:13
  • I understand why it goes wrong. What I want to know is how to fix it. In other words, how can I tweak \linelabel to write a cleveref-compatible line to the aux file?
    – JPi
    Commented Sep 18, 2017 at 16:13
  • 1
    One possibility is to use \cref@currentlabel, but I have no time to figure it out
    – user31729
    Commented Sep 18, 2017 at 16:30

1 Answer 1

3

This is a quick hack, using \@LN@linelabel which does the line labelling.

First I use the old version of the command, extract the label value again, then set a backup counter, set linenumber to the label value, decrease it and step it again to get the correct \cref@currentlabel and then write label directly as cleveref would do.

\documentclass{article}
\usepackage{refcount}


\usepackage{lineno}
\usepackage{cleveref}

\newcounter{backuplineno}

\crefname{linenumber}{linenumber}{linenumbers}
\Crefname{linenumber}{Linenumber}{Linenumbers}



\makeatletter
\let\@LN@linelabelorig\@LN@linelabel
\renewcommand{\@LN@linelabel}[1]{%
  \@LN@linelabelorig{#1}%
  \IfRefUndefinedExpandable{#1}{}{%
    \setcounter{backuplineno}{\value{linenumber}}
    \addtocounter{linenumber}{-1}%
    \setcounter{linenumber}{\numexpr\getrefnumber{#1} -1}
    \refstepcounter{linenumber}%
    \protected@write\@auxout{}%
    {\string\newlabel{#1@cref}{{\cref@currentlabel}{\thepage}}}%
    \setcounter{linenumber}{\value{backuplineno}}
  }%
}
\makeatother

\begin{document}
\linenumbers

Bla bla bla  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla \linelabel{foo}  bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla

\cref{foo} doesn't work because the cleveref line doesn't get written to the aux file, but \ref{foo} works.
\end{document}

enter image description here

1
  • 1
    I'll try to find a better version
    – user31729
    Commented Sep 18, 2017 at 16:43

You must log in to answer this question.

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