相关部分规范

时间:2010-01-14 17:27:44

标签: latex

是否可以指定相对于上一级别的\ section \ subsection \ subsubsection等级别?我在想的是像

\thissection The top level  
   \pushsection  
   \thissection The next level down  
   \thissection One more  
      \pushsection   
      \thissection Deeper  
   \popsection  
   \thissection At the same level and follows "one more"  

等。我的想法是我从内部写一份文件 out,即从更深层次开始,我不知道它上面会有多少层。这样就可以避免通过将\ subsection重命名为\ subsubsection等来进行大规模的重新调整。

BTW,谷歌搜索乳胶和“相关部分”导致几乎完全涉及滥用“相对”一词的命中;作者打算说“相关部分”。

感谢您的任何想法。

利安

3 个答案:

答案 0 :(得分:9)

您可以使用计数器和if-then-else逻辑来实现您的\pushsection\popsection\thissection

\usepackage{ifthen}
\newcounter{section-level}
\setcounter{section-level}{0}
\newcommand{\pushsection}{\addtocounter{section-level}{1}}
\newcommand{\popsection}{\addtocounter{section-level}{-1}}
\newcommand{\thissection}[1]
{
    \ifthenelse{\equal{\value{section-level}}{0}}{\section{#1}}{}
    \ifthenelse{\equal{\value{section-level}}{1}}{\subsection{#1}}{}
    \ifthenelse{\equal{\value{section-level}}{2}}{\subsubsection{#1}}{}
}

这与上面显示的完全相同,适用于3个级别的部分。当然,您应该做一些事情来处理超出范围的嵌套级别(例如崩溃TeX构建并打印警告)。

答案 1 :(得分:6)

我在其他答案中提出了一些建议。

而不是堆栈隐喻,命令是\leveldown\levelup\dynsection

asection环境存在,我添加了\gotochapterlevel

\leveldown\levelup使用可选参数一次跳转多个级别。

我试图尊重不同文档类及其名称的最低和最高级别,但实际上这很糟糕。所以现在你最好把你最喜欢的切片层次结构变成容易的东西。

我希望有人能发现这个有用,或者甚至可以改进它(不应该那么难,真的):

Relative Sectioning Package

答案 2 :(得分:1)

给定以下递归宏多次生成字符串:

\newcommand{\Repeat}[2]{% #1=number of times, #2=what to repeat
  \ifnum\the\numexpr#1\relax>0%
    #2%
    \expandafter\Repeat\expandafter{\the\numexpr#1-1\relax}{#2}%
  \fi%
}%

和一个全局计数器,它会说明我们需要预先添加多少个“子”:

\newcounter{section-level}
\setcounter{section-level}{0}

然后你有了宏:

\def\pushsection{\addtocounter{section-level}{1}}
\def\popsection{\addtocounter{section-level}{-1}}

\def\thissection#1{%
    \csname\Repeat{\value{section-level}}{sub}section\endcsname%
}%

但是,我也会考虑定义一个推送和弹出的环境:

\newenvironment{asection}[2][\defopt]{% #1=toc entry (optional), #2=heading
  \def\defopt{#2}%
  \thissection[#1]{#2}%
  \pushsection%
}{%
  \popsection%
}%

并将您的示例重写为:

\begin{asection}{The top level}
  \begin{asection}{The next level down}\end{asection}
  \begin{asection}{One more}
    \begin{asection}{Deeper}\end{asection}
  \end{asection}
  \begin{asection}{At the same level and follows "one more"}\end{asection}
\end{asection}