Tuesday, October 21, 2008

[TeX] Set a New Counter to Solve the Problem of Duplicate Identifier

Resetting counter can conflict with the hyperref package because hyperref will yield a warning message saying that there is a duplicate identifier. For example, if you have section 1.1, you will encounter this problem when you want to create a section 1.A for the appenix within a chapter.

\documentclass{book}
\usepackage{hypperref}
\begin{document}
\chapter{Introduction}
\section{What}
...
\renewcommand{\thesection}{\thechapter.\Alph{section}}
\setcounter{section}{0}
\section{Appendices}
\end{document}


If you don't use hypperref, this is not a problem. But if you do, the document will still be compiled correclty, but the hyperlink will not be pointing to the right target. So to solve this, you need to set a new counter.

\documentclass{book}
\usepackage{hypperref}
\begin{document}
\chapter{Introduction}
\section{What}
...
\newcounter{@appsec}
\setcounter{@appsec}{0}
\renewcommand{\thesection}{\thechapter.\Alph{@appsec}}
\stepcounter{@appsec}
\section{Appendices}
\end{document}


The step for \stepcounter makes the new counter works. Otherwise, it will show an blank space in the output.

No comments: