如何配置 \cref 在引用附录时写入“附录 A”而不是“章节 A”?

如何配置 \cref 在引用附录时写入“附录 A”而不是“章节 A”?

我想配置\cref为写“附录A”,而不是误导性的“第A章”。


最小工作示例(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}

结果截图:

上述代码将创建以下页面:

\cref 指的是误导性目标


问题说明:

现在的问题是:潜在读者不会明白“A 章”是附录的一部分。因此,我更愿意在正文中写上“附录 A”之类的内容。

如何配置\cref在标记目标附录时写入“附录A”而不是误导性的“章节A” \label{appendix:Random test appendix}

\cref对于图表来说,它已经做得非常好了,为什么对于附录就不行呢?在最好的情况下(就像它已经对图表所做的那样),它应该自动检测是否\cref位于句子的开头(=“A附录 A”)或句子中间/结尾处(=“A附录 A”)...

如何配置?:-)

答案1

您可以\label[appendix]{...}在附录中为每个章节使用标签,也可以\crefalias{chapter}{appendix}appendices环境中设置。

\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}

结果:

在此处输入图片描述

复杂示例:

\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}

结果:

在此处输入图片描述


如果附录后面没有“正常”章节,则可以使用开关\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}

或不带包装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}

如果附录页的目录条目应具有与章节条目相同的样式,则使用:

\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}

答案2

在此处输入图片描述

这是一个可以很好地配合hyperref并自动生成“附录”和“附录”单词的解决方案。

加载后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}

它定义了使用时附录引用将如何出现\cref在句子中间使用它。

另外,添加

\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}

它定义了使用时附录引用将如何出现\Cref在句首使用这个。

两种代码都将以非常优雅和通用的方式定制\cref\Cref命令引用附录。定义附录的标签时,使用 \label[app]{<Appendix_Label>}

此解决方案无法检测我们是在句子的开头还是中间。因此,您必须手动决定是否使用\Cref\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}

相关内容