Automatic label generation from the title of a section may sound attractive, but mostly it is not, because it leads to lots of problems. This affects not only math mode, but macro expansion in general. In addition, all cross-references must be changed if a title is modified even minimally. This is almost as impractical as resolving all cross-references manually instead of automatically by \label/\ref.
Nevertheless, you can define substitutions for all sorts of macros to try to achieve it anyway. Here is a small example using LaTeX3:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{etoolbox}
\makeatletter
\patchcmd{@sect}% <cmd>
{@xsect}% <search>
{\SecLabel{#8}@xsect}% <replace>
{}{}% <successs><failure>
\makeatother
\ExplSyntaxOn
\cs_new:Nn \doc_label_set_sanitized:Nn
{
\str_set:Nn #1 { #2 }
\str_remove_all:Nn #1 { $ }
\str_replace_all:Nnn #1 { \epsilon } { epsilon }
\str_replace_all:Nnn #1 { \delta } { delta }
% Extend this replacement list according to your own needs.
}
\NewDocumentCommand { \SecLabel } { m }
{
\doc_label_set_sanitized:Nn \l_tmpa_str { #1 }
\label { sec: \l_tmpa_str }
}
\NewDocumentCommand { \SecRef } { m }
{
\doc_label_set_sanitized:Nn \l_tmpa_str { #1 }
\ref { sec: \l_tmpa_str }
}
\ExplSyntaxOff
\begin{document}
\section{The ($\epsilon$, $\delta$)-definition --- the rigorous approach}
Symbolically, $\displaystyle (\forall\ \epsilon > 0 ) , (\exists\ \delta > 0) , (\forall x \in \mathbb{R}) , (0 < |x - p| < \delta \implies |f(x) - L| < \varepsilon)$.
\section{Continuity and Differentiability}
... This can be trivially proved using any of the methods stated in section \SecRef{The ($\epsilon$, $\delta$)-definition --- the rigorous approach}.
\end{document}
However, if you want to do this for the complete possible mathematics, this could become more than time-consuming.
As an alternative, you also could replace all macros by their names and additionally remove, e.g., subscripts and superscripts:
\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{etoolbox}
\makeatletter
\patchcmd{@sect}% <cmd>
{@xsect}% <search>
{\SecLabel{#8}@xsect}% <replace>
{}{}% <successs><failure>
\makeatother
\ExplSyntaxOn
\cs_new:Nn \doc_label_set_sanitized:Nn
{
\str_set:Nn #1 { #2 }
\str_remove_all:Nn #1 { $ }
\regex_replace_all:nnN { [\_^] } { } #1 % replace each macro by its name
% and also remove subscripts and superscripts
}
\NewDocumentCommand { \SecLabel } { m }
{
\doc_label_set_sanitized:Nn \l_tmpa_str { #1 }
\label { sec: \l_tmpa_str }
}
\NewDocumentCommand { \SecRef } { m }
{
\doc_label_set_sanitized:Nn \l_tmpa_str { #1 }
\ref { sec: \l_tmpa_str }
}
\ExplSyntaxOff
\begin{document}
\section{The ($\epsilon^1$, $\delta$)-\emph{definition} --- the rigorous approach}
Symbolically, $\displaystyle (\forall\ \epsilon > 0 ) , (\exists\ \delta > 0) , (\forall x \in \mathbb{R}) , (0 < |x - p| < \delta \implies |f(x) - L| < \varepsilon)$.
\section{Continuity and Differentiability}
... This can be trivially proved using any of the methods stated in section \SecRef{The ($\epsilon^1$, $\delta$)-\emph{definition} --- the rigorous approach}.
\end{document}
See “The LaTeX3 Interface” for more information.
\start{document}to\begin{document}and load the packages you need for the commands you use. – cfr Sep 14 '23 at 05:21\label{sec:delta-epsilon-method} ... \ref{sec:delta-epsilon-method}. Maths in a label is going to spell trouble. If you usehyperref, you may also need to use the short title to give an alternative. – cfr Sep 14 '23 at 05:29Basically the only problem concerns using math in the section name, and the lack of packages and the typo
– Number Basher Sep 14 '23 at 07:21startis irrelevant\labelcontains information for the writer, the section title for the reader. If I change a section title (think a typo, or a better word) I do not want to change all the\refs... – Rmano Sep 14 '23 at 07:46A Macro fighting against command expansion could be part of a template, whereas writing a label cannot be part of a template
– Number Basher Sep 14 '23 at 08:44