Quantcast
Channel: Cleveref's namecref usage in etoolbox' tests - TeX - LaTeX Stack Exchange
Viewing all articles
Browse latest Browse all 2

Answer by frougon for Cleveref's namecref usage in etoolbox' tests

$
0
0

Welcome to TeX.SE. You can use \cref@gettype to get the type of a cleveref reference in a macro of your choice (\mycref@type in the code below), then execute special code depending on the result. \cref@gettype expects that the \r@〈reference〉@cref macro has already been defined, which is done when the \newlabel command present in the .aux file is executed after the first compilation run with the 〈reference〉 in question. Therefore, my code checks whether the \r@〈reference〉@cref macro is defined and if not, prints ?? in the document along with the usual message on the terminal and in the log file:

LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right.

I removed the use of xspace in your code, because the original author of xspace nowadays advises against using it (moreover, as egreg pointed out, xspace wouldn't be useful here, since your macro takes an argument). I changed the fig:caption label to fig-caption, otherwise with cleveref and : as an active character,1 you'll encounter problems. Please also note that I use amsthm for the theorem in the example. Should you use theorem environments defined with \newtheorem from the LaTeX kernel, you'd find that the cleveref type for theorems using a shared counter is not necessarily what you expect (you'd get the name of the shared counter instead of the “specific” name; this doesn't happen with amsthm theorem environments).

Finally, don't forget to load cleveref last! :-)

\documentclass{article}\usepackage{expl3}\usepackage{amsthm}             % only for the demo%\usepackage{hyperref}          % usually last, but before cleveref\usepackage{cleveref}           % load this package last\newtheorem{theorem}{Théorème}\crefname{figure}{figure}{figures}    % instead of fig.\crefname{theorem}{théorème}{théorèmes}\makeatletter\ExplSyntaxOn% Let's borrow \str_case:onF from expl3 (\cs_new_eq:NN is like \let but checks% that the “destination” command name doesn't already exist).\cs_new_eq:NN \mycref@str@oswitch \str_case:onF\ExplSyntaxOff\newcommand*{\acref}[1]{%  \ifcsname r@#1@cref\endcsname    \cref@gettype{#1}{\mycref@type}%    \mycref@str@oswitch{\mycref@type}      {{figure}{la }%       {theorem}{le }%%      {...}{...}% you can add as many cases as you want!      }      {\errmessage{%         My package: unknown reference type for label '#1':'\unexpanded\expandafter{\mycref@type}'}%      }%  \else    \G@refundefinedtrue    \nfss@text{\reset@font\bfseries ??}%    \@latex@warning{Reference `#1' on page \thepage\space undefined}%  \fi  \cref{#1}%}\makeatother\begin{document}  \section{Title}\label{sec-title}  \begin{theorem}\label{th-some-theorem}    This is a theorem.  \end{theorem}  \begin{figure}    \centering X    \caption{Caption}    \label{fig-caption}  \end{figure}  Test with figure: \acref{fig-caption}.  Test with theorem: \acref{th-some-theorem}.% Test with section: \acref{sec-title}. % prints an error, as expected\end{document}

enter image description here

If you uncomment the \usepackage{hyperref} line and recompile until LaTeX is happy (because of the change in \newlabel syntax this implies), you'll get:

enter image description here


Footnote

  1. Which will normally be the case if you use \usepackage[french]{babel} with the pdfTeX engine.

Viewing all articles
Browse latest Browse all 2

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>