兩個部分之間的條件 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}}%
}

相關內容