7

The package cleveref has two types of references, \cref for lowercase and \Cref for uppercase. Does the package provide a way to define more types? If not, how may you define one yourself?

As an example, one may want to define abbreviated versions of the references, such that the code:

\documentclass{article}

\usepackage{fontspec, amsthm, hyperref}
\usepackage[nameinlink]{cleveref}

\newtheorem{theorem}{Theorem}

% Defining \abbrcref
% Defining \abbrCref
% Defining \abbrcrefformat
% Defining \abbrCrefformat

\crefformat{theorem}{#2theorem #1#3}
\Crefformat{theorem}{#2Theorem #1#3}
\abbrcrefformat{theorem}{#2th.#1#3}
\abbrCrefformat{theorem}{#2Th.#1#3}

\begin{document}
    \begin{theorem}
        \label{sometheorem}
    \end{theorem}

    \cref{sometheorem}, \Cref{sometheorem}, \abbrcref{sometheorem}, \abbrCref{sometheorem}
\end{document}

Would produce:

enter image description here

How would one go about doing that?

1 Answer 1

6

You can define a variable reference name and introduce a conditional for the cross references you may want to abbreviate.

\documentclass{article}

\usepackage{fontspec, amsthm, hyperref}
\usepackage[nameinlink]{cleveref}

\newtheorem{theorem}{Theorem}

% Defining \abbrcref
% Defining \abbrCref
% Defining \abbrcrefformat
% Defining \abbrCrefformat

\newcommand{\theoremref}{\ifabbrcref th.\,\else theorem~\fi}
\newcommand{\Theoremref}{\ifabbrcref Th.\,\else Theorem~\fi}

\crefformat{theorem}{#2\theoremref#1#3}
\Crefformat{theorem}{#2\Theoremref#1#3}

\newif\ifabbrcref
\NewDocumentCommand{\genericabbrcref}{msom}{%
  \begingroup\abbrcreftrue
  \IfBooleanTF{#2}{%
    \IfNoValueTF{#3}{#1*{#4}}{#1*[#3]{#4}}%
  }{%
    \IfNoValueTF{#3}{#1{#4}}{#1[#3]{#4}}%
  }%
  \endgroup
}
\newcommand{\abbrcref}{\genericabbrcref\cref}
\newcommand{\abbrCref}{\genericabbrcref\Cref}

\begin{document}

\begin{theorem}\label{sometheorem}
\end{theorem}

\cref{sometheorem}, \Cref{sometheorem}, \abbrcref{sometheorem}, \abbrCref{sometheorem}

\end{document}

See texdoc xparse for more information about \NewDocumentCommand.

enter image description here

You must log in to answer this question.

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