0

I'm trying to modify https://tex.stackexchange.com/a/399944/2066 to adjust the spacing after sections headings which are not immediately followed by other section headings. However, if I replace the \hrule\vspace{1ex} with \vspace{-\baselineskip} in that answer, using the code

\documentclass{article}
\usepackage{lipsum}
\makeatletter
\def\@afterheading{%
  \@nobreaktrue
  \everypar{%
    \if@nobreak
    \@nobreakfalse
    \clubpenalty \@M
    \if@afterindent \else
                    {\setbox\z@\lastbox
                      \vspace{-\baselineskip}%
                    }%
                    \fi
                    \else
                    \clubpenalty \@clubpenalty
                    \everypar{}%
                    \fi}}
\makeatother
\begin{document}
\section{Test}

\subsection{Sub Test} \lipsum[4]

\subsection{Sub Test} \lipsum[4]

\section{Test}

\subsection{Sub Test} \lipsum[4]

\section{Test} \lipsum[4] \subsection{Sub Test}

\section[optional]{Test} \subsection[optional]{Sub Test} \lipsum[4]

\section{blblblb} \lipsum[4]

\end{document}

I get the very ugly

enter image description here

How do I get the first line of the paragraph to be moved up, rather than the second line?

Jason Gross
  • 2,135

1 Answers1

2

You are adding the negative space after the first line of the paragraph, you need to back out of horizontal mode with \par to add the negative space before the first line, but then take account that the indentation suppression needs to be re-done for the "real" paragraph.

So this does what is intended although hooking into the after-indent test is only a very loose approximation to testing for a following section head so this will not work in all cases.

enter image description here

\documentclass{article}
\usepackage{lipsum}
\makeatletter
\def\@afterheading{%
  \@nobreaktrue
  \everypar{%
    \if@nobreak
    \@nobreakfalse
    \clubpenalty \@M
    \if@afterindent \else
                    {\setbox\z@\lastbox
                      \par\vspace{-\baselineskip}\noindent
                    }%
                    \fi
                    \else
                    \clubpenalty \@clubpenalty
                    \everypar{}%
                    \fi}}
\makeatother
\begin{document}
\section{Test}

\subsection{Sub Test} \lipsum[4]

\subsection{Sub Test} \lipsum[4]

\section{Test}

\subsection{Sub Test} \lipsum[4]

\section{Test} \lipsum[4] \subsection{Sub Test}

\section[optional]{Test} \subsection[optional]{Sub Test} \lipsum[4]

\section{blblblb} \lipsum[4]

\end{document}

David Carlisle
  • 757,742