cleveref:自定义 \cref 输出

cleveref:自定义 \cref 输出

我正在使用该cleveref软件包。我的文档如下所示:

\documentclass{article}
\usepackage{cleveref}

\begin{document}
\tableofcontents

\section{Section A}
    Refer to \cref{append} for more details.

\appendix
\renewcommand{\thesection}{\Roman{section}}
\renewcommand{\thesubsection}{\Alph{subsection}}
\renewcommand{\thesubsubsection}{\Roman{subsubsection}}

\section{Appendix}
    \subsection{Appendix A}
        \subsubsection{Detailed Explanation} \label{append}
            blah
    \subsection{Appendix B}
\end{document}

我必须以这种方式使用小节和小小节,因为附录本身需要在 中有自己的条目和小节编号ToC

现在我希望我的输出类似于

A部分

更多详细信息请参阅附录 A 第 I 部分。

我已经尝试使用\crefname来自定义输出:

\crefname{secapp}{Anhang \Alph{subsection}, Section}{Appendix \Alph{subsection}, Section}

%Stuff here
\subsubsection{Detailed Explanation} \label[secapp]{append}

secapp但是像这样定义会导致cleveref打印被调用的子部分的编号\cref

有没有办法像上面描述的那样使用来格式化输出cleveref

谢谢!

答案1

在我看来,命名/编号方案令人困惑,但使用xassoccnt\RegisterPostLabelHook{\zlabel}宏会自动定义额外的标签,存储\thesubsection相关附录“部分”,稍后再提取\parentCref

\documentclass{article}
\usepackage{lipsum}
\usepackage{xassoccnt}
\usepackage{xpatch}
\usepackage{hyperref}
\usepackage[user,counter,hyperref]{zref}
\usepackage{cleveref}

\makeatletter
\AtEndPreamble{
  \newif\if@hyperrefloaded
  \@ifpackageloaded{hyperref}{
    \@hyperrefloadedtrue
  }{
    \@hyperrefloadedfalse
  }
}
\makeatother


\usepackage{xparse}

% Define a new property named 'appendix'

\makeatletter
\zref@newprop{appendix}{\thesubsection}

% Add the new property to the main property list stored with \zlabel, but for \appendix only
\g@addto@macro{\appendix}{%
  \zref@addprops{main}{appendix}%
}

% Command for uppercase output
\NewDocumentCommand{\parentCref}{m}{%
  \zref@ifrefundefined{#1}{%
    \Cref{#1}%
  }{%
    \if@hyperrefloaded
    \hyperlink{\zref@extract{#1}{anchor}}{\appendixname\ \zref@extract{#1}{appendix}, Section \zref@extract{#1}{default}}%
    \else
    \appendixname\ \zref@extract{#1}{appendix}, Section \zref@extract{#1}{default}%
    \fi
  }%
}
\makeatother


\AtBeginDocument{%
 \RegisterPostLabelHook{\zlabel}
}

\begin{document}


\section{Normal section} \label{firstsection}
Refer to \parentCref{append} for more details, but don't forget \parentCref{append:b}.

\appendix
\renewcommand{\thesection}{\Roman{section}}
\renewcommand{\thesubsection}{\Alph{subsection}}
\renewcommand{\thesubsubsection}{\Roman{subsubsection}}


\clearpage

\section{Appendix}
\subsection{Appendix A}
\subsubsection{Detailed Explanation} \label{append}
blah
\subsection{Appendix B} \label{appendix:B}
\subsubsection{Even more detailed explanations} \label{append:b}


\end{document}




%\crefname{subsubappendix}{\appendixname \Alph{subsection}, Section}{\appendixname \Alph{subsection}, Section}

\crefformat{subsubappendix}{#2\appendixname\ thesubappendix, Section  #1#3}
\Crefformat{subsubappendix}{#2\appendixname\ thesubappendix, Section  #1#3}

\begin{document}
\tableofcontents

\section{Section A} \label{firstsection}
Refer to \cref{append} for more details, but don't forget \Cref{append:b}.

\appendix
\renewcommand{\thesection}{\Roman{section}}
\renewcommand{\thesubsection}{\Alph{subsection}}
\renewcommand{\thesubsubsection}{\Roman{subsubsection}}



\section{Appendix}
\subsection{Appendix A}
\subsubsection{Detailed Explanation} \label{append}
blah
\subsection{Appendix B} \label{appendix:B}
\subsubsection{Even more detailed explanations} \label{append:b}

\end{document}

在此处输入图片描述

相关内容