Post by GLPost by Ulrich D i e zIf in this case \label and \ref are used, make sure to include the
value of the counter \examcountersequence into the name of
each label in order to avoid duplicate-reference-errors.
Yes of course. Global counters (for Problems or Exercices for example)
should be cleaned as well etc. if there is any...
According to the LaTeX2e-sources, file m:ltcounts.dtx, there is
the macro \cl@@ckpt which holds an \@elt-list for all counters
defined via \newcounter.
It is used for taking checkpoints for the \include-system.
You can (ab)use it for getting a checkpoint into a macro
for later resetting counters.
Heiko Oberdiek provided \HyperDestNameFilter for the hyperref-
package which can be used for providing a unique prefix to
each anchor-name, ensuring no duplicate anchors will be created.
Getting bookmarks right is a bit of fun as \HyperDestNameFilter
by default is not applied when writing bookmarks to the .out-file
but is applied internally by the \BOOKMARK-command when
reading the .out-file which makes maintaining bookmarks
with different anchor-name-prefixes impossible. But this can
be changed easily.
Putting the pieces together, I can propose the following template:
\documentclass{article}
\usepackage{hyperref}
\makeatletter
%% put \include-checkpoints into macros:
\newcommand*\UDptelt[1]{%
\noexpand\setcounter{#1}{\the\@nameuse{c@#1}}%
}%
\newcommand\UDckpttomacroglobal[1]{%
\begingroup
\let\@elt\UDptelt
\xdef#1{\cl@@ckpt}%
\endgroup
}%
%% patch bookmarking:
\def\@@BOOKMARK[#1][#2]#3#4#5{%
\expandafter\edef\csname @count@#3\endcsname{%
\the\@***@counter
}%
\edef\@mycount{\the\@***@counter}%
\***@StepCount\@***@counter
\edef\@parcount{%
\expandafter\ifx\csname @count@#5\endcsname\relax
0%
\else
\csname @count@#5\endcsname
\fi
}%
\immediate\special{%
%%% !!! next line changed:
!outline #3;p=\@parcount,i=\@mycount,%
s=\ifx#2-c\else o\fi,t=#4%
}%
}%
\def\***@writebookmark#1#2#3#4#5{%
% section number, text, label, level, file
\ifx\WriteBookmarks\relax%
\else
\ifnum#4>\***@bookmarksdepth\relax
\else
%%% !!! next line changed:
\@@writetorep{#1}{#2}{\HyperDestNameFilter{#3}}{#4}{#5}%
\fi
\fi
}
\let\***@write=\***@write
\def\@@writetorep#1#2#3#4#5{%
\begingroup
\edef\***@tempa{#5}%
\ifx\***@tempa\***@bookmarkstype
\edef\***@level{#4}%
\ifx\***@levelcheck Y%
\@tempcnta\***@level\relax
\advance\@tempcnta by -1 %
\ifnum\***@currentbookmarklevel<\@tempcnta
\advance\@tempcnta by -\***@currentbookmarklevel\relax
\advance\@tempcnta by 1 %
\***@Warning{%
Difference (\the\@tempcnta) between bookmark levels is %
greater \MessageBreak than one, level fixed%
}%
\@tempcnta\***@currentbookmarklevel
\advance\@tempcnta by 1 %
\edef\***@level{\the\@tempcnta}%
\fi
\else
\global\let\***@levelcheck Y%
\fi
\global\let\***@currentbookmarklevel\***@level
\@tempcnta\***@level\relax
\expandafter\xdef\csname Parent\***@level\endcsname{#3}%
\advance\@tempcnta by -1 %
\edef\***@tempa{#3}%
\edef\***@tempb{\csname Parent\the\@tempcnta\endcsname}%
\ifx\***@tempa\***@tempb
\***@Warning{%
The anchor of a bookmark and its parent's must not%
\MessageBreak be the same. Added a new anchor%
}%
\phantomsection
\fi
\***@bookmarksnumbered
\let\numberline\***@numberline
\let\booknumberline\***@numberline
\let\partnumberline\***@numberline
\let\chapternumberline\***@numberline
\else
\let\numberline\@gobble
\let\booknumberline\@gobble
\let\partnumberline\@gobble
\let\chapternumberline\@gobble
\fi
%%% !!! next 2 lines changed:
\@ifundefined{***@XeTeXBigCharstrue}%
{}{\***@XeTeXBigCharstrue}%
\pdfstringdef\***@tempa{#2}%
%%% !!! next 2 lines changed:
\@ifundefined{***@SanitizeForOutFile}%
{}{\***@SanitizeForOutFile\***@tempa}%
%%% !!! next line changed:
% \***@filesw
\stepcounter{***@seq@number}%
\@ifundefined{@outlinefile}{%
}{%
%%% !!! next line changed:
\***@write\@outlinefile{}{%
\protect\BOOKMARK
[\***@level][\@bookmarkopenstatus{\***@level}]{#3}%
{\***@tempa}{\***@tempb}%
\@percentchar\space\***@seq@number
}%
}%
%%% !!! next line changed:
% \fi
\fi
\endgroup
}
%% Bookmarking-patching done.
\makeatother
\begin{filecontents*}{UDtest}
\section{\texorpdfstring{Exam}{Exam (sheet \examcountersequence)}}
That's the test/exam.
The value of \texttt{\string\examcountersequence} is \examcountersequence.
\subsection{some exercise}\label{\HyperDestNameFilter{some exercise}}%
This is an exercise.
\subsection{other exercise}
This is another exercise. Please use the result of exercise
\ref{\HyperDestNameFilter{some exercise}}.
\end{filecontents*}
\begin{document}
\begingroup
\UDckpttomacroglobal{\UDResetCounters}%
\newcommand\examcountersequence{0}
\loop
\edef\examcountersequence{\number\numexpr\examcountersequence+1\relax}%
\edef\HyperDestNameFilter##1{Sheet_\examcountersequence_##1}%
\UDResetCounters
\input{UDtest}%
\cleardoublepage
\ifnum \examcountersequence<10 \repeat
\endgroup
\end{document}
Ulrich