我寻找一种解决方案,以便从宏(包括带星号的 section 命令变体)访问当前部分(章节、节等)的名称。建议的宏集这里在基类中,它能很好地发挥作用book
。
\documentclass{book}
\usepackage{etoolbox}
% Patch the sectioning commands to provide a hook to be used later
\preto{\chapter}{\def\leveltitle{\chaptertitle}}
\preto{\section}{\def\leveltitle{\sectiontitle}}
\preto{\subsection}{\def\leveltitle{\subsectiontitle}}
\preto{\subsubsection}{\def\leveltitle{\subsubsectiontitle}}
\makeatletter
\pretocmd{\@sect}
{\expandafter\gdef\leveltitle{#8}}
{}{}
\pretocmd{\@ssect}
{\expandafter\gdef\leveltitle{#5}}
{}{}
\pretocmd{\@chapter}
{\expandafter\gdef\leveltitle{#2}}
{}{}
\pretocmd{\@schapter}
{\expandafter\gdef\leveltitle{#1}}
{}{}
\makeatother
\newcommand\test{%
\noindent
The chapter title is \chaptertitle\\
The section title is \sectiontitle\\
The subsection title is \subsectiontitle\\
The subsubsection title is \subsubsectiontitle
}
\begin{document}
\chapter{First chapter}
\section{First section}
\subsection{First subsection}
\subsubsection{First subsubsection}
\test
\chapter*{Second chapter}
\section*{Second section}
\subsection*{Second subsection}
\subsubsection*{Second subsubsection}
\test
\end{document}
但是我需要在 Koma-Script 中实现我的宏,scrbook
这里事情开始变得出乎意料。使用相同的代码但使用类,scrbook
我得到以下结果:
章节名称未呈现。我查看了代码scrbook
,标题确实是#2。宏有问题吗?如何修复此问题?
对于我的需求,无法使用nameref
,我需要一个硬编码解决方案。谢谢你的帮助。
奖励:是否可以在长标题和短标题(用 定义的标题)之间进行选择\section[short]{long}
?再次感谢。
答案1
您可以使用nameref
,您所需要的只是一个自动设置\label
和一个当前标签的名称。从即将推出的 KOMA-Script 3.27(目前仅可从 KOMA-Script 源存储库或KOMA-Script 预发布存储库)新的do-hook功能可用于添加自动标签:
\documentclass[headings=optiontoheadandtoc]{scrbook}[2019/03/05]
\usepackage{nameref}
\newcounter{secautolabel}
\AddtoDoHook{heading/endgroup}{\setautolabel}
\newcommand*{\setautolabel}[1]{%
\stepcounter{secautolabel}%
\label{sec:autolabel:\thesecautolabel}%
\expandafter\xdef\csname #1title\endcsname{%
\noexpand\nameref{sec:autolabel:\thesecautolabel}%
}%
}
\newcommand\test{%
\noindent
The chapter title is \chaptertitle\\
The section title is \sectiontitle\\
The subsection title is \subsectiontitle\\
The subsubsection title is \subsubsectiontitle
}
\begin{document}
\chapter{First chapter}
\section{First section}
\subsection[reference={The First subsection reference}]{First subsection}
\subsubsection{First subsubsection}
\test
\chapter*{Second chapter}
\section*{Second section}
\subsection*{Second subsection}
\subsubsection*{Second subsubsection}
\test
\end{document}
结果如下:
所以您不需要修补或使用(内部)命令。
使用当前官方版本,您可以通过修补来做类似的事情\sectionlinesformat
:\chapterlinesformat
\documentclass[headings=optiontoheadandtoc]{scrbook}
\usepackage{nameref}
\newcounter{secautolabel}
\newcommand*{\setautolabel}[1]{%
\stepcounter{secautolabel}%
\label{sec:autolabel:\thesecautolabel}%
\expandafter\xdef\csname #1title\endcsname{%
\noexpand\nameref{sec:autolabel:\thesecautolabel}%
}%
}
\usepackage{xpatch}
\xapptocmd{\sectionlinesformat}{\setautolabel{#1}}{}{\undefined}
\xapptocmd{\sectioncatchphraseformat}{\setautolabel{#1}}{}{\undefined}
\xapptocmd{\chapterlinesformat}{\setautolabel{#1}}{}{\undefined}
\xapptocmd{\chapterlineswithprefixformat}{\setautolabel{#1}}{}{\undefined}
\newcommand\test{%
\noindent
The chapter title is \chaptertitle\\
The section title is \sectiontitle\\
The subsection title is \subsectiontitle\\
The subsubsection title is \subsubsectiontitle
}
\begin{document}
\chapter{First chapter}
\section{First section}
\subsection[reference={The First subsection reference}]{First subsection}
\subsubsection{First subsubsection}
\test
\chapter*{Second chapter}
\section*{Second section}
\subsection*{Second subsection}
\subsubsection*{Second subsubsection}
\test
\end{document}
结果和上面一样。
headings=optiontotocandhead
顺便说一句:在这两个例子中,我展示了如何使用选项和扩展可选参数(例如)来操作结果\subsection
。