Graphics, Figures & Tablessetcounter to zero vs. hyperref error

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
j.zapal
Posts: 1
Joined: Wed May 27, 2009 2:43 pm

setcounter to zero vs. hyperref error

Post by j.zapal »

This is more of a posting solution rather than asking for help. I just solved following problem and would like to share the solution with others.

The problem is that when you reset counter to say tables to zero, hyperref package will give you error (having two or more tables with number one say). I faced this problem as I tried to number tables in my appendix as A1, A2 and so on.

Here is something that should solve the problem

Code: Select all

\newcounter{myc} %define new counter
\renewcommand{\thetable}{A\arabic{myc}} % use the new counter for tables
\let\otable\table
\let\endotable\endtable
\renewenvironment{table}{\addtocounter{myc}{1} \begin{otable}}{\end{otable}} % add to new counter as you initiate table enrironment
J

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

setcounter to zero vs. hyperref error

Post by localghost »

You should clarify the context in which this workaround is useful. From my point of view you worked out something for the article class. Other classes reset the float number by default. It's better to show the effect with a minimal working example (MWE) to make others able to comprehend it instantly.


Best regards and welcome to the board
Thorsten
grinder
Posts: 9
Joined: Sat Oct 24, 2009 3:02 pm

setcounter to zero vs. hyperref error

Post by grinder »

Hi! I am trying to do the same, but for sections. The documentclass is article. I am having problems with the bookmarks in the produced pdf. Specifically, my document consists of two parts, so I want the sections to be automatically reset in the start of each part. Currently, I am reseting the counter manually (\setcounter{section}{0}). If I don't do that the structure of the document is reflected in the bookmarks:
bookmarks01.jpg
bookmarks01.jpg (14.59 KiB) Viewed 10242 times
But if i do it, then the following problem occurs:
bookmarks02.jpg
bookmarks02.jpg (12.45 KiB) Viewed 10242 times
I am trying since yesterday, but to no avail :( Any ideas? Thank you in advance for any help.

PS If that helps, I am using texlive in Debian Unstable
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Re: setcounter to zero vs. hyperref error

Post by localghost »

The necessity of a MWE is also true for this problem.


Best regards and welcome to the board
Thorsten¹
grinder
Posts: 9
Joined: Sat Oct 24, 2009 3:02 pm

Re: setcounter to zero vs. hyperref error

Post by grinder »

I perfectly understand what the original poster has done. I am almost there (i 've done some progress), but the problem is that I don't know exactly which parameters of the section environment I need to look at. Any help?
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

setcounter to zero vs. hyperref error

Post by localghost »

grinder wrote:[...] I perfectly understand what the original poster has done. [...]
Good for you. You're one step ahead of me. Concerning your issue my request for a MWE remains.
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

setcounter to zero vs. hyperref error

Post by sommerfee »

BTW: I don't like the solution from the first post since re-defining the table environment could interfere with packages which do redefine the table environment as well. Furthermore the problem is that \theHtable must be unique for every table, so in my eyes it would make a more straight-ahead solution to redefine \theHtable to ensure this:

Code: Select all

\renewcommand\thetable{A\arabic{table}}
\renewcommand\theHtable{A\arabic{table}}
\setcounter{table}{0}
Here comes a MWE:

Code: Select all

\documentclass{article}
\usepackage{hyperref}

\begin{document}

\listoftables

\begin{table}
\centering A table
\caption{A table}
\end{table}

\appendix

\renewcommand\thetable{A\arabic{table}}
\renewcommand\theHtable{A\arabic{table}}
\setcounter{table}{0}

\begin{table}
\centering A table
\caption{A table}
\end{table}

\end{document}
Without the redefinition of \theHtable this would give an inproper hyperlink:

Code: Select all

pdfTeX warning (ext4): destination with the same identifier (
name{table.1}) has been already used, duplicate ignored
Axel
grinder
Posts: 9
Joined: Sat Oct 24, 2009 3:02 pm

setcounter to zero vs. hyperref error

Post by grinder »

Yeah baby, found it at last!!! :D Here it is:

Code: Select all

\newcounter{myc}[part] % define new counter which is going to be automagically reset at the start of each part
\renewcommand{\thesection}{\arabic{myc}} % use the new counter for sections
\let\osection\section % take a "snapshot" of the current state of section
\renewenvironment{section}{\stepcounter{myc}\osection} % add the new counter as you initiate a section. the new counter will be incremented by a step of 1.
Hope that the comments are going to help you understand the code. Cheers! ;)
User avatar
sommerfee
Posts: 503
Joined: Mon Apr 09, 2007 4:20 pm

setcounter to zero vs. hyperref error

Post by sommerfee »

grinder wrote:\renewenvironment{section}
Section isn't an environment so it's better to take \renewcommand instead.

HTH,
Axel
Post Reply