autoref、titleclass 和 titlesec——显示父级和子级计数器

autoref、titleclass 和 titlesec——显示父级和子级计数器

我想为工作包 (WP) 实现一个新的标题类,并为子工作包实现一个嵌套的标题子类。两者的工作方式应类似于\section\subsection,即当工作包计数器增加时,子工作包计数器会重置。

为了实现这一点,我使用了\titleclasstitlesec 包的命令以及相应的\newcounter命令。然而,问题是,当我引用工作包带有 autoref,则只会显示子工作包的计数器。但是,我想要父工作包的计数器,后跟“。”,然后是子工作包的计数器,就像部分和子部分一样。

例如,如果wp:sub2test是第一个父工作包中的第二个子工作包的标签,\autoref{wp:sub2test}则应显示“WP 1.2”。但是,正如您在我的 MWE 中看到的那样,它显示“WP 2”。我该如何解决这个问题,即,我该如何告诉 autoref 吐出父计数器,然后是一个点,然后是子计数器?

\documentclass[11pt,a4paper]{article}
\usepackage{titlesec}
\usepackage{hyperref}

\titleclass{\workpackage}{straight}[\section]
\newcounter{workpackage}
\titleformat{\workpackage}
  {\normalfont\normalsize\bfseries}{}{0em}{WP \theworkpackage \quad -- \quad}
\titlespacing*{\workpackage}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\workpackageautorefname}{WP}

\titleclass{\subworkpackage}{straight}[\section]
\newcounter{subworkpackage}
\titleformat{\subworkpackage}
  {\normalfont\normalsize\bfseries}{}{0em}{WP \theworkpackage.\thesubworkpackage \quad -- \quad}
\titlespacing*{\subworkpackage}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\subworkpackageautorefname}{WP}

\begin{document}

\workpackage{test}
\label{wp:test}
some wp
\subworkpackage{sub1test}
\label{wp:sub1test}
some subwp

\subworkpackage{sub2test}
\label{wp:sub2test}
some subwp

\section{Referring to WP with autoref}
\label{sec:sec}
This gives \autoref{wp:test} and \autoref{wp:sub1test} and \autoref{wp:sub2test}, but it should give WP 1 and WP 1.1 and WP 1.2.
\end{document}

output

答案1

subworkpackage的“子”级别workpackage,而不是 的\section,因此必须使用\workpackage作为“父”级别,而不是\section,否则所有计数器信息都将是错误的,参考标签也是如此!

\documentclass[11pt,a4paper]{article}
\usepackage{titlesec}

\usepackage{hyperref}


\titleclass{\workpackage}{straight}[\section]
\newcounter{workpackage}
\titleformat{\workpackage}
  {\normalfont\normalsize\bfseries}{}{0em}{WP \theworkpackage \quad -- \quad}
\titlespacing*{\workpackage}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\workpackageautorefname}{WP}

\titleclass{\subworkpackage}{straight}[\workpackage]
\newcounter{subworkpackage}
\renewcommand{\thesubworkpackage}{\theworkpackage.\arabic{subworkpackage}}
\titleformat{\subworkpackage}
  {\normalfont\normalsize\bfseries}{}{0em}{WP \thesubworkpackage \quad -- \quad}
\titlespacing*{\subworkpackage}{0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}
\newcommand{\subworkpackageautorefname}{WP}



\begin{document}

\workpackage{test}
\label{wp:test}
some wp
\subworkpackage{sub1test}
\label{wp:sub1test}
some subwp

\subworkpackage{sub2test}
\label{wp:sub2test}
some subwp

\section{Referring to WP with autoref}
\label{sec:sec}
This gives \autoref{wp:test} and \autoref{wp:sub1test} and \autoref{wp:sub2test}, but it should give WP 1 and WP 1.1 and WP 1.2.
\end{document}

enter image description here

相关内容