带后缀的零件编号:I、II、IIIa、IIIb、IV、

带后缀的零件编号:I、II、IIIa、IIIb、IV、

我希望我的零件编号如下:

I, II, IIIa, IIIb, ... , IV, ...

我编写了一些运行良好的代码。但是当使用时hyperref,我收到此警告: destination with the same identifier (name{part.1}) has been already used, duplicate ignored,因为我将子编号的计数器重置为零。

问题

有什么方法可以达到相同的结果,但无需重置零件计数器(并且不会产生hyperref警告)?

代码

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\tableofcontents
\pagebreak

\part{First}
\part{Second}

% change part numbering to ROMAN alph
\newcommand{\thelastpart}{\arabic{part}}%store old number
\edef\thelastpartexp{\thelastpart}
\addtocounter{part}{1}
\edef\themainpart{\thepart} 
\setcounter{part}{0}%set subcounter
\renewcommand{\thepart}{\themainpart\alph{part}}

\part{Third a}
\part{Third b}

% reset part numbering
\setcounter{part}{\thelastpartexp}%restore counter
\addtocounter{part}{1}
\renewcommand{\thepart}{\Roman{part}}


\part{Fourth}

\end{document}

答案1

为了让 hyperref 对您自己的解决方案感到满意,只需重新定义\theHpart以提供唯一的值,例如:

\documentclass{article}
\usepackage{hyperref}

\begin{document}
\tableofcontents
\pagebreak

\part{First}
\part{Second}

% change part numbering to ROMAN alph
\newcommand{\thelastpart}{\arabic{part}}%store old number
\edef\thelastpartexp{\thelastpart}
\addtocounter{part}{1}
\edef\themainpart{\thepart}
\setcounter{part}{0}%set subcounter
\renewcommand{\thepart}{\themainpart\alph{part}}
\renewcommand{\theHpart}{\themainpart\alph{part}}%<--------------

\part{Third a}
\part{Third b}

% reset part numbering
\setcounter{part}{\thelastpartexp}%restore counter
\addtocounter{part}{1}
\renewcommand{\thepart}{\Roman{part}}
\renewcommand{\theHpart}{\Roman{part}} %<--(probably not really needed)

\part{Fourth}

\end{document}

答案2

这是一个受 启发的解决方案。您可以使用带有可选参数的subequation命令,在该命令中您可以指定要共享主编号的部分的选项。您还可以指定应放入目录中的其他标题。\Partsubparttoctitle=...

\documentclass{article}
\usepackage{hyperref}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\Part}{ O{} m }
 {
  \ralfix_part:nn { #1 } { #2 }
 }
\keys_define:nn { ralfix/part }
 {
  toctitle .tl_set:N = \l_ralfix_toctitle_tl,
  subpart .bool_set:N = \l_ralfix_subpart_bool,
  subpart .default:n = { true },
 }
\cs_new_protected:Npn \ralfix_part:nn #1 #2
 {
  \keys_set:nn { ralfix/part }
   {
    subpart=false,
    toctitle=#2,
    #1
   }
  \bool_if:NTF \l_ralfix_subpart_bool
   {
    \int_compare:nT { \value{subpart} = 0 }
     {
      \stepcounter{part}
      \cs_gset:Npx \thepart { \thepart \exp_not:N \alph{subpart} }
      \cs_gset_eq:cc { c@part } { c@subpart }
     }
   }
   {
    \cs_gset_eq:cN { c@part } \ralfix_part_counter:
    \cs_gset_eq:NN \thepart \ralfix_part_thepart:
    \int_compare:nT { \value{subpart}=1 }
     {
      \use:c { @latex@warning@no@line }
       { You~had~only~one~subpart~%
         \int_to_Roman:n {\value{part}-1},~%
         check~your~input }
     }
    \setcounter{subpart}{0}
   }
  \stepcounter{Hpart} % keep hyperref happy
  \part[\l_ralfix_toctitle_tl]{#2}
 }

\cs_set_eq:Nc \ralfix_part_counter: { c@part }
\cs_set_eq:NN \ralfix_part_thepart: \thepart
\ExplSyntaxOff
\newcounter{Hpart}
\newcounter{subpart}

\begin{document}
\tableofcontents
%\pagebreak

\Part{First}
\Part{Second}

\Part[subpart]{Third a}
\Part[subpart,toctitle=Third toc b]{Third b}

\Part{Fourth}

\end{document}

在此处输入图片描述

注意:如果只有一个子部分,则添加警告。

答案3

我将创建一个完全不同的分段命令,如下所示:

\documentclass{article}
\usepackage{mwe}
\usepackage{hyperref}

\newcommand{\formatsubparthead}{\Large\bfseries}
\newcommand{\formatsubparttext}{\Large\bfseries}

\newcounter{subpart}[part]
\newcommand{\subpart}[1]{%
  \addtocounter{subpart}{1}%
  \vspace*{1em}\noindent %
  {\formatsubparthead Part \Roman{part}\alph{subpart}}%
  \par\vspace*{1em}
  {\formatsubparttext #1}%
  \addcontentsline{toc}{part}{\Roman{part}\alph{subpart}\quad #1}%
  \par\vspace*{2em}\noindent%
}

\begin{document}
\tableofcontents
\part{Part One}
\subpart{Something}
\lipsum[1]
\subpart{Something Else}
\lipsum[2]
\part{Part Two}
\lipsum[3]
\end{document}

输出:

输出

相关内容