在我目前正在处理的文档中,我们经常更改章节/部分等的层次结构。因此,在文本中,我想在类似这样的句子中引用当前的层次结构级别
In this chapter we will talk about ...
由于结构经常变化,我想将当前层次结构的级别名称(上例中的“章节”)添加到某些命令中,以便当我们再次更改结构时,它会自动适应。
但是我找不到这样的命令。大多数命令都用于获取当前章节的标题或对其他章节的引用(包括章节编号)。
因此,根据这个例子,我需要写什么才能\someCommand
获得当前章节/部分/小节/等的名称?
\documentclass[a4paper]{memoir}
\begin{document}
\chapter{Example-Chapter}
Current level: \someCommand - should be "chapter" and not "Example-Chapter"
\section{Example-Section}
Current level: \someCommand - should be "section" and not "Example-Section"
\end{document}
答案1
\chapter
您可以使用重置链接计数器的事实,并且同样的情况也会发生\section
。
\documentclass[a4paper]{memoir}
\newcommand{\someCommand}{%
\ifnum\value{section}=0
chapter%
\else
\ifnum\value{subsection}=0
section%
\else
subsection%
\fi
\fi
}
\setsecnumdepth{subsection}
\begin{document}
\chapter{Example chapter}
Current level: \someCommand, should be ``chapter''
\section{Example section}
Current level: \someCommand, should be ``section''
\subsection{Example subsection}
Current level: \someCommand, should be ``subsection''
\section{Example section}
Current level: \someCommand, should be ``section''
\end{document}
答案2
此版本还适用于未编号结构层级。结构宏定义\chapter
等\section
在开头以结构层级名称和编号的定义进行扩展。
\documentclass{report}
\setcounter{secnumdepth}{-1}
\usepackage{etoolbox}
\newcommand*{\tmp}[3]{%
\pretocmd#1{%
\gdef\StructureLevelNumber{#2}%
\gdef\StructureLevelName{#3}%
}{}{%
\errmessage{Cannot patch \string#1}%
}%
}
\tmp\part{-1}{part}
\tmp\chapter{0}{chapter}
\tmp\section{1}{section}
\tmp\subsection{2}{subsection}
\tmp\subsubsection{3}{subsubsection}
\tmp\paragraph{4}{paragraph}
\tmp\subparagraph{5}{subparagraph}
\newcommand*{\test}{%
We are in structure level ``\StructureLevelName'' %
(\StructureLevelNumber).%
}
\begin{document}
\part{Part title}
\test
\chapter{Chapter title}
\test
\section{Section title}
\test
\subsection{Subsection title}
\test
\subsubsection{Subsubsection title}
\test
\paragraph{Paragraph title.}
\test
\subparagraph{Subparagraph title.}
\test
\end{document}
第三页:
具有结构层次的参考文献
该包zref
支持定义可存储在标签(\zlabel
、\zref@label...
)和引用(\zref
、\zref@extract...
)中的新属性。
下面的例子仅使用整数来标识结构级别,并通过宏为结构级别生成名称\StructureLevelName
。
\documentclass{report}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{color}
\setcounter{secnumdepth}{-1}
\usepackage{etoolbox}
\newcommand*{\tmp}[2]{%
\pretocmd#1{%
\gdef\StructureLevel{#2}%
}{}{%
\errmessage{Cannot patch \string#1}%
}%
}
\tmp\part{-1}
\tmp\chapter{0}
\tmp\section{1}
\tmp\subsection{2}
\tmp\subsubsection{3}
\tmp\paragraph{4}
\tmp\subparagraph{5}
\newcommand*{\StructureLevel}{-2}
\newcommand*{\StructureLevelName}[1]{%
\ifcase\numexpr(#1)\relax
chapter%
\or
section%
\or
subsection%
\or
subsubsection%
\or
paragraph%
\or
subparagraph%
\else
\ifnum\numexpr(#1)\relax=-1 %
part%
\else
unknown%
\fi
\fi
}
\usepackage{zref-base, zref-titleref, zref-user}
\makeatletter
\zref@newprop{StructureLevel}[-2]{\StructureLevel}
\zref@addprop{main}{StructureLevel}
\newcommand*{\zrefStructureLevelName}[1]{%
\zref@refused{#1}%
\StructureLevelName{%
\zref@extractdefault{#1}{StructureLevel}{-2}%
}%
}
\makeatother
\newcommand*{\test}{%
We are in structure level
``\textcolor{red}{\StructureLevelName{\StructureLevel}}'' %
(\textcolor{blue}{\StructureLevel}).%
}
\newcommand*{\testref}[1]{%
\texttt{\textbackslash zlabel\{#1\}}:
Structure element is ``\textcolor{red}{\zrefStructureLevelName{#1}}'',
title: \textcolor{blue}{\ztitleref{#1}}\par
}
\begin{document}
\part{Part title}
\zlabel{a}
\test
\chapter{Chapter title}
\zlabel{b}
\test
\section{Section title}
\zlabel{c}
\test
\subsection{Subsection title}
\zlabel{d}
\test
\begin{equation}
\zlabel{eq}
E=mc^2
\end{equation}
\subsubsection{Subsubsection title}
\zlabel{e}
\test
\paragraph{Paragraph title.}
\zlabel{f}
\test
\subparagraph{Subparagraph title.}
\zlabel{g}
\test
\newpage
\testref{a}
\testref{b}
\testref{c}
\testref{d}
\testref{e}
\testref{f}
\testref{g}
\texttt{\textbackslash zlabel\{eq\}}:
Equation \zref{eq} is in structure element
``\textcolor{red}{\zrefStructureLevelName{eq}}''.
\end{document}
最后一页参考文献:
答案3
虽然这不是一个独特的新答案,但我想添加它以供参考:
在对@egreg 的答案进行了一些实验之后,我还删除了“静态”分段标签,并用用途替换它们,因此它与文档中hyperref
的使用保持一致。\autoref
\documentclass[a4paper]{memoir}
\usepackage{hyperref}
\newcommand{\structuringLevel}{%
\ifnum\value{section}=0
\chapterautorefname%
\else
\ifnum\value{subsection}=0
\sectionautorefname%
\else
\subsectionautorefname%
\fi
\fi
}
\setsecnumdepth{subsection}
\begin{document}
\chapter{Example chapter}
Current level: \someCommand, should be ``chapter''
\section{Example section}
Current level: \someCommand, should be ``section''
\subsection{Example subsection}
Current level: \someCommand, should be ``subsection''
\section{Example section}
Current level: \someCommand, should be ``section''
\end{document}
答案4
这里有一个基于cleveref
包的解决方案。
优点:
- 它不需要低级代码。这使 LaTeX 代码更易于阅读。
- 它区分大写(例如“Section”)和非大写(例如“section”)。它还提供复数形式,以防万一 ;)。
- 我们使用标准包:无需定义自己的函数。
缺点:
- 我们必须标记各个部分
\label
(然而,这是编写文档时的通常程序)
示例代码
\documentclass[a4paper]{memoir}
\usepackage{cleveref}
%Redefining the names associated to the 'chapter' counter
\crefname{chapter}{chapter}{chapters} %uncapitalized
\Crefname{chapter}{Chapter}{Chapters} %capitalized
%Redefining the names associated to the 'section' counter
\crefname{section}{section}{sections}
\Crefname{section}{Section}{Sections}
%Redefining the names associated to the 'subsection' counter
\crefname{subsection}{subsection}{subsections}
\Crefname{subsection}{Subsection}{Subsections}
%...
%You can name as many sectioning commands as you want
%...
%Redefining the names associated to the 'subparagraph' counter
\crefname{subparagraph}{subparagraph}{subparagraphs}
\Crefname{subparagraph}{Subparagraph}{Subparagraphs}
%Here set the name of the lowest section that has to be named
\setsecnumdepth{subparagraph}
\begin{document}
\chapter{Example chapter}\label{sec:level01}
Current level: \namecref{sec:level01}, should be ``chapter''.\\ %Uncapitalized name
\nameCref{sec:level01} is the current level. %Capitalized name
\section{Example section}\label{sec:level02}
Current level: \namecref{sec:level02}, should be ``section''.\\
\nameCref{sec:level02} is the current level.
\subsection{Example subsection}\label{sec:level03}
Current level: \namecref{sec:level03}, should be ``subsection''.\\
\nameCref{sec:level03} is the current level.
\subparagraph{Example subparagraph}\label{sec:level05}
Current level: \namecref{sec:level05}, should be ``subparagraph''.
\nameCref{sec:level05} is the current level.
\section{Example section}\label{sec:level04}
Current level: \namecref{sec:level04}, should be ``section''.\\
\nameCref{sec:level04} is the current level.
\end{document}