1

I cannot find any documentation at all on the samepage environment (aside from a cryptic comment on p229 of the LaTeX book that "The \samepage command still works, but is now of little use", which may or may not be relevant to the environment of the same name). This answer and the name of the environment suggests it should keep its contents on the same page, however it doesn't, as this MWE demonstrates:

\documentclass{article}
\usepackage{lipsum}
\usepackage{parskip}

\begin{document} \lipsum[4-8]

\begin{samepage} foo

bar \end{samepage} \end{document}

This breaks the page between the 'foo' and the 'bar'. Am I misusing the samepage environment? Is it documented anywhere?

This example requires xelatex rather than latex, but you can see the same in latex by adding a third line to the samepage environment.

David Carlisle
  • 757,742
richard
  • 720
  • 2
    please provide an example that shows your problem. The code you show makes no output at all. If I add \end{document} to get output, it makes a single page. – David Carlisle Jun 18 '23 at 22:15
  • @DavidCarlisle The only error was the missing \end{document}. Use xelatex and you'll see the problem. Question updated accordingly. – richard Jun 18 '23 at 22:21
  • Don't pay too much attention to me, but I think that what it should be on the same page is just the paragraph that doesn't fit, not he whole content like a minipage. That is, avoid the break the page within a paragraph, but not between two paragraphs. – Fran Jun 18 '23 at 22:22

1 Answers1

2

samepage makes it infinitely bad to break at every place it could reasonably access,so between lines of a paragraph (but not single line paragraphs) and at every place latex has control so list items, math displays etc.

Perhaps surprisingly tex has an \interlinepenalty primitive to control penalties at line breaks, but nothing to control the penalty for breaking at \parskip.

Latex's new para hooks finally give access to the start and end of a paragraph (see texdoc ltpara-doc) so you could add a penalty:

enter image description here

\documentclass{article}
\usepackage{lipsum}
\usepackage{parskip}
\AddToHook{env/samepage/begin}{%
\AddToHook{para/before}{\nopagebreak}%
\AddToHook{para/after}{\nopagebreak}%
}

\begin{document} \lipsum[4-8]

\begin{samepage} foo

bar

\end{samepage}

\end{document}

Ulrike Fischer
  • 327,261
David Carlisle
  • 757,742