1

I followed the code in this answer. (sub)(sub)sections. However, if there is any math in the (sub)(sub)section title it fails to work. This is probably expected, but is there a way to overcome this? Or what label should I reference using \ref instead? Thanks in advance.

MWE:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}

\usepackage{etoolbox} \makeatletter \patchcmd{@sect}% <cmd> {@xsect}% <search> {\label{sec:#8}@xsect}% <replace> {}{}% <successs><failure> \makeatother

\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 \ref{sec:The ($\epsilon$, $\delta$)-definition --- the rigorous approach}.

\end{document}

  • Thanks for providing code and a reference. This is a decent first question. However, I'm not sure what problem you're trying to solve. The linked answer concerns cross-references, but your code gives an error. Is that the problem? You need to change \start{document} to \begin{document} and load the packages you need for the commands you use. – cfr Sep 14 '23 at 05:21
  • Just create a label manually in the normal way e.g. \label{sec:delta-epsilon-method} ... \ref{sec:delta-epsilon-method}. Maths in a label is going to spell trouble. If you use hyperref, you may also need to use the short title to give an alternative. – cfr Sep 14 '23 at 05:29
  • @cfr sorry i completely messed up my mwe, now it should be fixed

    Basically the only problem concerns using math in the section name, and the lack of packages and the typo start is irrelevant

    – Number Basher Sep 14 '23 at 07:21
  • @cfr is there absolutely no workaround? can't i just change a macro (do something once), instead of add a label to every section? – Number Basher Sep 14 '23 at 07:22
  • edit: or can i change the macro to ignore math? – Number Basher Sep 14 '23 at 07:24
  • Well if you read the comments under the question you linked to you will see that they discuss this problem. – Ulrike Fischer Sep 14 '23 at 07:26
  • 1
    @NumberBasher I basically think that in general this is a bad idea: the \label contains 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:46
  • that is just a single find replace – Number Basher Sep 14 '23 at 08:32
  • @UlrikeFischer but to no avail – Number Basher Sep 14 '23 at 08:32
  • 1
    no they should tell you that it is a bad idea to generate such labels. But yes I know that people never believe it if one tell them not to do something. You want to save time to write down a label and will instead spent time to fight against command expansion and curious label names but that is your choice. – Ulrike Fischer Sep 14 '23 at 08:39
  • @UlrikeFischer

    A 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
  • hyperref has a around 300 lines long command \pdfstringdef whose purpose is to make content safe to write it into the bookmark. It doesn't have to work in a roundtrip through auxiliary files, isn't used in a csname and nevertheless doesn't work in all cases, user have to handle special cases sometimes with \texorpdfstring. Apart from this: even if you manage to sanitize your label name, how will you ever use it? Will you look into the aux to find out which name your code produced? You are simply filling the aux with unusable stuff and slow down compilation for nothing. – Ulrike Fischer Sep 14 '23 at 09:01
  • @UlrikeFischer I wanted something like the current accepted answer – Number Basher Sep 14 '23 at 10:33
  • I know that you wanted that. But I doubt that you are really aware of the side-effects and problems. You are not the first trying to do this and then discover that it is not so easy. Add hyperref and a \paragraph{Remark} to the example and try again. – Ulrike Fischer Sep 14 '23 at 11:12

1 Answers1

2

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.

cabohah
  • 11,455
  • can we simply replace characters not allowed in math, so just removing $ and \? or what other changes do i need to make? – Number Basher Sep 14 '23 at 08:31
  • ^ the above `` was meant to be \\; also the provided MWE does not work – Number Basher Sep 14 '23 at 08:41
  • @NumberBasher You have to replace macros not allowed in \newlabel and therefore neither allowed in \label nor in \ref, \pageref etc. These are not only math mode macros, but also a lot of other macros, like \emph, \textit … Just removing math code wouldn't be sufficient. As already told: Automatic labeling by title isn't as easy as it may seem. It is quit difficult. It is similar to automatic bookmarking. hyperref needs a lot of code to handle it. But if you want you can try using LaTeX3 regular expressions to remove all macros. – cabohah Sep 14 '23 at 08:59
  • @NumberBasher What do you mean with: »The provided MWE does not work«? My example works as it is using an up-to-date LaTeX (at least 2020-10-01). If you are using an older LaTeX you should update or add \usepackage{xparse}. Support for older LaTeX is difficult. – cabohah Sep 14 '23 at 09:03
  • i dont know why i though the mwe didnt work – Number Basher Sep 14 '23 at 10:28
  • This is something I was looking for, thank you. – Number Basher Sep 14 '23 at 10:33