# Resetting counter but changing its formatted string still gives pdfTeX warning (ext4)

A seemingly long question, but it's because I included 3 pieces of code - which are easy to understand.
Resetting a counter and changing its formatted string through \renewcommand\themycounter{\Alph{mycounter}} still gives the following warning when using \refstepcounter:

pdfTeX warning (ext4): destination with the same identifier (name{mycounter.1}) has been already used, duplicate ignored

Without hyperref, no warning is printed. I thought that with \alph I changed the counter sufficiently to use its values again for referencing, but apparently it does not suffice.

Question 1. How do I get rid of those warnings?

MWE

\documentclass{article}
\usepackage{hyperref}
\newcounter{mycounter}

\begin{document}

\refstepcounter{mycounter}\themycounter

\renewcommand{\themycounter}{\Alph{mycounter}}
\setcounter{mycounter}{0}

\refstepcounter{mycounter}\themycounter

\end{document}


And there is more...

Interestingly, when I use \counterwithin{mycounter}{section} (command provided by the package chngcntr), resetting the counter after \appendix gives no warning

\documentclass{article}
\usepackage{hyperref}
\newcounter{mycounter}
\usepackage{chngcntr}
\counterwithin{mycounter}{section}

\begin{document}

\section{Some section}
\refstepcounter{mycounter}\themycounter

\appendix
\setcounter{mycounter}{0}

\section{Some appendix}
\refstepcounter{mycounter}\themycounter

\end{document}


whereas \counterwithin{mycounter}{yourcounter} does give a warning, after changing \theyourcounter:

\documentclass{article}
\usepackage{hyperref}
\newcounter{mycounter}
\newcounter{yourcounter}
\usepackage{chngcntr}
\counterwithin{mycounter}{yourcounter}
\setcounter{yourcounter}{1}

\begin{document}

\refstepcounter{mycounter}\themycounter

\renewcommand{\theyourcounter}{\Alph{yourcounter}}
\setcounter{mycounter}{0}

\refstepcounter{mycounter}\themycounter

\end{document}


Question 2. How can I change yourcounter in a similar way as how section is changed by \appendix so as to avoid the warning?

With hyperref you should also redefine the internal hyperref representation:

\documentclass{article}
\usepackage{hyperref}
\newcounter{mycounter}

\begin{document}

\refstepcounter{mycounter}\themycounter

\renewcommand{\themycounter}{\Alph{mycounter}}
\renewcommand\theHmycounter{\Alph{mycounter}}
\setcounter{mycounter}{0}

\refstepcounter{mycounter}\themycounter

\end{document}

• Works perfectly! Apparently it is necessary to put \usepackage{hyperref} before the creation of the relevant counters. – Schneider Dec 24 '16 at 18:35