
Рассмотрим следующий 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
среды (вместо пары строк chapter
/ chapters
, жестко закодированной в MWE).
решение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's отвечатьМне удалось сделать это немного более автоматизированным способом:
\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}