2

I want to copy the label command locally to preserve its use, for example when other packages might modify its behavior later on. For this reason, I would like to copy label using let (or similar) rather than def.

The following code works.

\documentclass{article}

\usepackage{amsmath} \usepackage{showkeys}

\NewCommandCopy{\mylabel}{\label}

\begin{document}

I tried to label here. \mylabel{test}

\newpage

I tried to reference here. \pageref{test}

\end{document}

However, I would also like to use hyperref, but it breaks things. The following code does not work.

\documentclass{article}

\usepackage{amsmath} \usepackage{showkeys}

\NewCommandCopy{\mylabel}{\label}

\usepackage{hyperref}

\begin{document}

I tried to label here. \mylabel{test}

\newpage

I tried to reference here. \pageref{test}

\end{document}

More explicitly, I am getting some runaway argument error from a hyperref internal function.

Is there a way to copy the label command with hyperref support?

This question seems related to this question, but simply using \let as suggested is not working for me.

  • 2
    Your label function doesn't produce a label as needed by hyperref. – Ulrike Fischer Mar 20 '22 at 16:59
  • @Ulrike Fischer How would I make it do so? – Derive Foiler Mar 20 '22 at 16:59
  • 3
    Imho your plan to "preserve" the old label is simply wrong. If a package redefines \label then probably because it needs the changed definition, undoing that is bound to break something. – Ulrike Fischer Mar 20 '22 at 17:05
  • 3
    here you need to copy it after you have loaded hyperref you can't use the original label or pagref definitions after you load hyperref so saving them does nothing useful. – David Carlisle Mar 20 '22 at 17:07
  • @David Carlisle I have tried copying after hyperref is loaded, and it gives the same error. See https://www.overleaf.com/read/jnhmqpqzynxt – Derive Foiler Mar 20 '22 at 17:08
  • @Ulrike Fischer I am dealing with an environment (namely, restatable) which kills the label function internally. I want to be able to label it anyways (I am being very careful about this). – Derive Foiler Mar 20 '22 at 17:11
  • 2
    It works for me if I put \NewCommandCopy{\mylabel}{\label} after begin document (ie after hyperref is set up) (you need to delete the aux file or ignore errors when you first switch) but better not to do this, – David Carlisle Mar 20 '22 at 17:14
  • Be aware of commands like \addtocontents or \@outputpage which do \let\label\@gobble but which do not \let\mylabel\@gobble. Same with hyperef-thingies like \texorpdfstring. – Ulrich Diez Mar 20 '22 at 17:50
  • @UlrichDiez yes anything that patches \label will need to be modified to patch \mylabel as well. Not making the copy will certainly be preferable. – David Carlisle Mar 20 '22 at 18:29
  • @DavidCarlisle Probably with \mylabel a mechanism is feasible where instead of the saved definition of \label the current \label-command is carried out (no matter what meaning exactly it currently has) in case \label does not have its usual meaning and therefore currently seems to be patched. Could turn things into confusing patchwork. – Ulrich Diez Mar 20 '22 at 18:37
  • I am dealing with an environment (namely, restatable) which kills the label function internally I suppose this is done because you need to ensure that duplicate labels are avoided when things are "restated". – Ulrich Diez Mar 20 '22 at 18:42
  • hyperref patches both the command \label and the commands for cross-referencing so that more data than just page-number and value of the counter stepped as the last one before placing the label is associated to a cross-referencing-label. If you do \NewCommandCopy{\mylabel}{\label} before hyperref is loaded, then \mylabel creates cross-referencing-labels where hyperref's enlargement of the set of data handled by the cross-referencing-mechanism is not taken into account and therefore cross-referencing-commands' extraction of data associated to a cross-referencing-label goes wrong. ... – Ulrich Diez Mar 21 '22 at 09:07
  • ... associated to a cross-referencing-label goes wrong. Be aware that with many aspects of the hyperref-package things are delayed until the begin-document-hook is carried out. This is the case, e.g., with carrying out modifications to kernel-macros like \label and \ref and \pageref. – Ulrich Diez Mar 21 '22 at 09:10
  • The main problem is that hyperref changes the number of arguments used by \newlabel which is called by \label (via the aux file). You can always use \ref* or \pageref* to remove the hyperlink. – John Kormylo Mar 21 '22 at 14:43

0 Answers0