在处理大型 tex 文档时,我经常发现自己使用搜索/替换来重组它们(例如部分出局章节或小节出局小节s)
有没有什么办法可以相对地定义我的章节/部分?
因此
\chapter{one}
\section{two}
\section{three}
\chapter{four}
可以这样写:
\currentdepth{one}
\nextdepth{two}
\currentdepth{three}
\prevdepth{four}
这样,我就可以轻松地在文档中移动内容,而不必触碰所有标题。
谢谢你!
答案1
初步解决方案,目前尚未完成!
\documentclass{book}
\usepackage{etoolbox}
\usepackage{xparse}
\newcounter{sectionlevel}
\makeatletter
\NewDocumentCommand{\currentdepth}{sO{}m}{%
\IfBooleanTF{#1}{%
}{
\ifnum\value{sectionlevel} = -1
\part[#2]{#3}%
\else
\ifcase\value{sectionlevel}
\chapter[#2]{#3}
\or
\section[#2]{#3}
\or
\subsection[#2]{#3}
\or
\subsubsection[#2]{#3}
\or
\paragraph[#2]{#3}
\or
\subparagraph[#2]{#3}
\else
Oopss!
\fi
\fi
}
}
\NewDocumentCommand{\nextdepth}{sO{}m}{%
\stepcounter{sectionlevel}
\currentdepth[#2]{#3}
}
\NewDocumentCommand{\previousdepth}{sO{}m}{%
\addtocounter{sectionlevel}{-1}
\currentdepth[#2]{#3}
}
\makeatother
\AtBeginDocument{%
\setcounter{sectionlevel}{0} %
}
\begin{document}
\tableofcontents
\currentdepth{Hello World}
\nextdepth{Hello World - Section}
\currentdepth{Hello World - Section - Again}
\nextdepth{Hello World - Subsection}
\previousdepth{A section again}
\previousdepth{A new chapter}
\end{document}
编辑改进的版本,带有星号和可选版本,并且\previousdepth<-3>{...}
能够跳回 3 个级别(或其他)。
没有对允许的级别进行真正的检查!
\documentclass{book}
\usepackage{etoolbox}
\usepackage{xparse}
\usepackage{letltxmacro}
\newcounter{sectionlevel}
\makeatletter
\NewDocumentCommand{\currentdepth}{som}{%
\LetLtxMacro\generic@@section\relax%
\ifnum\value{sectionlevel} = -1
\global\LetLtxMacro \generic@@section\part
\else
\ifcase\value{sectionlevel}
\global\LetLtxMacro\generic@@section\chapter
\or
\global\LetLtxMacro\generic@@section\section
\or
\global\LetLtxMacro\generic@@section\subsection
\or
\global\LetLtxMacro\generic@@section\subsubsection
\or
\global\LetLtxMacro\generic@@section\paragraph
\or
\global\LetLtxMacro\generic@@section\subparagraph
\else
Oopss!
\fi
\fi
\IfBooleanTF{#1}{% Is it the starred version?
\generic@@section*{#3}%
}{%
\IfValueTF{#2}{%
\generic@@section[#2]{#3}%
}{%
\generic@@section{#3}%
}%
}%
}
\NewDocumentCommand{\nextdepth}{som}{%
\stepcounter{sectionlevel}%
\IfBooleanTF{#1}{%
\currentdepth*{#3}%
}{%
\IfValueTF{#2}{%
\currentdepth[#2]{#3}%
}{%
\currentdepth{#3}%
}%
}%
}
\NewDocumentCommand{\previousdepth}{sD<>{-1}om}{%
\addtocounter{sectionlevel}{#2}%
\IfBooleanTF{#1}{%
\currentdepth*{#4}%
}{%
\IfValueTF{#3}{%
\currentdepth[#3]{#4}%
}{%
\currentdepth{#4}%
}%
}%
}
\AtBeginDocument{%
\setcounter{sectionlevel}{0} %
}
\begin{document}
\tableofcontents
\currentdepth{Hello World}
\nextdepth{Hello World - Section}
\currentdepth{Hello World - Section - Again}
\currentdepth*{Hello World - Section - Again - but starred}
\nextdepth{Hello World - Subsection}
\previousdepth{A section again}
\previousdepth{A new chapter}
\nextdepth{Yet another section}
\nextdepth{Yet another subsection}
\nextdepth{Yet another subsubsection}
\previousdepth<-3>{And this is a ...?}
\end{document}