如何获取当前章节名称、节名称、小节名称等?

如何获取当前章节名称、节名称、小节名称等?

如何获取以下的当前名称:章节、节、小节、框架、标签、mdframed?

答案1

有了这个解决方案,您仍然可以使用 et al 的星号版本和可选参数\chapter

\documentclass{book}
\let\Chaptermark\chaptermark
\def\chaptermark#1{\def\Chaptername{#1}\Chaptermark{#1}}
\let\Sectionmark\sectionmark
\def\sectionmark#1{\def\Sectionname{#1}\Sectionmark{#1}}
\let\Subsectionmark\subsectionmark
\def\subsectionmark#1{\def\Subsectionname{#1}\Subsectionmark{#1}}
\let\Subsubsectionmark\subsubsectionmark
\def\subsubsectionmark#1{\def\Subsubsectionname{#1}\Subsubsectionmark{#1}}

\begin{document}
\chapter{First chapter}\label{ch:first}
This is chapter~\ref{ch:first} with title ``\Chaptername''.
\section{First section}\label{sec:first}
This is section~\ref{sec:first} with title ``\Sectionname''.
\subsection{The first subsection}\label{subsec:first}
This is subsection~\ref{subsec:first} with title ``\Subsectionname''.
\subsubsection{Last subsubsection}\label{subsubsec:first}
This is subsubsection~\ref{subsubsec:first} with title ``\Subsubsectionname''.
\end{document}

答案2

就章节标题而言,nameref包裹非常适合这种情况。它提供\nameref{<label>}返回与标签关联的部分标题的功能:

在此处输入图片描述

\documentclass{article}
\usepackage{nameref}% http://ctan.org/pkg/nameref
\begin{document}
\section{First section}\label{first}
This is section~\ref{first} with title \nameref{first}.
\subsection{Second subsection}\label{second}
This is subsection~\ref{second} with title \nameref{second}.
\subsubsection{Last subsubsection}\label{third}
This is subsubsection~\ref{third} with title \nameref{third}.
\end{document}

hyperref提供类似的功能,因为它可以加载nameref。因此,它们可以毫无问题地协同工作,允许使用超链接标题。memoir做一些类似的事情来存储部门单位的名称/标题。

这可以扩展到包括捕获与其他环境或结构相关的名称/标题。

答案3

这是一个解决方案,但不是最佳方案。它只是更新旧的分段命令,并让新的命令更新 currentxxx 宏。

\begin{document}

\newcommand{\currentchapter}{}
\let\oldchapter\chapter
\renewcommand{\chapter}[1]{\oldchapter{#1}\renewcommand{\currentchapter}{#1}}

\newcommand{\currentsection}{}
\let\oldsection\section
\renewcommand{\section}[1]{\oldsection{#1}\renewcommand{\currentsection}{#1}}

\newcommand{\currentsubsection}{}
\let\oldsubsection\subsection
\renewcommand{\subsection}[1]{\oldsubsection{#1}\renewcommand{\currentsubsection}{#1}}

\newcommand{\currentsubsubsection}{}
\let\oldsubsubsection\subsubsection
\renewcommand{\subsubsection}[1]{\oldsubsubsection{#1}\renewcommand{\currentsubsubsection}{#1}}


\chapter{chapter test}
\section{section test}
\subsection{subsection test}

\currentchapter\\
\currentsection\\
\currentsubsection\\

\end{document}

答案4

titleref提供类似的功能nameref,但不兼容hyperref

在此处输入图片描述

\documentclass{article}
\usepackage{titleref}
\begin{document}
\section{First section}\label{first}
This is section~\ref{first} with title \titleref{first}.
\subsection{Second subsection}\label{second}
This is subsection~\ref{second} with title \titleref{second}.
\subsubsection{Last subsubsection}\label{third}
This is subsubsection~\ref{third} with title \titleref{third}.
\end{document}

相关内容