Package hyperref
needs the counter values to generate unique anchor names. But there are counter values that are not unique. For example, you have several sections with number 1
.
Because of this, hyperref
has introduced \theH<counter>
and prefers it over \the<counter>
for use in anchor names. \the<counter>
might contain weird or duplicate values. As long as \theH<counter>
expands to a simple unique string, hyperref
is happy.
The following example provides unique definitions for \theHsection
:
\documentclass{report}
\usepackage{hyperref}
\usepackage{bookmark}
\bookmarksetup{
numbered,
open
}
\renewcommand*{\thesection}{\arabic{section}}
\begin{document}
\tableofcontents
\chapter{First numbered chapter}
\section{AB}
\section{BC}
\section{DE}
\chapter*{First unnumbered chapter}
\addcontentsline{toc}{chapter}{First unnumbered chapter}
\setcounter{section}{0}
\renewcommand*{\theHsection}{chX.\the\value{section}}
\section{FG}
\section{HI}
\section{IJ}
\chapter*{Second unnumbered chapter}
\addcontentsline{toc}{chapter}{Second unnumbered chapter}
\setcounter{section}{0}
\renewcommand*{\theHsection}{chY.\the\value{section}}
\section{KL}
\section{MN}
\section{NO}
\renewcommand*{\theHsection}{\theHchapter.\the\value{section}}
\chapter{Last numbered chapter}
\section{PQ}
\section{RS}
\section{TU}
\end{document}
As David Carlisle says, rename your labels. This can easily be done using the Stream Editor, sed
for example.
Let's say that you have one chapter file about Lions, and one about Zebras; in both chapter files you have used the labelling convention you described, something like the following:
lions.tex
\chapter{Lions}
\section{Intro}\label{sec:intro}
\section{Results}\label{sec:results}
Here's a reference to \ref{sec:intro} and \ref{sec:results}.
zerbras.tex
\chapter{Zebras}
\section{Intro}\label{sec:intro}
\section{Intro}\label{sec:intro}
\section{Results}\label{sec:results}
Here's a reference to \ref{sec:intro} and \ref{sec:results}.
You can use sed
to search and replace each \label
and \ref
in each of the files so that all of your labels and references change appropriately
sed -i 's/\\\(label\|ref\){\([^}]*\)/\\\1{lions:\2/g' lions.tex
sed -i 's/\\\(label\|ref\){\([^}]*\)/\\\1{zebras:\2/g' zebras.tex
Now your files look like the following
lions.tex (new)
\chapter{Lions}
\section{Intro}\label{lions:sec:intro}
\section{Results}\label{lions:sec:results}
Here's a reference to \ref{lions:sec:intro} and \ref{lions:sec:results}.
zebras.tex (new)
\chapter{Zebras}
\section{Intro}\label{zebras:sec:intro}
\section{Intro}\label{zebras:sec:intro}
\section{Results}\label{zebras:sec:results}
Here's a reference to \ref{zebras:sec:intro} and \ref{zebras:sec:results}.
Understanding \(label\|ref){([^}]*)/\\1{lions:\2/g
The basic syntax I have used is s/old/new/g
to substitute 'old' with 'new'. The g
flag says to do it globally. Let's break the above expression down into parts:
\\\(label\|ref\)
matches \label
or \ref
and stores the result into memory, to be used later as \1
. Note that we need to use a \
to escape special characters
{\([^}]*\)
matches the stuff inside {...}
, but does so in a non-greedy way. It is very important for this regexp not to be greedy; if were greedy, then when operating upon the expression Here's a reference to \ref{sec:intro} and \ref{sec:results}
it would match
sec:intro} and \ref{sec:results}
which is not what we intend!
\\\1{lions:\2
is the replacement text, using \1
and \2
as the match that has been stored into memory.
Best Answer
Since you are using a
\section*
for it, there are no complications for the table of contents.To address both a possible criticism of non-generality as well as the suggestion of egreg, one could implement the above idea as shown below:
In this way, a desire at some future point to change the format for all such starred sections would require only a single-line change to the document, that is, the definition of
\myformat
.