Redefinir localmente el nombre de referencia de Cleveref para los capítulos.

Redefinir localmente el nombre de referencia de Cleveref para los capítulos.

Considere el siguiente 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}

Da como resultado el siguiente resultado:

Resultado actual

Si bien se desea que el resultado sea el siguiente:

Resultado deseado

Me pregunto cómo cleverefse puede arreglar el intento anterior de redefinir localmente el nombre de las referencias de los capítulos para que funcione según lo previsto.

En general, necesito una solución para utilizar \crefnamemacros dentro de un ámbito local.
Agregar \begingroup/ \endgroupno ayudó.

Como mejora adicional, el nombre de referencia original de los capítulos podría almacenarse al principio y restaurarse después del final del appendicesentorno (en lugar del par de cadenas chapter/ chapterscodificado en el MWE).

Respuesta1

"El paquete cleverefredefine el cambio" \appendix. Si agrega este comando (con o sin el appendicesentorno) obtendrá las referencias deseadas en su MWE. Pero no hay ningún comando para volver a las referencias de capítulos habituales.

Una solución es utilizar el argumento opcional de label. Este argumento opcional adicional también lo proporciona 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}

ingrese la descripción de la imagen aquí

Respuesta2

Despuésesdd respuestaLogré hacerlo de una manera un poco más automatizada:

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

Resultado

información relacionada