0

I'm wondering if there is a way to name the label with the same name of a section automatically. In the below example, instead of writing \label{page1}, I'd like to write something like \label{special_stuff} where special_stuff is a sort of a variable which assumes the value of the name of the first section before the command \label.

The only part of the code where it should be necessary to write page1 is \pageref{page1}.

\documentclass{article}

\begin{document}

\section{page1} \label{page1} page 1

\clearpage \section{page2} Here a reference to page \pageref{page1}.

\end{document}

Qrrbrbirlbel
  • 119,821
  • 1
    Possible duplicate https://tex.stackexchange.com/questions/62217/automatic-section-labeling (but usually not a good idea, this will cause more problems than it solves) – samcarter_is_at_topanswers.xyz Nov 29 '23 at 11:02
  • If you did that automatically all your references would break if you decide to that the name should better be "Page 1" instead of page1. And you would run into problems if you have two \section{Introduction}. – Ulrike Fischer Nov 29 '23 at 11:02
  • There are solution around, check https://tex.stackexchange.com/questions/246184/automatic-creations-of-labels (there is one more recent that I can't find...) but are you sure is a good idea? If you change the title of the section, even slightly, you have to change all the references... the title is for the reader, the label for the writer of a document, they're separate domains. – Rmano Nov 29 '23 at 11:03
  • 2
    That is, a good label is \label{sec:explaining-duck-feeding} for a section maybe titled "The pro and cons of feeding wild ducks with bread". A good label is something you remember easily and you don't change (ever). My labels are often in Italian... even when writing an English document. – Rmano Nov 29 '23 at 11:06
  • Thank you very much for your suggestions! – Gennaro Arguzzi Nov 29 '23 at 11:07

0 Answers0