# hyperref and repeating things counted by \refstepcounter

I try to write document, using hyperref package, which has elements of sectioning repeated several times with the same number.
These things are counted by \refstepcounter.

In the example document I only have section 1 several times.
In the real life case it is not only about \sections. There are subsections, numbered exercises and equations and the like also.

I know you must not have two destinations with same name. So in the example document \theHsection is redefined.
This seems to work out.

The page counter is reset also. As a consequence I must redefine \thepage and all indexing-macros which rely on page-destinations. \thepage after redefining uses \texorpdfstring to add \Person-string to every page number in the pdf that is not part of the displayed text. This is not optimal because it besides changing names of page-destinations also changes visible text-phrase for page-numbes in bookmarks and in bottom of frame of pdf-viewer's window for displaying the document.

So in the example below I do a lot of patching.

\documentclass{article}
\usepackage{refcount, makeidx}
\usepackage{hyperref}
\makeatletter

\newcommand\savecounterstolabel[1]{%
\begingroup
\def\@elt##1{\space\space\string\localsetcounter{##1}{\number\value{##1}}\@percentchar^^J}%
\protected@edef\@currentlabel{\@percentchar^^J\cl@@ckpt}%
\label{#1}%
\endgroup
}%
\@ifdefinable\localsetcounter{%
\DeclareRobustCommand\localsetcounter[2]{%
\csname c@#1\endcsname=#2\relax
}%
}%
\newcommand\locallyrestorecountersfromlabel[1]{%
\getrefbykeydefault{#1}{}{}%
}%
\newcommand\myhyperpage[2]{%
}%
\newcommand\patch@@wrindex{%
\def\@@wrindex##1|##2|##3\\{%
\if@filesw
\ifx\\##2\\%
\protected@write\@indexfile{%
\let\thepage\thepagecopy
\let\savedthepage\relax
}{\string\indexentry{##1|myhyperpage{\Person}}{\savedthepage}}%
\else
\HyInd@@@wrindex{##1}##2\\%
\fi
\fi
\endgroup
\@esphack
}%
}%

\newcommand\namelist{Aldrich,Brenda,Charles,Demelza,Emerson,%
Farrah,Gilford,Harmony,Irving,Jocelyn,Kenton,%
Lindsay,Manley,Nara,Ogden,Pamela,Quentin,%
Roberta,Stan,Tanja,Ulysses,Veronica,Wayland,%
Xanthis,York,Zilla}

\makeindex

\begin{document}
\vspace*{-3cm}%
\tableofcontents
\newpage
\savecounterstolabel{LabelBeforeBranchingTheText}%
\expandafter\@for\expandafter\Person\expandafter:\expandafter=\namelist\do{%
\begingroup
\locallyrestorecountersfromlabel{LabelBeforeBranchingTheText}%
% This is my current workaround for page-numbers/page-destinations
% but I would like not to need to resort to \texorpdfstring
% because this does write \Person's expansion not only to
% destination-names but also to page numbers displayed as
% part of visible phrases of bookmarks or displayed in the
% frame bottom of the window where the pdf-viewer displays
% the document.
\let\savedthepage=\thepage
\def\thepagecopy{\savedthepage.\Person}%
\def\thepage{\savedthepage\texorpdfstring{}{.\Person}}%
\patch@@wrindex
% I would like a hook so that this is not only done for
% section counter but for all counters:
\let\SavedTheHsection=\theHsection
\def\theHsection{\Person.\SavedTheHsection}%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\null\hfill(Sheet for \Person)
\section{First section for \Person}
\label{FirstSectionFor\Person}%
Text\index{text} of first section for \Person
\newpage
\endgroup
}%
\expandafter\@for\expandafter\Person\expandafter:\expandafter=\namelist\do{%
\noindent\hyperref[FirstSectionFor\Person]{%
}\smallskip\\%
}%
\newpage
Some text\index{text}.
\printindex
\end{document}


Question 1: Is there a better way of handling this page anchor issue?

Question 2: By now I have to redefine \theH... manually for each counter that is incremented by \refstepcounter within the things that get repeated.

Is it possible in hyperref to provide a prefix only in some places which gets added to all destinations created in these places and which is included when the names of these destinations get written to auxiliyary files (for example by a \label-command, or for the table of contents, or for the .out-file)?
To me seems this would be really really useful when it comes to repeating things.

If it is not already there: Which underlying macro(s) of hyperref do I need to patch for adding a hook, so the hook can (in local scope) be redefined for adding prefix to all destinations which is also written when destination-name is written to .aux-file/.toc-file/.out-file ? Are things implemented in hyperref in a way where adding such a hook would not be a problem regarding the chronologic ansynchronity of output routines? (Otherwise problem could be that hook not redefined any more at the time when output routine acts and \writes are written.) I tried reading the commented source of hyperref package, but it is too much for an evening after a day full of work.

I cannot use \HyperDestNameFilter/\HyperDestLabelReplace for adding prefix to all destinations that get created in a specific local scope because \HyperDestNameFilter/\HyperDestLabelReplace does not change what gets written to .aux/.toc/.out-files. This implies: Modification of \HyperDestNameFilter inside local scope will not be in effect outside loal scope. Therefore linking to destinations created inside the local scope from outside the local scope will not work out.

Answers and pointers to resources where I can find/conclude answers to my questions appreciated.

I am not doing slides. I create written exams where people basically have to solve same math-problems but with different coefficients.

Sincerely

Jewdokija

• Hyperref does support \theHpage for slides. Try \Hy@plainpagesfalse – John Kormylo Oct 7 '19 at 11:33

Usually the macro \hyper@makecurrent is used for creating the names of anchors/destinations of things that are counted in terms of \refstepcounter and saving these names as a macro called \@currentHref.

Perhaps you can patch \hyper@makecurrent to add a prefix or a postfix or whatever.

"Usually" because there are exceptions. E.g., packages that provide their own ways of supporting the loading of the hyperref-package.

Besides this there are named destinations etc where \hyper@makecurrent is not used.
E.g., the page-anchors that can be linked via \hyperpage.
E.g., destinations created in terms of \hyperdef and referenced in terms of \hyperref.
E.g., destinations created by means of \hypertarget and referenced by means of \hyperlink.
(Probably destinations created by means of \phantomsection. Maybe the names of these destinations get created and stored as \@currentHref without using \hyper@makecurrent, but I am not sure.)

Here you might need to patch macros that form the interface to the hyperref-drivers (things like \hyper@@anchor, \hyper@@anchorstart, \Hy@writebookmark, \hyper@link, \hyper@linkstart, etc).

When doing that—in order to prevent adding the prefix twice—you may need to implement distinguishing whether the name of the destination/anchor was already provided in a prior processing-step (e.g., via \@currentHref, e.g., due to reading things from an auxiliary-file where destination-names were written with prefix) or not.

All in all this will not be funny.

My personal opinion is:

For getting these things done properly, strictly dividing destinations/anchors/targets into different categories and reflecting these categories by means of the macros used for processing names of destinations/anchors/targets is needed.

Such categories could, e.g., be:

• Names of destinations/anchors that are placed into the document.
• Names of destinations/anchors/targets that refer to things outside the document. (e.g., "https://www.[...]" or "mailto:", e.g., labels imported by means of the xr-package).

Macro-categories for reflecting these categories could be:

• Macros which create names of destinations/anchors that get placed into the document and which provide that information to other macros. (Something like the \hyper@makecurrent-\@currentHref-mechanism.)
• Macros which process names of destinations/anchors that are placed into the document and where taking prefixes into account is not needed as taking prefixes into account already took place at the time of writing destination-names/anchor-names to .aux-file/to .toc-file/whatever.
• Macros which process names of destinations/anchors that got placed into the document and where prefixes etc still need to be taken into account.
• Macros which process links/references to things that are outside the document (e.g., "https://www.[...]" or "mailto:", e.g., labels imported by means of the xr-package).