6

This question is tied to my other question about French section numbering using bis, ter, etc. The previous question gave me a method to number the different sections with the latin bis, ter, and others in sequential order.

Now I am trying to cross-reference these sections but the cross reference numbering is not working out correctly. Please consider the MWE below, which is slightly tweaked from the answer to the aforementioned question.

\documentclass{book}
\usepackage{xparse}
\usepackage{enumitem}
\usepackage[explicit]{titlesec}
\usepackage{lipsum}
\usepackage{calc}
\usepackage{totcount}
\usepackage{hyperref}

\ExplSyntaxOn
\seq_new:N \bislist
\seq_set_split:Nnn \bislist {;} {bis;ter;quater;quinquies;sexies;septies;octies;novies;decies}

\NewDocumentCommand {\bisprint} {m}
 {
  \seq_item:Nn \bislist {#1}
 }
\ExplSyntaxOff

\newtotcounter{bismax}
\newcounter{bis}
\stepcounter{bis}
\setcounter{bismax}{1}

\newcommand{\bisadd}{\stepcounter{bis} \setcounter{bismax}{\minof{4}{\maxof{\thebismax}{\thebis-1}}}}

\AtBeginDocument{\newlength{\bislength}
\settowidth{\bislength}{\textit{\bisprint{\totvalue{bismax}}}}
\addtolength{\bislength}{1.5em}}

\titleformat{\section}[block]{\normalfont\large\normalfont\filcenter}{\thesection. --- #1}{0.5em}{}[\setcounter{bis}{1}]

\renewcommand\thesection{\arabic{section}}

\titleformat{name=\section,numberless}[block]{\normalfont\large\normalfont\filcenter}{\arabic{section} \textit{\bisprint{\arabic{bis}}}. --- #1}{0.5em}{}[ \addcontentsline{toc}{section}{\protect\numberline{\arabic{section} \textit{\bisprint{\arabic{bis}}}}#1} \bisadd{}]

\makeatletter
\renewcommand*\l@section{\@dottedtocline{1}{1.5em}{\bislength}}
\makeatother

\begin{document}



\tableofcontents

\section{First section header}\label{Sec1}
\lipsum[5]
\section*{Second section header}\label{Sec1bis}
\lipsum[6]
\section*{Third section header}\label{Sec1ter}
\lipsum[7]
Let's reference Section \ref{Sec1bis} and \ref{Sec1ter} here.  This should have said "Section 1 \textit{bis} and Section 1 \textit{ter}."  You will see that the cross-references do not work out correctly.

\end{document}

This produces the following output:

No. 1
No. 2

As you can see, the last paragraph attempts to cross-reference Sections 1 bis and 1 ter, but the Latin ordinals aren't properly shown. I assume this has something to do with the \section* command, but I really don't know for sure.

3

Here's an idea with minimal impact on the usual commands: with \section+ you introduce a “bis” section. When a \section without suffix is scanned, the normal counter is stepped and the bis counter is reset.

\documentclass{book}

\usepackage{xparse}
\usepackage{titlesec}
\usepackage{hyperref}

\usepackage{lipsum}

\titleformat{\section}[block]
  {\normalfont\large\normalfont\filcenter}
  {\thesection. --- }
  {0em}
  {}

\ExplSyntaxOn
\cs_set_eq:NN \origsection \section

\RenewDocumentCommand{\section}{s t+ o m }
 {
  \IfBooleanTF{#1}
   { \origsection* { #4 } }
   {
    \stepcounter{Hsection}
    \IfBooleanTF{#2}
     {
      \addtocounter{section}{-1}
      \stepcounter{bis}
     }
     {
      \setcounter{bis}{1}
     }
    \IfNoValueTF{#3}
     { \origsection { #4 } }
     { \origsection [ #3 ] { #4 } }
   }
 }

\DeclareExpandableDocumentCommand {\bisprint} {}
 {
  \int_case:nnF { \value{bis} }
   {
    {0}{}
    {1}{}
    {2}{~bis}
    {3}{~ter}
    {4}{~quater}
    {5}{~quinquies}
    {6}{~sexies}
    {7}{~septies}
    {8}{~octies}
    {9}{~novies}
    {10}{~decies}
   }
   { ~ HEY! }
 }
\ExplSyntaxOff

\renewcommand{\thesection}{\arabic{section}\textit{\bisprint}}
\newcounter{bis}
\newcounter{Hsection}


\begin{document}



\tableofcontents

\section{First section header}\label{Sec1}
\lipsum[5]
\section+{Second section header}\label{Sec1bis}
\lipsum[6]
\section+{Third section header}\label{Sec1ter}
\lipsum[7]

Let's reference Section \ref{Sec1bis} and \ref{Sec1ter} here.  This should have said "Section 1 
\textit{bis} and Section 1 \textit{ter}."  You will see that the cross-references do not work out 
correctly.

\section{This should be 2}

\section+{This should be 2bis}

\end{document}

enter image description here

I leave to you further refinements.

  • I have other syntax that requires \usepackage[explicit]{titlesec} to work. Putting the [explicit] flag in makes the section headers in the body of the document disappear with your syntax. How can I avoid this? – grfrazee Sep 17 '15 at 20:36
  • @grfrazee My experience is that the explicit option is almost never needed. If you must use it, put #1 in the now empty pair of braces at the end of the \titleformat command. – egreg Sep 17 '15 at 20:40
  • Perfect. Thanks. I especially like that this solution doesn't redefine the \section*{} command, which was something that didn't quite sit well with me on my MWE. – grfrazee Sep 17 '15 at 20:43
2

You can change your numberless \titleformat to the following:

\makeatletter
\providecommand{\phantomsection}{}% To work with hyperref, if needed
\titleformat{name=\section,numberless}[block]
  {\normalfont\large\normalfont\filcenter}
  {\phantomsection% Set hyperref mark
   \protected@xdef\@currentlabel{\thesection~\textit{\bisprint{\arabic{bis}}}}%
   \arabic{section} \textit{\bisprint{\arabic{bis}}}. --- #1}
  {0.5em}
  {}
  [ \addcontentsline{toc}{section}{\protect\numberline{\arabic{section} \textit{\bisprint{\arabic{bis}}}}#1} \bisadd{}]
\makeatother

This updates \@currentlabel - used by the \label-\ref system to include your \bisprint-ed number.

enter image description here

The above also works as expected when hyperref is used.

  • I believe that all that's still needed to do in order to make this code work with hyperref is to change the two instances of \stepcounter to \refstepcounter. – Mico Sep 16 '15 at 20:15
  • Apologies, I forgot to add hyperref to my MWE. What would have to be done to get this to work with hyperref? – grfrazee Sep 16 '15 at 20:15
  • @Mico, thanks, I was typing my comment when you posted yours. I'll give that a try. – grfrazee Sep 16 '15 at 20:16
  • @grfrazee: See the minor update to the answer, which adds \phantomsection where needed. – Werner Sep 16 '15 at 20:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.