0

I have a package that would like to put a reference to a section. The package cannot put the label right after the section title, so for now it just drops a \label{sec:mysection}. The problem is that if an equation is defined before, then (using \autoref{sec:mysection}) the label points to the previous equation, not to the section.

Using \cref{sec:mysection} solves one part of the issue: the text is fine, but the link still points to the equation. Any idea how to solve that?

Any idea?

Thanks!

enter image description here

MWE:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}

\usepackage{hyperref} \usepackage{cleveref}

\begin{document}

\section{My section}

With autoref, the name is bad, and the link is bad: \autoref{section:shouldbeappendixA}.\ With cleverref, the name is good, but the link is bad: \cref{section:shouldbeappendixA}.

\appendix \section{Proofs} Here is my appendix \vspace{3cm} \begin{equation} 1+1=2 \end{equation} \vspace{3cm}\ I would like this label to point to the heading of the current section, not to the last equation I wrote\label{section:shouldbeappendixA}. But I don't want to move the label code, because it's inserted automatically by a script.

\end{document}

EDIT 2021: Explanation of the original problem

To clarify the original problem I am having, I wrote this library which allows a user to move proofs in appendix. Basically, a user writes a code like:

\section{My section A}
\begin{thmE}[Title][end,category=sectionA]
  My first theorem
\end{thmE}
\begin{proofE}
  My first proof
\end{proofE}

\section{My section B} \begin{thmE}[Title][end,category=sectionB] My second theorem \end{thmE} \begin{proofE} My second proof \end{proofE}

\section{Appendix: proof of section A} Here are the proofs of section A: \printProofs[sectionA]

\section{Appendix: proof of section B} Here are the proofs of section B: \printProofs[sectionB]

Then, the final output will be:

enter image description here

Internally, the library creates one file per category (i.e. groups of proofs). This file, later included via \printProof[nameCategory], will contain for each proof in this category, something like:

\label{proofsection:prAtEndii}
\begin{proof}[Proof of \autoref{thm:prAtEndii}]\phantomsection\label{proof:prAtEndii}
  My first proof
\end{proof}

Therefore, if I have 2 libraries in the category, the included code will be:

\label{proofsection:prAtEndii}
\begin{proof}[Proof of \autoref{thm:prAtEndii}]\phantomsection\label{proof:prAtEndii}
  My first proof
\end{proof}
\label{proofsection:prAtEndiii}
\begin{proof}[Proof of \autoref{thm:prAtEndiii}]\phantomsection\label{proof:prAtEndiii}
  My third proof
\end{proof}

Then, the link to the section containing the proof is done via \autoref{proofsection:prAtEndii} or \cref{proofsection:prAtEndii} right after writing the theorem. The problem is that the label proofsection:prAtEndii may not point really to the first section because it won't be written right after the section (this is the case for the label proofsection:prAtEndiii in the above example).

One solution may be to create one more file internally containing only the labels like \label{proofsection:prAtEndii}\label{proofsection:prAtEndiii}, create another function like \addLabelToSection[nameOfCategory], and ask to the user to include it in the appendix section via something like:

\section{Appendix: proof of section A}
\addLabelToSection[sectionA]
Here are the proofs of section A:
\printProofs[sectionA]

but I don't really like this approach because the user will have to write two commands to print the proofs instead of one. Moreover, if at some point they change the name of the category while forgetting to replace the name also in the \addLabelToSection command, then the references will be missing, producing mystic errors like Can't find label proofsection:prAtEndii... which is not a great experience for the user. Is there a way to get rid of this burden?

tobiasBora
  • 8,684
  • better think about a better way to label the section. While it would be possible to guess the destination name, it is quite unclear if your example really reflects the real use case. – Ulrike Fischer Jul 14 '20 at 08:34
  • @UlrikeFischer : What do you mean? Basically my package is just called with something like \printProofs somewhere in an appendix file, and my goal is to write inside \printProofs a code to reliably create an anchor to the current section automatically. I provided a command to let the user customize the name of the anchor, but ideally I would prefer my package to automatically create a label of the current section. – tobiasBora Jul 15 '20 at 09:10
  • I did understand what your goal is. But hyperref uses an internal name for the destination anchor, this name depends in parts on document settings. hyperref doesn't keep a list of the names in memory - at the next counter (e.g. at the equation) this name is overwritten. To retrieve it later one would either have to change hyperref to keep track of all names, or guess the name, with the danger to guess wrong. – Ulrike Fischer Jul 15 '20 at 09:19
  • So basically you say "this is not possible with hyperref". But wouldn't it be possible with another package? For example, cleveref seems to guess properly the name of the section, only the link is wrong, so I guess cleveref is not too far from the solution... – tobiasBora Jul 15 '20 at 09:22
  • certainly it is possible, if you write the right amount of code. But I don't see really why it should be needed. It looks as if your code to create the latex document is wrong if it doesn't place the labels in the right place. Why do you expect hyperref to help you with some work-around instead of correcting your code? – Ulrike Fischer Jul 15 '20 at 09:28
  • Why do you mean by correcting my code? I don't have any code that creates the whole latex document, it's the goal of the user to write the different sections... Then, whenever the user wants to print the proofs collected in previous sections, he writes \printProofs (usually in appendix, but it's not required), but I don't ask the user to write that command right after the section title, because he may want to write some introduction text.And then I don't see how to create, from the printProofs command,a label that points to the current section. What do you mean here by "correct your code"? – tobiasBora Jul 15 '20 at 12:02
  • assuming that the text starting with Here is my appendix is user text. Why don't your user doesn't add the label there? Then it would be at the right place. Or why don't you offer some interface like an optional argument somewhere and your code then adds it as \label to the \section command? – Ulrike Fischer Jul 15 '20 at 12:07
  • Sure, I could ask to the user to label himself the section... But I don't really like this idea for several reasons: 1) it complicates a bit the library, as the user needs to use a fixed label for the sections, and it may be hard for him to maintain (what if user decides to move the \printProofs into another section, but forgets to move the label?). The fact that the library can be used to insert proofs in different sections still increases the complexity of maintainability for the user 2) if a co-author is not aware of the role of the label, he may remove it thinking that it's not used. – tobiasBora Jul 15 '20 at 12:17
  • You second solution may be interesting, but not sure to understand it fully. Do you mean I should redefine the \section command to automatically add the label for the user? It may work, even if I'm a bit hesitant to touch the section command which is quite fundamental, but could work. If yes, I would need a way to create "link synonyms" I guess. Is there a way to say "label myappendix is a synonym for label abcd"? – tobiasBora Jul 15 '20 at 12:19
  • look I have no idea how you autogenerate your code and which part is from the user or not, so I can't make concrete suggestions. I only think that having the label in the wrong place is not good - apart from the wrong link, as soon as your user use \pageref somewhere it will be wrong too. So you should develop something that allows your user to add the label at the right place. – Ulrike Fischer Jul 15 '20 at 12:33
  • Why don't you let your \printproofs command generate also the \section info too? Then you can control the placement of the section label. – Willie Wong Jun 10 '21 at 18:40
  • @WillieWong Because I want to allow my users to write stuff before the proofs (like in the example), or even put several \printProofs inside a single section. – tobiasBora Jun 11 '21 at 13:32
  • Both can be solved using key values and arguments. You are already taking an input the sections of proofs to print: just allow it to be a comma separated list of which proofs to print. You can add another argument for the introductory text to print. Making the command print the section heading has the advantage that it will be in line with other commands that print stuff (like the bibliography and the indices). – Willie Wong Jun 11 '21 at 14:27
  • What if instead of printing \label{proofsection:prAtEndiii} you place in front of it \addtocounter{section}{-1} and \refstepcounter{section}? This way the number will be correct but the hyperref link will be off. – Willie Wong Jun 11 '21 at 14:33
  • @WillieWong I guess I could create something like you say, with a command that accepts the section inputs, but it complicates the command quite a bit since I need to give it the name of the section, the introduction, accept list of inputs... Moreover, I also need to find a method to allow the user to put text in between multiple printproofs in the same section, and also I'm afraid that I may have some troubles if the introduction itself writes some proofs in the same category. – tobiasBora Jun 11 '21 at 14:52
  • @WillieWong Also, if I want to write the proofs at the end of the current section, it means that I need to embrace the whole section with my command, which is quite error prone and dirty since it will indent the whole section... Btw, I'm not sure to understand the trick with \addtocounter, why is it supposed to make it work? – tobiasBora Jun 11 '21 at 14:54
  • The \addcounter reduces the section counter by one. The \refstepcounter increase the section counter by 1 and sets the reference anchor to be the section counter. So \label that follows will use the section counter. For plain LaTeX this is good enough. But if you want to use packages like hyperref or pageref then this will put the anchor on the wrong location (right before the proof, as opposed to at section start), so hyperlinks and page references may point to the wrong page. – Willie Wong Jun 11 '21 at 15:41

1 Answers1

1

This is an extended comment with one idea of what you can do; I don't have the time to code it up for you but you seem to be proficient around TeX.

The idea is basically to hook into what \label does: it prints something into the .aux file based on the value of \@currentlabel, which is set by \refstepcounter which itself is implicitly called by \section or many environments, and is modified by packages that much around such things (like \hyperref). So what you want to do is to swap out the value of \@currentlabel on the sly.

One way to do this is to patch \section with something like (untested, only to illustrate concept, and may have bugs)

\let\old@tobias@section\section
\renewcommand\section[1]{\old@tobias@section{#1}\let\tobias@stored@label\@currentlabel}

And then, when you generate your proof sections and preparing them for printing, do

\let\@currentlabel\tobias@stored@label
\label{proofsection:...}

so when you do the labeling, the most recently stored section label is actually called.


The code above will not work perfectly as written, since you also have to handle \thepage which is also used in constructing the labels (see How does \@currentlabel work? How does it connect to sectioning commands / headings?).

The design of LaTeX's \label really is intended to put a label exactly where it is sitting in your source code, with the most recently \refsteped counter. Solving the issue with the latter is relatively easy; you can cheat by \refsteping the appropriate counter yourself or muck around with \@currentlabel like suggested above. Convincing LaTeX that your label really doesn't want to be where you put it, but elsewhere on a different page, will take more work, and can interact funnily with other ref packages (e.g. hyperref).

Willie Wong
  • 24,733
  • 8
  • 74
  • 106
  • Thanks for your answer, sorry I'm not sure why I did not replied before. I wanted to avoid patching sections because I'm afraid to do it the wrong way, especially given that each document class may implement sections differently (and I need to patch sections, but also subsections, chapters... in case the user wants to put it somewhere else), but I guess you are right, it's certainly the best solution. Thanks a lot! – tobiasBora Jan 27 '22 at 14:51