3

Is there a way to configure the cleveref package to refer to figure names that are in the appendices differently from figures in the main body?

I want figures in the main body to be captioned and referred to in the text as "Figure 1" and figures in the appendices as "Appendix figure A.1" etc. I tried using the following at the start of the appendices environment.

\begin{appendices} 
\renewcommand{\figurename}{Appendix figure}

However, that only changes the captions of the appendix figures and not how cleveref refers to them (the caption of the figure is "Appendix figure A.1" but using \cref{appendixfig} in the main text or in the appendices still produces "Figure A.1").

1 Answer 1

3

figure-captions:

As you already noted in your question you need to redefine \figurename within the appendix in order to get figure-captions right in the appendix.


cleveref:

You can introduce another cleveref-referencing-type "appendixfigure" via \crefname.

In the appendix you can override the standard "figure"-type by the "appendixfigure"-type via \crefalias.

This way in \cref-references to figures from the appendices you will have the phrase "Appendix figure"/"Appendix figures" if these phrases were provided when specifying the "appendixfigure"-type.

These are the things that can be done in order to get some of the cleveref-stuff right.


hyperref's \autoref:

But things will be a bit more tricky when you wish to also get hyperref's \autoref-stuff right:

With \autoref the phrase prepended to the referenced value is derived from the name of the counter whose value is referenced by calling one of the macros \⁠⟨name of counter⟩⁠refname or \⁠⟨name of counter⟩⁠autorefname.
The name of the counter whose value is referenced in turn is extracted from the name of the destination which the reference shall form a hyperlink to.

Therefore you need to patch the macro which creates names of destinations for writing them to the .aux-file as part of a \newlabel-entry, i.e., the macro \hyper@makecurrent, to check if in the appendices-environment while creating the destination and, if so, to replace the phrase denoting the name of the figure-counter by something else. (In the example below that phrase is replaced by the phrase "appendixfigure".) Of course there must be defined a macro \⁠⟨something else⟩⁠refname or \⁠⟨something else⟩⁠autorefname.

\documentclass{book}
\usepackage{appendix}
\usepackage{hyperref}
\usepackage{cleveref}
\crefname{figure}{Figure}{Figures}
\crefname{appendixfigure}{Appendix figure}{Appendix figures}
% The following stuff is to get hyperref's autoref-feature right:
\usepackage{etoolbox}
\newif\ifappendices\appendicesfalse
\makeatletter
\patchcmd{\hyper@makecurrent}{%
    \ifx\Hy@param\Hy@chapterstring
        \let\Hy@param\Hy@chapapp
    \fi
}{%
    \ifx\Hy@param\Hy@chapterstring
        \let\Hy@param\Hy@chapapp
    \fi
    \ifappendices
      \expandafter\Hy@paramappendixreplace\expandafter{\Hy@param}%
    \fi
}{}{\errmessage{failed to patch}}
%
\newcommand\Hy@paramappendixreplace[1]{%
   \Hy@paramappendixreplacefork
   #1\relax{\def\Hy@param{appendixfigure}}%
   figure\relax{}%
   \relax\relax\relax\relax
}%
\@ifdefinable\Hy@paramappendixreplacefork{%
  \long\def\Hy@paramappendixreplacefork#1figure\relax#2#3\relax\relax\relax\relax{#2}%
}%
\makeatother
\newcommand*\appendixfigureautorefname{Appendix figure}%
\newcommand*\appendixfigurerefname{Appendix figure}%
% End of patching hyperref's autoref-feature

\begin{document}

\hrule

\noindent \textbf{Referencing:} \verb|\cref{regular figure}|: \cref{regular figure}

\noindent \textbf{Referencing:} \verb|\cref{appendix figure}|: \cref{appendix figure}

\noindent \textbf{Referencing:} \verb|\nameref{regular figure}|: \nameref{regular figure}

\noindent \textbf{Referencing:} \verb|\nameref{appendix figure}|: \nameref{appendix figure}

\noindent \textbf{Referencing:} \verb|\autoref{regular figure}|: \autoref{regular figure}

\noindent \textbf{Referencing:} \verb|\autoref{appendix figure}|: \autoref{appendix figure}

\hrule

\tableofcontents
\listoffigures

\chapter{Regular Chapter}
\begin{figure}[t]%
\begin{center}
\fbox{Some figure in the mainmatter.}
\caption{Figure in the mainmatter}
\label{regular figure}
\end{center}
\end{figure}

\begin{appendices}
\appendicestrue
\crefalias{figure}{appendixfigure}%
\renewcommand\figurename{Appendix figure}%
\chapter{Some Appendix}
%%%
\addtocontents{lof}{\string\noindent\string\textbf{Figures in appendices}\string\vskip10pt\relax}%
%%%
\begin{figure}[t]%
\begin{center}
\fbox{Some figure in the appendix.}
\caption{Figure in the appendix}
\label{appendix figure}
\end{center}
\end{figure}
\end{appendices}

\vfill

\hrule

\noindent \textbf{Referencing:} \verb|\cref{regular figure}|: \cref{regular figure}

\noindent \textbf{Referencing:} \verb|\cref{appendix figure}|: \cref{appendix figure}

\noindent \textbf{Referencing:} \verb|\nameref{regular figure}|: \nameref{regular figure}

\noindent \textbf{Referencing:} \verb|\nameref{appendix figure}|: \nameref{appendix figure}

\noindent \textbf{Referencing:} \verb|\autoref{regular figure}|: \autoref{regular figure}

\noindent \textbf{Referencing:} \verb|\autoref{appendix figure}|: \autoref{appendix figure}

\hrule

\vfill

\end{document}

enter image description here

You must log in to answer this question.

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