创建上下文相关标题

创建上下文相关标题

这个问题导致了一个新的方案的出现:
coseoul

是否可以在 LaTeX 中创建标题,而无需指定所需的标题级别,而是指定标题所在的层次结构中向上、向下、停留或重新开始?

也就是说,不要写

\section{Level 1}
\subsection{Level 2}
\subsection{Level 2}
\subsubsection{Level 3}
\subsection{Level 2}
\section{Level 1}

写一些类似

\sectiontop{Level 1}
\sectiondown{Level 2}
\sectionstay{Level 2}
\sectiondown{Level 3}
\sectionup{Level 2}
\sectiontop{Level 1}

如果您使用类似这样的方法,就可以快速进行更改。例如,如果您突然意识到想要将标题放在第 2-5 行的某个标题下,则可以通过在\sectiondown它们前面放置一个来轻松完成此操作。如果您使用的是必须指定每个标题级别的系统,则必须对所有 4 行进行单独更改。

答案1

就是这样。它可以工作,但目前只适用于\chapter\section和。它通过定义来工作\subsection,它最初是空的,因此您需要使用新的部分命令之一(、和)。之后您可以使用、和。您可以将其扩展为等,但请记住以下几点:\subsubsection\currrentlevel\newchapter\newsection\newsubsection\newsubsubsection\levleup\levelstay\leveldown\part\paragraphs

  • \levelup正好向上移动一个级别,您不能从\subsubsection(例如 1.2.3.4)移动到\section(例如 1.3),因为这是两个级别的差异。您可以定义一个\twolevelup,或者使用\newsection(但这会破坏相对论)
  • 定义的顺序很重要。它是通过\ifthenelse构造完成的,因此\levleup定义需要从上到下(cs-ss-sss)并且\levledown需要从下到上(sss-ss-sc)
  • 记住你使用的是什么类。article不知道\chapter,所以\levleup在某个部分之后尝试可能会导致错误
  • 请记住,\currentsection一开始并没有初始化,因此可以使用例如\newchapter

\documentclass{report}
\usepackage{ifthen}

\newcommand{\currentlevel}{}

\newcommand{\levelup}[1]%
{   \ifthenelse{\equal{\currentlevel}{c}}%
    {\chapter{#1}\renewcommand{\currentlevel}{c}}{}%
    \ifthenelse{\equal{\currentlevel}{s}}%
    {\chapter{#1}\renewcommand{\currentlevel}{c}}{}%
    \ifthenelse{\equal{\currentlevel}{ss}}%
    {\section{#1}\renewcommand{\currentlevel}{s}}{}%
    \ifthenelse{\equal{\currentlevel}{sss}}%
    {\subsection{#1}\renewcommand{\currentlevel}{ss}}{}%
}

\newcommand{\leveldown}[1]%
{   \ifthenelse{\equal{\currentlevel}{sss}}%
    {\subsubsection{#1}\renewcommand{\currentlevel}{sss}}{}%    
    \ifthenelse{\equal{\currentlevel}{ss}}%
    {\subsubsection{#1}\renewcommand{\currentlevel}{sss}}{}%    
    \ifthenelse{\equal{\currentlevel}{s}}%
    {\subsection{#1}\renewcommand{\currentlevel}{ss}}{}%
    \ifthenelse{\equal{\currentlevel}{c}}%
    {\section{#1}\renewcommand{\currentlevel}{s}}{}%
}

\newcommand{\levelstay}[1]%
{   \ifthenelse{\equal{\currentlevel}{c}}%
    {\chapter{#1}}{}%
    \ifthenelse{\equal{\currentlevel}{s}}%
    {\section{#1}}{}%
    \ifthenelse{\equal{\currentlevel}{ss}}%
    {\subsection{#1}}{}%
    \ifthenelse{\equal{\currentlevel}{sss}}%
    {\subsubsection{#1}}{}%
}

\newcommand{\newchapter}[1]{\chapter{#1}\renewcommand{\currentlevel}{c}}
\newcommand{\newsection}[1]{\section{#1}\renewcommand{\currentlevel}{s}}
\newcommand{\newssubection}[1]{\subsection{#1}\renewcommand{\currentlevel}{ss}}
\newcommand{\newssubsubection}[1]{\subsubsection{#1}\renewcommand{\currentlevel}{sss}}

\setcounter{tocdepth}{5} 

\begin{document}

\tableofcontents
\newpage

\newchapter{chapter 1}

\leveldown{ 1.1}
\levelstay{ 1.2}
\levelstay{ 1.3}
\leveldown{ 1.3.1}
\levelstay{ 1.3.2}
\levelup{ 1.4}
\levelstay{ 1.5}
\leveldown{ 1.5.1}
\levelstay{ 1.5.2}
\leveldown{ 1.5.2.1}
\levelstay{ 1.5.2.2}
\levelstay{ 1.5.2.3}
\levelstay{ 1.5.2.4}

\newchapter{chapter 2}

\leveldown{ 2.1}
\levelstay{ 2.2}
\levelstay{ 2.3}
\leveldown{ 2.3.1}
\levelstay{ 2.3.2}
\levelup{ 2.4}
\levelstay{ 2.5}
\leveldown{ 2.5.1}

\end{document}

编辑1:我彻底改造了它。新功能:

  • 检查是否存在\chapter,如果不存在则跳过
  • 现在包括\part\paragraph\subparagraph
  • 变高/变低的水平被可用的最高/最低水平所取代
  • 包括\levelmultiup,向上延伸多个层级,例如从\subsubsection\chapter通过\levlemultiup{chapter heading}{3}

但仍有几件事要做:

  • 指定要使用的最大/最小级别(例如,如果你不\part喜欢
  • 当级别太低或太高时,可选择发出某种警告,以便进行更改(因此不再处于所需的层次结构中)
  • 以某种方式扩展各个轮廓元素,以便设置它们\currentlevel,例如比初始化更好的方法\setcounter{currentlevel}{7}

代码如下:

\documentclass{report}
\usepackage{ifthen}
\usepackage[margin=10mm]{geometry}
\usepackage{multicol}

\newcommand{\chex}{}

\ifthenelse{\isundefined{\chapter}}{\renewcommand{\chex}{N}}{\renewcommand{\chex}{Y}}

\newcounter{currentlevel}
% part                  = 7
% chapter               = 6
% section               = 5
% subsection        = 4
% subsubsection =   3
% paragraph         =   2
% subparagraph  =   1

\newcommand{\findnewlevel}[1]% uppity (-1 for level down, 0 for stay, 1 for up, 2-6 for multiup
{   \addtocounter{currentlevel}{#1}%
    \ifthenelse{\equal{\chex}{N}}%
    {   \ifthenelse{\value{currentlevel} = 6}%
        {   \ifthenelse{#1 > 1}{\addtocounter{currentlevel}{1}}{\addtocounter{currentlevel}{-1}}}{}}{}%
    \ifthenelse{\value{currentlevel} < 1}{\setcounter{currentlevel}{1}}{}%
    \ifthenelse{\value{currentlevel} > 7}{\setcounter{currentlevel}{7}}{}%
}

\newcommand{\levelchange}[2]% title, uppity
{   \findnewlevel{#2}%
    \ifthenelse{\value{currentlevel} = 1}{\subparagraph{#1}}{}%
    \ifthenelse{\value{currentlevel} = 2}{\paragraph{#1}}{}%
    \ifthenelse{\value{currentlevel} = 3}{\subsubsection{#1}}{}%
    \ifthenelse{\value{currentlevel} = 4}{\subsection{#1}}{}%
    \ifthenelse{\value{currentlevel} = 5}{\section{#1}}{}%
    \ifthenelse{\value{currentlevel} = 6}{\chapter{#1}}{}%
    \ifthenelse{\value{currentlevel} = 7}{\part{#1}}{}%
}

\newcommand{\levelup}[1]{\levelchange{#1}{1}}

\newcommand{\leveldown}[1]{\levelchange{#1}{-1}}

\newcommand{\levelstay}[1]{\levelchange{#1}{0}}

\newcommand{\levelmultiup}[2]{\levelchange{#1}{#2}} %title, uppity

\setcounter{tocdepth}{5} 
\setcounter{secnumdepth}{5}

\begin{document}

\pagestyle{plain}
\begin{multicols}{2}
\tableofcontents
\end{multicols}
\newpage

\setcounter{currentlevel}{7}

\levelstay{part}
\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\leveldown{subsubsec}
\leveldown{par}
\leveldown{subpar}
\levelstay{subpar}
\levelup{par}
\levelstay{par}
\levelup{subsubsec}
\levelstay{subsubsec}
\levelup{subsec}
\levelstay{subsec}
\levelup{sec}
\levelstay{sec}
\levelup{chap}
\levelstay{chap}
\levelup{part}

\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\leveldown{subsubsec}
\leveldown{par}
\leveldown{subpar}
\levelmultiup{part}{6}

\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\leveldown{subsubsec}
\leveldown{par}
\levelmultiup{part}{5}

\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\leveldown{subsubsec}
\levelmultiup{part}{4}

\leveldown{chap}
\leveldown{sec}
\leveldown{subsec}
\levelmultiup{part}{3}

\leveldown{chap}
\leveldown{sec}
\levelmultiup{part}{2}

\end{document}

结果如下:

上下文相关标题

相关内容