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.

0

1 Answer 1

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}
8
  • 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, 2023 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. Oct 14, 2023 at 14:22
  • 1
    I knew that LaTeX was unnecessarily complicated.
    – wipet
    Oct 14, 2023 at 15:39
  • 1
    @wipet I knew you would say that:-) Oct 14, 2023 at 15:57
  • 1
    @BCS basically those two cases are the only ones needed. You don't like the 80's ? :-) Oct 14, 2023 at 18:12

You must log in to answer this question.

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