1

Is there a way to stop pagebreaks inside paragraphs in a document with \obeylines? I have found this question: How to avoid page-breaks inside paragraphs?, but none of the solutions suggested there seem to work with \obeylines. An alternative for \obeylines would also help, but I need a global command, not \\ after every line of text. (I am making a songbook where every line has a hard linebreak, so \\ would be very impractical)

Here is an example of what I tried:

\documentclass[a6paper]{article}

\usepackage[margin=100pt]{geometry}
%\usepackage[defaultlines=100]{nowidow}

\widowpenalties 1 10000
\raggedbottom

%\clubpenalties 1 10000

%\interlinepenalty 10000

\begin{document}

\obeylines

TITLE
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
~\par
bar
bar
bar
bar
bar
bar
bar
bar
\end{document}

As is, the bars are split between the two pages of the document. When I tried substituting \obeylines with a \\ on every line, the \widowpenalties command worked, but with \obeylines it doesn't.

1 Answer 1

1

A page break is normally allowed after \par, which is what \obeylines issues.

You can use the (new) hook mechanism:

\documentclass{article}

\usepackage[a6paper,margin=100pt]{geometry}

\newcommand{\normalpar}{\par\allowbreak}

\begin{document}

\obeylines\AddToHook{para/after}[obey]{\nobreak}

TITLE
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
~\normalpar
bar
bar
bar
bar
bar
bar
bar
bar

\RemoveFromHook{para/after}[obey]

\clearpage

\obeylines

TITLE
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
~\par
bar
bar
bar
bar
bar
bar
bar
bar

\end{document}

The second example shows that \par regains its standard properties.

enter image description here

Better define an environment.

\documentclass{article}

\usepackage[a6paper,margin=100pt]{geometry}

\newenvironment{obeynobreak}
 {%
  \par % ensure vertical mode
  \AddToHook{para/after}[obey]{\nobreak}
  \obeylines
 }
 {\RemoveFromHook{para/after}[obey]}
\newcommand{\normalpar}{\par\allowbreak}

\begin{document}

\begin{obeynobreak}
TITLE
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
~\normalpar
bar
bar
bar
bar
bar
bar
bar
bar
\end{obeynobreak}

\clearpage

\obeylines

TITLE
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
~\par
bar
bar
bar
bar
bar
bar
bar
bar

\end{document}
2
  • For some reason, I get an undefined control sequence error on AddToHook, not really sure why; you mentioned it is a new thing?
    – aky-her
    Sep 2, 2022 at 11:48
  • 1
    @aky-her You need an up-to-date (at least 2021) version of LaTeX.
    – egreg
    Sep 2, 2022 at 11:58

You must log in to answer this question.

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