4

I have an environment that needs to ensure that its body always starts on a new line, but that doesn't add extra blank space when doing that.

What I have works well, except with a few corner cases relating to \paragraph{...} without any text between it and my environment.

\documentclass[12pt]{article}

\begin{document}

\newenvironment{foo}{ \par }{ }

\section{Foo} \begin{foo} OK \end{foo}

\paragraph{Bar} chart. \begin{foo} OK \end{foo}

\paragraph{Baz} \begin{foo} Bad \end{foo}

\end{document}

I can force that to start a new line by adding a ~\\ in the right spot, but that also adds a blank at the start of all the other uses that were correct.

BCS
  • 251

1 Answers1

3

We have an issue somewhere to provide a better interface, but you can detect a pending run-in head or list item and flush it out with \leavevmode

enter image description here

\documentclass[12pt]{article}

\begin{document} \makeatletter \newenvironment{foo}{% \if@inlabel\leavevmode \fi \if@noskipsec\leavevmode \fi \par }{ } \makeatother \section{Foo} \begin{foo} OK \end{foo}

\paragraph{Bar} chart. \begin{foo} OK \end{foo}

\paragraph{Baz} \begin{foo} Bad \end{foo}

\end{document}

David Carlisle
  • 757,742
  • 2
    Maybe it requires detailed explanation. It seems somewhat in-logical to set \leavevmode in order to be the next text in a new paragraph. If TeX is in vertical mode already then next text begins a new paragraph and nothing more is needed (and \par suggested by OP does nothing). If TeX is in horizontal mode then \leavevmode is irrelevant. \par should be sufficient. Why it doesn't in this case of LaTeX macro \paragraph is very interesting question. – wipet Oct 14 '23 at 12:23
  • @wipet TeX is in vmode but the run-in heading is in a box ready to be inserted by \everypar. so \par does nothing but you need to start a paragraph to fire \everypar, then the \par can end the paragraph after the inserted head. – David Carlisle Oct 14 '23 at 14:22
  • 1
    I knew that LaTeX was unnecessarily complicated. – wipet Oct 14 '23 at 15:39
  • 1
    @wipet I knew you would say that:-) – David Carlisle Oct 14 '23 at 15:57
  • That seems to work. Regarding unnecessary complexity; the fact I'm far from sure that accounts for every place I might use that environment, and have no idea how I'd get that list places, or how I'd have things tell me if I bump into one, or how I'd figure out the correct test if I found another corner case... I kinda like TeX being so stable, but I wish it had chose a later era of language design to freeze in. – BCS Oct 14 '23 at 16:36
  • 1
    @BCS basically those two cases are the only ones needed. You don't like the 80's ? :-) – David Carlisle Oct 14 '23 at 18:12
  • @DavidCarlisle According to the 'pidia TeX started life more like in the 70's, and didn't even try to be Turing complete until ~5y later. At a guess its architecture wouldn't have even been close to groundbreaking even in the late 60's. Not that latest-and-greatest should be a goal, but it sometimes feels more like ASM (with a pre-processor) than a basic programming language. – BCS Oct 14 '23 at 19:05
  • @BCS sure but wipet was complaining about latex specifically,which is a child of the 80s, and tex 78 isn't really recognisable as what is now tex, which really started with tex82 – David Carlisle Oct 14 '23 at 19:12