2

I'm trying this:

\documentclass{article}
\usepackage{cleveref}
\begin{document}
\newcounter{rule}
\newcommand\printrule[1]{R#1}
\newcommand\orule[1]{\refstepcounter{rule}\label{#1}\printrule{\therule}: }
\crefname{rule}{Rule}{Rules}
\crefformat{rule}{#2\protect \printrule{#1}#3}
\orule{foo} Foo \\
\orule{bar} Bar \\
See \cref{foo}. \\
See \cref{foo,bar}.
\end{document}

I'm getting:

R1: Foo 
R2: Bar
See R1.
See ?? 1?? 2.

What is wrong? ChatGPT suggests that \crefformat{rule} should be replaced with \crefformat{Rule}, but it doesn't help :)

2
  • 1
    The problem vanishes if one moves the first fives lines after \begin{document} into the preamble. In particular, See \cref{foo,bar}. generates "See Rules 1 and 2."
    – Mico
    Jan 22, 2023 at 12:25
  • 1
    @Mico you are right! If you post an answer, I will gladly accept it.
    – yegor256
    Jan 22, 2023 at 12:30

1 Answer 1

4

My main suggestion is that you move the first five lines of code after \begin{document} into the preamble.

In addition, if you want cross-references to rules be prefaced with the letter "R" automatically, I suggest you modify the macro \therule accordingly. With this change in place, all you need to do after loading the cleveref package is to provide suitable \crefname and \Crefname directives.

enter image description here

\documentclass{article}

\newcounter{rule}
\renewcommand\therule{R\arabic{rule}}
\newcommand\orule[1]{\refstepcounter{rule}\label{#1}\therule:}

\usepackage{cleveref}
\crefname{rule}{rule}{rules}
\Crefname{rule}{Rule}{Rules}

\begin{document}
\orule{foo} Foo.

\orule{bar} Bar.

See \labelcref{foo}. \Cref{bar} expresses\dots\ See \cref{foo,bar}.
\end{document}

You must log in to answer this question.

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