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!
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:
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?


\printProofssomewhere in an appendix file, and my goal is to write inside\printProofsa 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\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 theprintProofscommand,a label that points to the current section. What do you mean here by "correct your code"? – tobiasBora Jul 15 '20 at 12:02Here is my appendixis 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\printProofsinto 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\printproofscommand generate also the\sectioninfo too? Then you can control the placement of the section label. – Willie Wong Jun 10 '21 at 18:40\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\addtocounter, why is it supposed to make it work? – tobiasBora Jun 11 '21 at 14:54\addcounterreduces the section counter by one. The\refstepcounterincrease the section counter by 1 and sets the reference anchor to be the section counter. So\labelthat follows will use the section counter. For plain LaTeX this is good enough. But if you want to use packages likehyperreforpagerefthen 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