获取当前不带标签的“部分”名称

获取当前不带标签的“部分”名称

更新:

我接受了提供的解决方案海科·奥伯迪克,因为这正是我要求的。

当只对章节名称感兴趣时,埃格尔的解决方案很好用。但是,要使其与 memoir 配合使用,必须将参数从 更改为#1#2不要问我为什么。

\documentclass[a4paper, 10pt, oneside]{memoir}
\usepackage{etoolbox}
\makeatletter
\apptocmd{\@chapter}{\gdef\currentchapter{#2}}{}{}
\def\currentchapter{?}
\makeatother

\begin{document}
\chapter{A Chapter}
``\currentchapter''
\section{A Section}
``\currentchapter''
\end{document}

老问题: 我想获取当前“部分”的名称(章节、节、小节,...)没有标记该部分。

我尝试使用\nameref例如,\thechapter但显然不起作用,因为\nameref需要一个引用(的名称\label)而不是一个数字。

是否有命令可以直接提供当前“部分”的名称?或者是否有命令可以根据部分编号“生成”引用,例如:\getname{\thechapter}

我已经发现,不可能获得当前“部分”的编号 - 我需要检查计数器以获取正确的“部分”。

我的目标是\todo从包中重新定义todonotes它始终显示当前的“部分”名称:

\let\Oldtodo\todo
\renewcommand{\todo}[1]{\Oldtodo{\currentname: #1}}

如果无法获取当前“部分”的名称,我也会对当前章节或部分名称感到满意:

\let\Oldtodo\todo
\renewcommand{\todo}[1]{\Oldtodo{\currentchaptername: #1}}

下面是一个没有的简单示例todonotes

\documentclass{memoir}
\begin{document}

\section{My section name}
The name of the current section is: " \currentname "
It should be: " My section name "

\subsection{My subsection name}
The name of the current subsection is: " \currentname "
It should be: " My subsection name "

\end{document}

请注意,我正在使用 documentclass memoir。

答案1

同一主题的变体。所有title-/nameref包都必须在某处记住当前标题。

包裹nameref

\documentclass{article}

\usepackage{nameref}
\makeatletter
\newcommand*{\currentname}{\@currentlabelname}
\makeatother

\begin{document}

\section{My section name}
The name of the current section is: "\currentname".\\
It should be: "My section name".

\subsection{My subsection name}
The name of the current subsection is: "\currentname".\\
It should be: "My subsection name".

\end{document}

带有 nameref 的结果

包裹titleref

\documentclass{article}

\usepackage{titleref}
\makeatletter
\newcommand*{\currentname}{\TR@currentTitle}
\makeatother

\begin{document}

\section{My section name}
The name of the current section is: "\currentname".\\
It should be: "My section name".

\subsection{My subsection name}
The name of the current subsection is: "\currentname".\\
It should be: "My subsection name".

\end{document}

结果相同。

包裹zref-titleref

\documentclass{article}

\usepackage{zref-titleref}
\makeatletter
\newcommand*{\currentname}{\zref@getcurrent{title}}
% or \newcommand*{\currentname}{\zref@titleref@current}
\makeatother

\begin{document}

\section{My section name}
The name of the current section is: "\currentname".\\
It should be: "My section name".

\subsection{My subsection name}
The name of the current subsection is: "\currentname".\\
It should be: "My subsection name".

\end{document}

结果相同。

答案2

这不完全是您问题的答案,但可能对其他人有用:

光束级提供命令\secname\subsecname获取当前章节或小节的名称。

此示例生成一个包含文本“当前部分:Foo”的框架:

\documentclass{beamer}
\begin{document}
  \section{Foo}
  \begin{frame}
    Current section: \secname
  \end{frame}
\end{document}

答案3

如果您使用依赖于内核命令来制作标题的标准类,那么如果该类是(当然,article删除命令) ,那么就有可能不经修改就可以工作。\chapter

\documentclass{book}

\usepackage{etoolbox}
\makeatletter
\newif\if@chapters
\@ifundefined{chapter}{\@chaptersfalse}{\@chapterstrue}
\if@chapters
  \apptocmd{\@chapter}{\gdef\currentname{#1}}{}{}
  \apptocmd{\@schapter}{\gdef\currentname{#1}}{}{}
\fi
\apptocmd{\@sect}{\gdef\currentname{#7}}{}{}
\def\currentname{---Still no title given---}
\makeatother

\begin{document}

\chapter{ABC}

\currentname

\section{My section name}
The name of the current section is: ``\currentname''
It should be: ``My section name''

\subsection{My subsection name}
The name of the current subsection is: ``\currentname''
It should be: ``My subsection name''

\end{document}

对于该类来说memoir没有什么可做的:该类已经提供\currenttitle确切地你想要什么。


请注意,你不应该\todo在尝试时重新定义;而是

\makeatletter
\renewcommand\todo[2][]{\@todo[#1]{\currentname: #2}}
\makeatother

答案4

你可以做类似的事情

\let\oldsection\section
\renewcommand{\section}[2][]{\def\currentname{#2}\oldsection[#1]{#2}}

您可能希望以更好的方式处理分段命令的可选参数。

\documentclass{article}
\makeatletter
\let\oldsection\section
\let\oldsubsection\subsection
\renewcommand{\section}[2][]{\def\currentname{#2}\oldsection[#1]{#2}}
\renewcommand{\subsection}[2][]{\def\currentname{#2}\oldsubsection[#1]{#2}}
\makeatother

\begin{document}

\section{My section name}
The name of the current section is: " \currentname "
It should be: " My section name "

\subsection{My subsection name}
The name of the current subsection is: " \currentname "
It should be: " My subsection name "

\end{document}

在此处输入图片描述

相关内容