我正在尝试从头开始创建自己的分段命令。虽然我可以在任何地方换行,但我不知道如何在标题中添加填充以及在子练习之间添加间距。我尝试使用\bigskip
或\vskip
,但当一个分段命令直接跟在另一个分段命令后面时,会导致空间过大。\section
、\subsection
等能够合并连续的间距。有没有简单的方法来复制这种行为?
编辑:我通过使用 取得了进步\addvspace
。在我看来,它看起来不错,只是 an\exercise
和\subexercise
紧随其后的 a 之间的空格看起来不对。例如,参见练习 1 和子练习 (A) 之间的空格。在这种情况下,我更希望忽略较大的\addvspace
from 。\subexercise
以下是我目前所掌握的信息:
\documentclass[a5paper, 10pt]{article}
\usepackage[showframe]{geometry}
\usepackage[latin]{babel}
\usepackage{stix2}
\NewDocumentCommand\exercise{m}{
\par
% VERTICAL SPACING BEFORE HEADING
\addvspace{\baselineskip}
%
\noindent
{\bfseries\Large Exercise #1}
\par
% VERTICAL SPACING AFTER HEADING
\addvspace{0.25\baselineskip}
%
\noindent
\ignorespaces
}
\NewDocumentCommand\subexercise{m}{
\par
% VERTICAL SPACING BETWEEN SUBEXERCISES
\addvspace{0.5\baselineskip}
%
\noindent
\makebox[0pt][r]{(#1)\hspace\labelsep}%
\ignorespaces
}
\begin{document}
\exercise{1}
\subexercise{A}
Aliquam ornare viverra nunc.
Pellentesque et velit nulla.
Integer vestibulum nisi auctor justo malesuada, id facilisis tortor finibus.
\subexercise{B}
Maecenas sem turpis, congue vel cursus ac, ultricies vel ligula.
Sed rutrum blandit luctus.
\exercise{2}
Morbi risus dui, aliquet non vulputate ut, pulvinar nec velit.
Proin venenatis leo nibh, vel malesuada eros tempus sit amet.
Duis semper elit sit amet lectus luctus ultricies.
\subexercise{A}
Cras euismod est diam, eu iaculis quam euismod vel.
Vivamus facilisis nec nisi non gravida.
Etiam ipsum erat, tristique sit amet ante non, porta iaculis odio.
Pellentesque varius purus vel nisl pellentesque imperdiet at a leo.
\[
E = mc^2
\]
\subexercise{B}
Proin sed posuere nulla, sit amet imperdiet turpis.
Nam dictum tempus dictum.
Maecenas at rutrum nibh, vitae maximus magna.
Nulla et ante eu diam pretium facilisis.
\exercise{3}
\exercise{4}
\end{document}
答案1
正如评论中指出的那样,标题的正确实现并不那么简单。幸运的是,内置\@startsection
命令足够灵活,可以实现所需的布局,尽管使用起来有点困难。
\documentclass[a5paper, 10pt]{article}
\usepackage[showframe]{geometry}
\usepackage[latin]{babel}
\usepackage{stix2}
\usepackage{xparse}
\makeatletter
\NewDocumentCommand\exercise{m}{
\@startsection{}{1}{0mm}
{-\baselineskip} % before
{0.25\baselineskip} % after
{\normalfont\Large\bfseries}*
{Exercise #1}
}
\NewDocumentCommand\subexercise{m}{
\@startsection{}{2}{0mm}
{0.5\baselineskip} % before
{0mm} % after
{}*
{\makebox[0pt][r]{(#1)\hspace\labelsep}}
}
\makeatother
\begin{document}
\exercise{1}
\subexercise{A}
Aliquam ornare viverra nunc.
Pellentesque et velit nulla.
Integer vestibulum nisi auctor justo malesuada, id facilisis tortor finibus.
\subexercise{B}
Maecenas sem turpis, congue vel cursus ac, ultricies vel ligula.
Sed rutrum blandit luctus.
\exercise{2}
Morbi risus dui, aliquet non vulputate ut, pulvinar nec velit.
Proin venenatis leo nibh, vel malesuada eros tempus sit amet.
Duis semper elit sit amet lectus luctus ultricies.
\subexercise{A}
Cras euismod est diam, eu iaculis quam euismod vel.
Vivamus facilisis nec nisi non gravida.
Etiam ipsum erat, tristique sit amet ante non, porta iaculis odio.
Pellentesque varius purus vel nisl pellentesque imperdiet at a leo.
\[
E = mc^2
\]
\subexercise{B}
Proin sed posuere nulla, sit amet imperdiet turpis.
Nam dictum tempus dictum.
Maecenas at rutrum nibh, vitae maximus magna.
Nulla et ante eu diam pretium facilisis.
\exercise{3}
\exercise{4}
\end{document}