7

I would like to have my parts numbered like this:

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

I have written some code which works fine. But when using hyperref, I get this warning: destination with the same identifier (name{part.1}) has been already used, duplicate ignored, because I reset the counter to zero for the sub-numbering.

Question

Is there any way to achieve the same result, but without reseting the part counter (and without producing hyperref-warnings)?

Code

\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}
  • if you can, I would look at writing something like a \subpart command to do this for you, and potentially not including it in the toc (or is the TOC a necessary feature?) – Sean Allred Apr 17 '13 at 13:30
  • It has to be in the TOC, as the part is just "splitted into two" – Peater de Xel Apr 17 '13 at 13:33

3 Answers3

7

Here's a solution inspired by subequation. You use the command \Part that takes an optional argument in which you can specify the subpart option for those parts you want to share the main number. You can also specify toctitle=... for a different title that should go in the TOC.

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

enter image description here

Note: added a warning if there's a single subpart.

egreg
  • 1,121,712
  • This is really nice, but "heavy"... Thank you for providing (another) xparse-solution, I learn a lot from your answers. – Peater de Xel Apr 20 '13 at 11:30
  • @ralfix If you look closely, it's not very different in implementation from Ulrike's, but it spares you from complicated code in the body of the document. – egreg Apr 20 '13 at 12:12
3

To make hyperref happy with your own solution it is enough to redefine \theHpart so that it gives unique values, e.g.:

\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}
Ulrike Fischer
  • 327,261
1

I would create a whole other sectioning command, like so:

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

Output:

output

Sean Allred
  • 27,421