(子)(子)部分的“自动标记”不适用于数学

(子)(子)部分的“自动标记”不适用于数学

我按照代码这个答案. (子)节。但是,如果 (子)节标题中有任何数学运算,它就会失败。这可能是意料之中的,但有办法克服这个问题吗?或者我应该使用什么标签来引用\ref?提前致谢。

梅威瑟:

\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}

答案1

从章节标题自动生成标签听起来很有吸引力,但实际上并非如此,因为它会导致很多问题。这不仅影响数学模式,而且会影响宏扩展。此外,即使标题有微小的修改,所有交叉引用也必须更改。这几乎与手动解析所有交叉引用而不是通过 \label/\ref 自动解析一样不切实际。

尽管如此,你仍然可以定义各种宏的替换来尝试实现它。这是一个使用 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}

然而,如果您想对完整的可能的数学进行此操作,这可能会变得更加耗时。

另外,您也可以用名称替换所有宏,并另外删除下标和上标等:

\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}

“LaTeX3 界面”了解更多信息。

相关内容