2

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.

3
  • 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. Commented Jun 18, 2023 at 22:15
  • @DavidCarlisle The only error was the missing \end{document}. Use xelatex and you'll see the problem. Question updated accordingly. Commented Jun 18, 2023 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. Commented Jun 18, 2023 at 22:22

1 Answer 1

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} 
3
  • This doesn't seem to work for me. I guess that means my LaTeX distribution (TeX Live 2020/Debian) isn't new enough. Commented Jun 18, 2023 at 23:18
  • 2020 is too old, you can of course add `\nopagebreak explicitly between foo and bar @richard Commented Jun 18, 2023 at 23:22
  • @Fran thanks... Commented Jun 18, 2023 at 23:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.