5

So, I'm trying to make an environment that is always padded above and below by a certain vertical space, no more and no less (unless the previous or following elements require even more space). My attempt is

\newenvironment{hello}{\addvspace{\baselineskip}}{\addvspace{\baselineskip}}

If I then have two hello environments one after another, then the second \addvspace happily does nothing, as desired. Similarly, after a section title, I get the desired behavior.

\documentclass{article}

\usepackage{lipsum}
%\usepackage{titlesec}
\newenvironment{hello}{\addvspace{\baselineskip}}{\addvspace{\baselineskip}}

\begin{document}

\section{Hellos}
\begin{hello}
    \lipsum[2]
\end{hello}
\begin{hello}
    \lipsum[3]
\end{hello}

\end{document}

But when using titlesec, there is too much space between the section title and the first hello environment. It looks like there's a definite space that goes along with the title that isn't visible to the first \addvspace after it, so \addvspace adds more space.

Any ideas on how to fix this? (Reasons why my entire approach is crappy are also welcome.)

6
  • It's a small bug in titlesec which uses \vspace instead of \vskip so it also inserts a \vskip0pt which prevents \addvspace to work correctly. Dec 6, 2011 at 5:57
  • @PhilippeGoutet. Not a bug. As the LaTeX Companion explains (p. 858), \addvspace is not in fact a user-level command and there are some rather surprissing effects. Dec 9, 2011 at 10:31
  • @JavierBezos: I consider it a bug because just loading titlesec changes the behavior of how \section are spaced. Is there a reason why you used \vspace instead of \vskip? The LaTeX kernel never uses \vspace. Dec 10, 2011 at 12:37
  • @PhilippeGoutet. Of course it changes the behaviour. This is the whole point of titlesec! Using \vspace (which, by the way, is used in \smallskip, \smallbreak and the like), can be more or less sensible and more or less questionable (and the same for \vskip, on the other hand), but it's not a bug. And the reason is I though lists after titles were better spaced (again, a questionable decision, but not a bug). Dec 11, 2011 at 18:47
  • [I saved a first version too soon and I could not finish the answer.] And the reason is I thought lists after titles were better spaced and it was coherent with \...skip (again, a questionable decision, but not a bug). Dec 11, 2011 at 18:56

1 Answer 1

5

For its spacing, the package titlesec uses \vspace which does not work well with \addvspace. You can use the etoolbox package to patch the internal \ttl@straight@ii command in order to replace the \vspace by a \vskip.

result of the code

\documentclass{article}

\usepackage{lipsum}
\usepackage{etextools}

\usepackage{titlesec}
\makeatletter
\patchcmd{\ttl@straight@ii}{\vspace{\@tempskipb}}{\vskip \@tempskipb}{}{}
\makeatother

\newenvironment{hello}{\addvspace{\baselineskip}}{\addvspace{\baselineskip}}

\begin{document}

\section{Hellos}
\begin{hello}
    \lipsum[2]
\end{hello}
\begin{hello}
    \lipsum[3]
\end{hello}

\end{document}
2
  • I'm curious why you said "You can use the etoolbox package" in your answer, but then the example uses etextools instead (which presumably pulls in etoolbox itself)?
    – dsedivec
    May 27, 2017 at 16:15
  • @dsedivec: a mistake on my part, etoolbox is all that is needed. May 28, 2017 at 7:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .