段落缩进:一个矛盾的要求

段落缩进:一个矛盾的要求
\documentclass{article}
\usepackage{blindtext}

\usepackage{titlesec}
\titleformat{\section}
  {\normalfont\scshape}{\thesection}{1em}{}
\titlespacing*{\section}
{0pt}{2ex plus 1ex minus .2ex}{1ex plus .2ex}

\newenvironment{myquote}[1]{%
  \small\par\vspace{3ex}
  \begin{minipage}{\dimexpr\linewidth -2\parindent\relax}%
    \def\myquoteauthorname{#1}%
  }{%
    \par\vspace{1ex}
    \noindent
    \hspace*{0.25\linewidth }%
      \rule{0.5\linewidth }{.4pt}
    \par\vspace{1ex}
    \centering
    \textsc{\myquoteauthorname}\par\vspace{3ex}
  \end{minipage}
  \par}

\begin{document}
\section*{Test Section}
\begin{myquote}{---Martina Navratilov}
Whoever said "It's not whether you win or lose that counts," probably lost.
\end{myquote}

\begin{myquote}{---Martina Navratilov}
Whoever said "It's not whether you win or lose that counts," probably lost.
\end{myquote}

\Blindtext[2]

\begin{myquote}{---17:31}
And do not kill your children for fear of poverty. We provide for them and for you. Indeed, their killing is ever a great sin.
\end{myquote}
\end{document}

在上面的 MWE 中,myquote环境应该像普通段落一样缩进。但是,带星号的版本表示titlesec第一段不应缩进。

我怎样才能排除myquote这个限制?

答案1

您可以有条件地插入\hspace*{\parindent}如果\if@nobreak为真且\if@afterindent为假,也就是说,我们处于某个部分(任何级别)的开始。

\@afterheading我还在情况属实的情况下添加了重置\if@nobreak,因此某一部分开头的引用后面不会立即出现分页符。

在下面的代码中,如果您更改\lipsum[1][1-11]\lipsum[1][1-9],下一部分将位于页面的底部,后跟引文和常规的两行文本。

如果我删除“重置”代码,引文将出现在页面末尾,而该部分的文本将出现在下一页。

\documentclass{article}
\usepackage{lipsum}

\usepackage{titlesec}
\titleformat{\section}
  {\normalfont\scshape}
  {\thesection}
  {1em}
  {}
\titlespacing*{\section}
  {0pt}
  {2ex plus 1ex minus .2ex}
  {1ex plus .2ex}

\makeatletter
\newenvironment{myquote}[1]{%
  \small\par\addvspace{3ex}
  \if@nobreak
    % we're at the start of a section
    % add the indent
    \if@afterindent\else\hspace*{\parindent}\fi
    % and instruct LaTeX to reset \@afterheading at the end
    \def\reset@nobreakatend{\@afterheading}%
  \else
    \def\reset@nobreakatend{}%
  \fi
  \begin{minipage}{\dimexpr\linewidth -2\parindent\relax}%
    \def\myquoteauthorname{#1}%
  }{%
    \par\vspace{1ex}
    \noindent
    \hspace*{0.25\linewidth}%
      \rule{0.5\linewidth }{.4pt}
    \par\addvspace{1ex}
    \centering
    \textsc{\myquoteauthorname}\par\vspace{3ex}
  \end{minipage}
  \par\nobreak\reset@nobreakatend}
\makeatother

\begin{document}
\section*{Test Section}
\begin{myquote}{---Martina Navrátilová}
Whoever said ``It's not whether you win or lose that counts,'' probably lost.
\end{myquote}

\lipsum[1][1-11] % with 1-9 the section will start on page 1

\begin{myquote}{---Martina Navrátilová}
Whoever said ``It's not whether you win or lose that counts,' probably lost.
\end{myquote}

\lipsum[1-2]

\section*{Test Section}
\begin{myquote}{---Martina Navrátilová}
Whoever said ``It's not whether you win or lose that counts,'' probably lost.
\end{myquote}

\lipsum[2-4]

\end{document}

在此处输入图片描述

相关内容