[Tex/LaTex] How to configure \cref to write “Appendix A” instead of “Chapter A” when referring to appendix

appendicescleverefcross-referencingkoma-script

I want to configure \cref to write "Appendix A" instead of misleading "Chapter A".


Minimum Working Example (MWE):

\documentclass{scrbook}
\usepackage[toc,page]{appendix}
\usepackage{cleveref}

\begin{document}

\tableofcontents

\chapter{Regular Chapter}
    This is a test, represented in \cref{appendix:Test}.

\begin{appendices}
    \chapter{Test Appendix}
        The contents...\label{appendix:Test}
\end{appendices}
\end{document}

Screenshot of the result:

The code above will create the following page:

\cref referring to misleading target


Description of the issue:

The problem now is: A potential reader won't understand that "Chapter A" is a part of the appendix. Therefore I would prefer to write something like "Appendix A" in the text.

How can I configure \cref to write "Appendix A" instead of misleading "Chapter A" when labelling the target appendix with \label{appendix:Random test appendix}?

\cref does such a great job for figures and tables, why not for appendices? In best case (like it already does for figures and tables), it should automatically detect if the \cref is placed at the beginning of a sentence (= "Appendix A") or in the middle/end of a sentence (= "appendix A")…

How to configure that? 馃檪

Best Answer

You can use \label[appendix]{...} for each chapter label in the appendix or you can set \crefalias{chapter}{appendix} in the appendices environment.

\documentclass{scrbook}
\usepackage{xpatch}
\usepackage[toc,page]{appendix}
\usepackage{cleveref}

\xapptocmd\appendices{%
  \crefalias{chapter}{appendix}%
}{}{\PatchFailed}

\begin{document}
\tableofcontents
\chapter{Regular Chapter}
This is a test, represented in \cref{appendix:Test}. \Cref{appendix:Test} \ldots

\begin{appendices}
  \chapter{Test Appendix}\label{appendix:Test}
  The contents \ldots
\end{appendices}
\end{document}

Result:

enter image description here

Complex example:

\documentclass{scrbook}
\usepackage{xpatch}
\usepackage[toc,page]{appendix}
\usepackage[colorlinks]{hyperref}
\usepackage{cleveref}

\xapptocmd\appendices{%
  \crefalias{chapter}{appendix}%
}{}{\PatchFailed}

\begin{document}
\tableofcontents
\chapter{Regular Chapter}
\begingroup
\raggedright
This is a test, represented in
  \cref{appendix:Test_A}.
  \Cref{appendix:Test_A}
  \ldots\par
This is a test, represented in
  \cref{appendix:Test_A,,appendix:Test_B,appendix:Test_C}.
  \Cref{appendix:Test_A,,appendix:Test_B,appendix:Test_C}
  \ldots\par
This is a test, represented in
  \cref{appendix:Test_A,appendix:Test_B,appendix:Test_C}.
  \Cref{appendix:Test_A,appendix:Test_B,appendix:Test_C}
  \ldots\par
This is a test, represented in
  \cref{appendix:Test_D,appendix:Test_B,appendix:Test_C}.
  \Cref{appendix:Test_D,appendix:Test_B,appendix:Test_C} \ldots\par
This is a test, represented in
  \cref{appendix:Test_A,appendix:Test_B,appendix:Test_C,appendix:Test_E,appendix:Test_F}.
  \Cref{appendix:Test_A,appendix:Test_B,appendix:Test_C,appendix:Test_E,,appendix:Test_F}
  \ldots\par
\endgroup

\begin{appendices}
  \chapter{Test Appendix}\label{appendix:Test_A}
  The contents \ldots
  \chapter{Test Appendix}\label{appendix:Test_B}
  The contents \ldots
  \chapter{Test Appendix}\label{appendix:Test_C}
  The contents \ldots
  \chapter{Test Appendix}\label{appendix:Test_D}
  The contents \ldots
  \chapter{Test Appendix}\label{appendix:Test_E}
  The contents \ldots
  \chapter{Test Appendix}\label{appendix:Test_F}
  The contents \ldots
\end{appendices}
\end{document}

Result:

enter image description here


If the appendix is not followed by "normal" chapters you can use the switch \appendix:

\documentclass{scrbook}
\usepackage[toc,page]{appendix}
\usepackage{cleveref}

\begin{document}
\tableofcontents
\chapter{Regular Chapter}
This is a test, represented in \cref{appendix:Test}. \Cref{appendix:Test} \ldots

\appendix
\begin{appendices}
  \chapter{Test Appendix}\label{appendix:Test}
  The contents \ldots
\end{appendices}
\end{document}

or without package appendices:

\documentclass{scrbook}
\usepackage{xpatch}
\usepackage{cleveref}

\xapptocmd\appendix{%
  \cleardoublepage
  \addpart{Appendices}%
}{}{\Patch Failed}

\begin{document}
\tableofcontents
\chapter{Regular Chapter}
This is a test, represented in \cref{appendix:Test}. \Cref{appendix:Test} \ldots

\appendix
\chapter{Test Appendix}\label{appendix:Test}
The contents \ldots
\end{document}

If the TOC entry for the appendices page should have the same style as chapter entries use:

\documentclass{scrbook}
\usepackage{xpatch}
\usepackage{cleveref}

\xapptocmd\appendix{%
  \cleardoublepage
  \addchaptertocentry{}{Appendices}%
  \addpart*{Appendices}%
}{}{\Patch Failed}

\begin{document}
\tableofcontents
\chapter{Regular Chapter}
This is a test, represented in \cref{appendix:Test}. \Cref{appendix:Test} \ldots

\appendix
\chapter{Test Appendix}\label{appendix:Test}
The contents \ldots
\end{document}
Related Question