1

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? :-)

  • The example does not use \cref. If the first letter should be uppercase use \Cref. – esdd Sep 29 '18 at 15:11
3

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}
  • I guess Lehmanns would print your book »250 ready-made code examples for KOMA-script«. At least I'd buy it. – Keks Dose Sep 30 '18 at 9:46
1

enter image description here

Here is a solution that works well with hyperref and generates 'Appendix' and 'Appendices' words automatically.

After loading cleveref, use this code in the preamble

\crefformat{app}{appendix #2#1#3}
\crefrangeformat{app}{appendices #3#1#4 to #5#2#6}
\crefmultiformat{app}{appendices #2#1#3}{ and #2#1#3}{, #2#1#3}{ and #2#1#3}
\crefrangemultiformat{app}{appendices #3#1#4 to #5#2#6}{ and #3#1#4 to #5#2#6}{, #3#1#4 to #5#2#6}{ and #3#1#4 to #5#2#6}

Which defines how appendices referencing will appear when using \cref. Use this at the middle of sentences.

Also, add this

\Crefformat{app}{Appendix #2#1#3}
\Crefrangeformat{app}{Appendices #3#1#4 to #5#2#6}
\Crefmultiformat{app}{Appendices #2#1#3}{ and #2#1#3}{, #2#1#3}{ and #2#1#3}
\Crefrangemultiformat{app}{Appendices #3#1#4 to #5#2#6}{ and #3#1#4 to #5#2#6}{, #3#1#4 to #5#2#6}{ and #3#1#4 to #5#2#6}

Which defines how appendices referencing will appear when using \Cref. Use this at the beginning of sentences.

Both codes will customize \cref and \Cref commands to refer to appendices in very elegant and generalized fashion. When you define a label for an appendix, use \label[app]{<Appendix_Label>}

This solution does not detect if we are at the beginning or the middle of a sentence. Thus, you must use decide manually whether to use \Cref or \cref.

\documentclass{scrbook}
\usepackage{fontspec}
\usepackage[x11names, svgnames]{xcolor}
\usepackage{hyperref}
\hypersetup{linkcolor=DodgerBlue3, colorlinks=true}


\usepackage[toc,page]{appendix}
\usepackage[notref, notcite, color]{showkeys}

\usepackage{cleveref}
\crefformat{app}{appendix #2#1#3}
\crefrangeformat{app}{appendices #3#1#4 to #5#2#6}
\crefmultiformat{app}{appendices #2#1#3}{ and #2#1#3}{, #2#1#3}{ and #2#1#3}
\crefrangemultiformat{app}{appendices #3#1#4 to #5#2#6}{ and #3#1#4 to #5#2#6}{, #3#1#4 to #5#2#6}{ and #3#1#4 to #5#2#6}

\Crefformat{app}{Appendix #2#1#3}
\Crefrangeformat{app}{Appendices #3#1#4 to #5#2#6}
\Crefmultiformat{app}{Appendices #2#1#3}{ and #2#1#3}{, #2#1#3}{ and #2#1#3}
\Crefrangemultiformat{app}{Appendices #3#1#4 to #5#2#6}{ and #3#1#4 to #5#2#6}{, #3#1#4 to #5#2#6}{ and #3#1#4 to #5#2#6}

\begin{document}

\tableofcontents

\chapter{Regular Chapter}

Using \texttt{$\backslash$Cref}

\noindent \Cref{Appendix_A}

\noindent \Cref{Appendix_A,Appendix_B,Appendix_C}

\noindent\Cref{Appendix_D,,Appendix_E,,Appendix_F}

\noindent \Cref{Appendix_A,Appendix_B,Appendix_C,Appendix_E,Appendix_F,Appendix_I}

\noindent \Cref{Appendix_B,Appendix_C,Appendix_E,Appendix_F,Appendix_G,Appendix_H,,Appendix_I}

\noindent \Cref{Appendix_A,Appendix_B,Appendix_C,Appendix_E,Appendix_F,Appendix_G,Appendix_H,Appendix_I}

\vspace{\baselineskip}


\noindent Using \texttt{$\backslash$cref}

\noindent Test sentence \cref{Appendix_A}

\noindent Test sentence \cref{Appendix_A,Appendix_B,Appendix_C}

\noindent Test sentence \cref{Appendix_D,,Appendix_E,,Appendix_F}

\noindent Test sentence \cref{Appendix_A,Appendix_B,Appendix_C,Appendix_E,Appendix_F,Appendix_I}

\noindent Test sentence \cref{Appendix_B,Appendix_C,Appendix_E,Appendix_F,Appendix_G,Appendix_H,,Appendix_I}

\noindent Test sentence \cref{Appendix_A,Appendix_B,Appendix_C,Appendix_E,Appendix_F,Appendix_G,Appendix_H,Appendix_I}

\begin{appendices}
    \chapter{Appendix A}
        The contents...\label[app]{Appendix_A}
     \chapter{Appendix B}
        The contents...\label[app]{Appendix_B}
     \chapter{Appendix C}
    The contents...\label[app]{Appendix_C}
     \chapter{Appendix D}
    The contents...\label[app]{Appendix_D}
     \chapter{Appendix E}
    The contents...\label[app]{Appendix_E}
     \chapter{Appendix F}
    The contents...\label[app]{Appendix_F}
     \chapter{Appendix G}
    The contents...\label[app]{Appendix_G}
     \chapter{Appendix H}
    The contents...\label[app]{Appendix_H}
     \chapter{Appendix I}
    The contents...\label[app]{Appendix_I}
\end{appendices}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.