两个部分之间的条件 vspace?

两个部分之间的条件 vspace?

如果两个节标题彼此相连,即它们之间没有其他文本或其他内容,我想在它们之间添加额外的垂直空间。是否有一些条件 if 命令,例如使用 if@aftersection...?

\documentclass[12pt, a4paper]{memoir}
\usepackage[utf8]{inputenc}

\begin{document}

\chapter{Chapter title}
\section{A first section title}
\section{A second section title}
Just some text. There should be a conditional vspace between the first 
section and the second section if there is nothing else between them.

\section{A third section}
Some more text here.

\section{A fourth section}
Some more text.

\end{document}

答案1

一般来说,你必须适应所有可能的参数组合memoir\section。具体来说,它需要两个可选参数(用于目录和运行标题)。除此之外,还可以扫描以查看下一个标记是否正在\section使用\@ifnextchar

在此处输入图片描述

\documentclass{memoir}

\usepackage{xparse}

\let\oldsection\section
\makeatletter
\RenewDocumentCommand{\section}{s o o m}{%
  \IfBooleanTF{#1}
    {\oldsection*{#4}}
    {\IfValueTF{#2}
      {\IfValueTF{#3}
        {\oldsection[#2][#3]{#4}}
        {\oldsection[#2]{#4}}}
      {\oldsection{#4}}}%
  \@ifnextchar\section{\vspace{5\baselineskip}}{}%
}
\makeatother

\begin{document}

\tableofcontents

\chapter{Chapter title}
\section{A first section title}
\section{A second section title}
Just some text. There should be a conditional \verb|\vspace| between the first 
section and the second section if there is nothing else between them.

\section{A third section}
Some more text here.

\section{A fourth section}
Some more text.

\end{document}

这是另一个选择:

定义一个获取三个参数的宏\ifnextcommandis{<this>}{<true>}{<next>}。指定<this><true>,并假定宏后面的内容将是<next>宏:

\newcommand{\ifnextcommandis}[3]{%
  \def\thiscommand{#1}% Store this command
  \def\nextcommand{#3}% Store next command
  \ifx\nextcommand\thiscommand\relax
    #2%
  \fi
  \nextcommand}% Re-insert next command

\let\oldsection\section
\RenewDocumentCommand{\section}{s o o m}{%
  \IfBooleanTF{#1}
    {\oldsection*{#4}}
    {\IfValueTF{#2}
      {\IfValueTF{#3}
        {\oldsection[#2][#3]{#4}}
        {\oldsection[#2]{#4}}}
      {\oldsection{#4}}}%
  \ifnextcommandis{\section}{\vspace{5\baselineskip}}%
}

相关内容