根据嵌套深度访问章节/节/...的标签

根据嵌套深度访问章节/节/...的标签

\nameref{}我想知道如何获取我的文本当前所在章节/节/小节/段落的标签。我不是指通过或获取章节的名称\titleref{},也不是指数字\ref{},也不是任何\label{}相关的输出。

我想获取\chaptername与当前嵌套深度相关的等,仅使用一个命令。最好与 babel 包结合使用,以便国际使用。如果这有效,重新排列章节/段落会更容易,因为标签会根据它们所在的标题进行更新。

我无法提供 MWE,但这里有一些与我的意思非常接近的东西。请注意,命令\currentheadingname是我正在寻找的缺失部分。

\documentclass{book}
\begin{document}
\chapter{SomeChapter}
Greetings from this \currentheadingname

\section{SomeSection}
In this \currentheadingname \dots 

\subsection{SomeSubsection}
This \currentheadingname is about \dots
\end{document}

期望结果如下:

  1. 某章

本章的问候

1.1. 某些部分

在这个部分 ...

1.1.1.某些小节

本节内容是关于...

有这样的命令吗?或者有什么解决方法?

答案1

据我所知它不存在,但可以定义。

我们将其定义为空

\newcommand{\currentheadingname}{}

\pretocmd并在包的帮助下为每个分段命令重新定义它etoolbox

\pretocmd{\part}{\renewcommand{\currentheadingname}{part\xspace}}{}{}
\pretocmd{\chapter}{\renewcommand{\currentheadingname}{chapter\xspace}}{}{}
\pretocmd{\section}{\renewcommand{\currentheadingname}{section\xspace}}{}{}
\pretocmd{\subsection}{\renewcommand{\currentheadingname}{subsection\xspace}}{}{}
\pretocmd{\subsubsection}{\renewcommand{\currentheadingname}{subsubsection\xspace}}{}{}
\pretocmd{\paragraph}{\renewcommand{\currentheadingname}{paragraph\xspace}}{}{}
\pretocmd{\subparagraph}{\renewcommand{\currentheadingname}{subparagraph\xspace}}{}{}

完成 MWE:

\documentclass{book}
\usepackage{etoolbox}
\usepackage{xspace} 

\newcommand{\currentheadingname}{}

\pretocmd{\part}{\renewcommand{\currentheadingname}{part\xspace}}{}{}
\pretocmd{\chapter}{\renewcommand{\currentheadingname}{chapter\xspace}}{}{}
\pretocmd{\section}{\renewcommand{\currentheadingname}{section\xspace}}{}{}
\pretocmd{\subsection}{\renewcommand{\currentheadingname}{subsection\xspace}}{}{}
\pretocmd{\subsubsection}{\renewcommand{\currentheadingname}{subsubsection\xspace}}{}{}
\pretocmd{\paragraph}{\renewcommand{\currentheadingname}{paragraph\xspace}}{}{}
\pretocmd{\subparagraph}{\renewcommand{\currentheadingname}{subparagraph\xspace}}{}{}

\begin{document}
\chapter{SomeChapter}
Greetings from this \currentheadingname

\section{SomeSection}
In this \currentheadingname \dots

\subsection{SomeSubsection}
This \currentheadingname is about \dots
\end{document} 

输出:

在此处输入图片描述

就 而言babel,不幸的是它仅定义了\partname和,因此对于等\chaptername无能为力。\section


编辑

“国际化使用”的解决方案。

如果您加载,则会hyperref定义以下命令:,,,\partautorefname等。它们根据加载的语言而变化(\chapterautorefname尽管并非所有语言都已定义,只有,,,,,,,,,,)。\sectionautorefnamebabelbabelafrikaansenglishfrenchgermanitalianmagyarportugesrussianspanishvietnamese

因此你可以将上述定义改为

\pretocmd{\part}{\renewcommand{\currentheadingname}{\MakeLowercase{\partautorefname}\xspace}}{}{}
\pretocmd{\chapter}{\renewcommand{\currentheadingname}{\MakeLowercase{\chapterautorefname}\xspace}}{}{}
\pretocmd{\section}{\renewcommand{\currentheadingname}{\sectionautorefname\xspace}}{}{}
\pretocmd{\subsection}{\renewcommand{\currentheadingname}{\subsectionautorefname\xspace}}{}{}
\pretocmd{\subsubsection}{\renewcommand{\currentheadingname}{\subsubsectionautorefname\xspace}}{}{}
\pretocmd{\paragraph}{\renewcommand{\currentheadingname}{\paragraphautorefname\xspace}}{}{}
\pretocmd{\subparagraph}{\renewcommand{\currentheadingname}{\subparagraphautorefname\xspace}}{}{}

注意\MakeLowercase\partautorefname\chapterautorefname因为它们是大写定义的。

以下 MWE 显示如何获取意大利语的当前标题名称:

\documentclass{book}
\usepackage[english,italian]{babel}
\usepackage{etoolbox}
\usepackage{xspace}
\usepackage{hyperref}

\newcommand{\currentheadingname}{}

\pretocmd{\part}{\renewcommand{\currentheadingname}{\MakeLowercase{\partautorefname}\xspace}}{}{}
\pretocmd{\chapter}{\renewcommand{\currentheadingname}{\MakeLowercase{\chapterautorefname}\xspace}}{}{}
\pretocmd{\section}{\renewcommand{\currentheadingname}{\sectionautorefname\xspace}}{}{}
\pretocmd{\subsection}{\renewcommand{\currentheadingname}{\subsectionautorefname\xspace}}{}{}
\pretocmd{\subsubsection}{\renewcommand{\currentheadingname}{\subsubsectionautorefname\xspace}}{}{}
\pretocmd{\paragraph}{\renewcommand{\currentheadingname}{\paragraphautorefname\xspace}}{}{}
\pretocmd{\subparagraph}{\renewcommand{\currentheadingname}{\subparagraphautorefname\xspace}}{}{}

\begin{document}
\chapter{SomeChapter}
Greetings from this ``\currentheadingname''

\section{SomeSection}
In this ``\currentheadingname'' \dots

\subsection{SomeSubsection}
This ``\currentheadingname'' is about \dots

\subsubsection{SomeSubsubsection}
This is a ``\currentheadingname''.

\paragraph{SomeParagraph}
This is a ``\currentheadingname''.

\subparagraph{SomeSubparagraph}
This is a ``\currentheadingname''.
\end{document} 

输出:

在此处输入图片描述

相关内容