Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zref-perpage doesn't work with include files #10

Open
FrankMittelbach opened this issue Apr 9, 2021 · 11 comments
Open

zref-perpage doesn't work with include files #10

FrankMittelbach opened this issue Apr 9, 2021 · 11 comments

Comments

@FrankMittelbach
Copy link

zref-perpage generates labels the form

\zref@newlabel{zref@1}{\abspage{1}\page{1}\pagevalue{1}}
\zref@newlabel{zref@2}{\abspage{1}\page{1}\pagevalue{1}}

but the label name zref@<number> is always starting from 1 without regard to the fact that you may only run one include file not everything. As a result you get duplicate labels.

Example

\begin{filecontents}[overwrite]{zref1.tex}
  Test\footnote{A}
  Test\footnote{B}
  \newpage
  Test\footnote{C}
\end{filecontents}

\begin{filecontents}[overwrite]{zref2.tex}
  Test\footnote{A2}
  Test\footnote{B2}
  \newpage
  Test\footnote{C2}
\end{filecontents}



\documentclass{article}

\usepackage[perpage,user]{zref}

\zmakeperpage{footnote}

% \includeonly{zref2}       % <--- uncomment on a 2 run to get the problem

\begin{document}

\include{zref1}
\include{zref2}

\end{document}

I haven't checked but if these labels are also used elsewhere by zref you will have the same problems with other modules of zref.

@u-fischer
Copy link
Contributor

zref explicitly excludes the unique counter used here from the standard resets:

\ZREF@Robust\def\zref@require@unique{%
  \@ifundefined{c@zref@unique}{%
    \begingroup
      \let\@addtoreset\ltx@gobbletwo
      \newcounter{zref@unique}%
    \endgroup

Your example works if one get around this restriction by defining the counter before loading zref:

\newcounter{zref@unique}%
\usepackage[perpage,user]{zref}

But I have currently no idea why zref doesn't simply use \newcounter.

@FrankMittelbach
Copy link
Author

well not doing this ensure that the counter is indeed unique across the document run, but why that is a virtue or needed is not clear to me.

If you have include files and in file A you get more labels then you get duplicates on file B which means you need to rerun file B. That may be the only reason why you have to rerun it, so perhaps he tried to avoid this, but now you get always duplicates whenever you run only some include files, so without it it is totally broken.

What could perhaps work is to add some safety value at the end of an include file so that the counter saved is not say 65 but
100 and 123 -> 200 and so on (ie starting the next file on the next 100 (or 1000)). That way there is some safety buffer avoiding unnecessary reruns).

@u-fischer
Copy link
Contributor

Hm, simply adding a value would shift all the following labels, wouldn't it? So you would have to rerun the files to get them right again. Rounding up to the next full 100 could probably work.

@FrankMittelbach
Copy link
Author

you have to round to the next 100 (or 200) for sure, otherwise the problem would persist if file A gets additional labels: you want to be able to absorb them (usually) without the need to rerun B.

If this counter is only used for per page values then rounding to the next 100 is probably fine. That means you get an unnecessary rerun when the page count of a chaper just crosses 100 pages (with footnotes) which will not happen often.

@u-fischer
Copy link
Contributor

Something like this?

\makeatletter\ExplSyntaxOn
\AddToHook{include/after}{\setcounter{zref@unique}{\fp_to_int:n{ceil(\c@zref@unique +100,-2)}}}
\makeatother\ExplSyntaxOff

@FrankMittelbach
Copy link
Author

I fear not but perhaps that means we have a thought error in the include set of hooks. If an include file is not loaded then non of its hooks are iare executeded.

@u-fischer
Copy link
Contributor

I thought I had tried it, but perhaps I got confused with the aux-file, I will check again.

@FrankMittelbach
Copy link
Author

it works as documented the hook is not executed if the include is bypassed, but this is arguably wrong. One could argue that the include hooks should always be executed and if on wants something only executed when the file is loaded then the corresponding file hooks could be used (which also exist), doing that would give extra flexibility compared to what is offered now.

@u-fischer
Copy link
Contributor

Actually I think it works if expected if you additionally add if one allow the counter to add to the ckpt by remove the \let, or by doing

\newcounter{zref@unique}%
\usepackage[perpage,user]{zref}
\begin{filecontents}[overwrite]{zref1.tex}
  Test\footnote{A}
  Test\footnote{B}
  \newpage
  Test\footnote{C}
\end{filecontents}

\begin{filecontents}[overwrite]{zref2.tex}
  Test\footnote{A2}
  Test\footnote{B2}
  \newpage
  Test\footnote{C2}
\end{filecontents}

\begin{filecontents}[overwrite]{zref3.tex}
  Test\footnote{A3}
  Test\footnote{B3}
  \newpage
  Test\footnote{C3}
\end{filecontents}

\documentclass{article}
\newcounter{zref@unique}
\usepackage[perpage,user]{zref}

\zmakeperpage{footnote}
\makeatletter\ExplSyntaxOn
\AddToHook{include/after}
{\setcounter{zref@unique}{\csname fp_to_int:n\endcsname{ceil(\c@zref@unique +100,-2)}}}
\makeatother\ExplSyntaxOff
 
% \includeonly{zref2}       % <--- uncomment on a 2 run 
\begin{document}

\include{zref1}
\include{zref2}
\include{zref3}

\end{document}

@FrankMittelbach
Copy link
Author

ok, my bad, it does work because when hook is executed before the checkpoint is written (I thought the after one comes later), so yes it would do.

@car222222
Copy link

@FrankMittelbach Good that this can be made to work as needed.

But probably good to note here that the include mechanism nevertheless needs to be analysed and revised sometime soon, in connection with 'attributes and cross-refs' more generally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants