本地重新定義cleveref的章節參考名稱

本地重新定義cleveref的章節參考名稱

考慮以下 MWE:

\documentclass{scrbook}
\usepackage{appendix}
\usepackage{cleveref}

\usepackage{etoolbox}
\BeforeBeginEnvironment{appendices}{%
    \crefname{chapter}{appendix}{appendices}%
    %\Crefname{chapter}{Appendix}{Appendices}%
}
\AfterEndEnvironment{appendices}{%
    \crefname{chapter}{chapter}{chapters}%
    %\Crefname{chapter}{Chapters}{Chapters}%
}

\begin{document}
    \noindent Reference to~regular chapter: \cref{chp:regular_chapter}.

    \noindent Reference to~appendix chapter: \cref{chp:appendix_chapter}.

    \chapter{Regular chapter} \label{chp:regular_chapter}

    Regular chapter text.

    \begin{appendices}
        \chapter{Appendix chapter} \label{chp:appendix_chapter}

        Appendix chapter text.
    \end{appendices}
\end{document}

結果如下:

實際結果

雖然希望結果如下:

期望的結果

我想知道如何修復上述在本地重新定義cleveref章節引用名稱的嘗試,使其按預期工作。

一般來說,我需要一個\crefname在本地範圍內使用巨集的解決方案。
添加\begingroup/\endgroup沒有幫助。

作為額外的改進,章節的原始參考名稱可能會首先被存儲,並在環境結束後恢復appendices(而不是在 MWE 中硬編碼的chapter/字串對)。chapters

答案1

封裝cleveref重新定義了開關\appendix。如果新增此命令(有或沒有appendices環境),您將在 MWE 中獲得所需的參考。但沒有指令可以切換回常規章節參考。

解決方案是使用 的可選參數label。這個附加的可選參數也由 提供cleveref

\documentclass{scrbook}
\usepackage{appendix}
\usepackage{cleveref}

\begin{document}
\noindent Reference to regular chapter: \cref{chp:regular_chapter}.

\noindent Reference to appendix chapter: \cref{chp:appendix_chapter}.

\noindent Referece to another regular chapter: \cref{chp:another}.

\chapter{Regular chapter} \label{chp:regular_chapter}
Regular chapter text.

\begin{appendices}
  \chapter{Appendix chapter}
  \label[appendix]{chp:appendix_chapter}% <- changed (optional argument added)
  Appendix chapter text.
\end{appendices}

\chapter{Another regular chapter}\label{chp:another}
\end{document}

在此輸入影像描述

答案2

esdd的 回答我設法以更自動化的方式做到這一點:

\documentclass{scrbook}
\usepackage{appendix}
\usepackage{cleveref}

\begin{document}
    \noindent Reference to~regular chapter: \cref{chp:regular_chapter}.

    \noindent Reference to~appendix chapter:  \cref{chp:appendix_chapter}.

    \noindent Reference to~another regular chapter:  \cref{chp:another_regular_chapter}.

    \chapter{Regular chapter} \label{chp:regular_chapter}

    Regular chapter text.

    \begin{appendices}
        \appendix
        \chapter{Appendix chapter} \label{chp:appendix_chapter}

        Appendix chapter text.
    \end{appendices}
    \renewcommand{\thechapter}{\arabic{chapter}}

    \chapter{Another regular chapter} \label{chp:another_regular_chapter}

    Another regular chapter text.
\end{document}

結果

相關內容