获取特定级别的当前部分的标题(类似于 \thesection)

获取特定级别的当前部分的标题(类似于 \thesection)

类似于thesection,,thesubsection...我想获取某个部分的标题(因为它在文档正文中),例如通过(伪代码)\thesectiontitle,,\thesubsectiontitle...
解决方案不应该使用目录,因为我希望能够使用自定义标题作为目录(通过\section[custom title]{body title})。

答案1

由于文档的逻辑结构遵循层次结构,并且您只对层次结构中较高元素的标题感兴趣,因此以下基本补丁可以\@sect起作用:

在此处输入图片描述

\documentclass{article}

\usepackage{etoolbox}
\makeatletter
\pretocmd{\@startsection}{\gdef\thesectiontype{#1}}{}{}
\pretocmd{\@sect}{\@namedef{the\thesectiontype title}{#8}}{}{}
\pretocmd{\@ssect}{\@namedef{the\thesectiontype title}{#5}}{}{}
\makeatother

\setlength{\parindent}{0pt}% Just for this example
\begin{document}

\section{A section}

\subsection{A subsection}
\thesectiontitle

\subsubsection{A subsubsection}
\thesectiontitle \par
\thesubsectiontitle

\subsection*{Another subsection}
\thesectiontitle

\subsubsection{Another subsubsection}
\thesectiontitle \par
\thesubsectiontitle

\end{document}

您可以定义一些默认值,\thesectiontitle如果调用则返回任何内容,例如没有\section调用过。但是,鉴于目前的描述,我不确定这样做是否有必要。

答案2

由于时间不够,只能提供初步解决方案,而不是修补等等\section

代码使用旧版本对等进行了稍微的重新定义,并在末尾\section附加了定义。\thesectionname

但是,\thesubsectionname在下一节中使用它会导致错误的名称,因此我使用技巧在特定结构级别开始时\@elt递归地杀死所有内容。\the...names

\documentclass{article}


\usepackage{xparse}

\setcounter{secnumdepth}{3}

\makeatletter

\newcommand{\@kickthenames}[1]{%
  \begingroup
  \def\@elt##1{\@ifundefined{the##1name}{}{\expandafter\gdef\csname the##1name\endcsname{---undefined---}}\@kickthenames{##1}}% Recursively kill the names
  \expandafter\csname cl@#1\endcsname%
  \endgroup
}


\NewDocumentCommand{\RedefineLevel}{om}{%
 \expandafter\expandafter\expandafter\let\expandafter\expandafter\csname latex@@#2\endcsname\csname #2\endcsname%
 \expandafter\RenewDocumentCommand\csname #2\endcsname{som}{%
   \@kickthenames{#2}%
    \IfBooleanTF{##1}{%
      \expandafter\csname latex@@#2\endcsname*{##3}%
    }{%
      \IfValueTF{##2}{%
        \expandafter\csname latex@@#2\endcsname[##2]{##3}%
      }{%
        \expandafter\csname latex@@#2\endcsname{##3}%
      }%
    }%
    \expandafter\edef\csname the#2name\endcsname{##3}%
  }%
}

\makeatother

\RedefineLevel{section}
\RedefineLevel{subsection}
\RedefineLevel{subsubsection}

\begin{document}
\tableofcontents
\section{A1}
\subsection[B1]{B1}
\subsubsection[C1]{C1}
\subsection[B2]{B2}
\subsubsection[C2]{C2}

The current section name is \thesectionname

The current subsection name is \thesubsectionname

The current subsubsection name is \thesubsubsectionname

\subsubsection[C3]{C3}

The current section name is \thesectionname

The current subsection name is \thesubsectionname

The current subsubsection name is \thesubsubsectionname


\section{Z}

The current section name is \thesectionname

The current subsection name is \thesubsectionname

The current subsubsection name is \thesubsubsectionname


\subsection{B1}

The current section name is \thesectionname

The current subsection name is \thesubsectionname

The current subsubsection name is \thesubsubsectionname

\subsection{B2}
The current section name is \thesectionname

The current subsection name is \thesubsectionname

The current subsubsection name is \thesubsubsectionname

\subsubsection{C1}

The current section name is \thesectionname

The current subsection name is \thesubsectionname

The current subsubsection name is \thesubsubsectionname



\end{document}

在此处输入图片描述

相关内容