0

In my document, sometimes there is a sentence like The diagram <tikz-cd diagram here> commute., in which case there is only one word after the displayed equation. I would like to make sure that this word does not appear on the top of the next page. I checked this question, but the solution there does not work in my document.

I'm aware that in the final version, this problem can be solved with \enlargethispage{\baselineskip}. However, documents like course notes are quite dynamic and they hardly reach a "final" state, so using and maintaining these \enlargethispage can be a problem.

The code below serves as a MWE. It is not an exact replication of my actual situation -- in fact, not like in this MWE, there are enough space to place that single word in my document, yet LaTeX decides that there should be a page break anyway.

\documentclass{article}

\usepackage[showframe,margin=0.5cm]{geometry}
\usepackage{tikz-cd}

\begin{document}

\vspace*{24.5cm}
% \enlargethispage{\baselineskip}

The diagram
\[
    \begin{tikzcd}
        \bullet & \bullet \\
        \bullet & \bullet
        \arrow[from=1-1, to=1-2]
        \arrow[from=2-1, to=2-2]
        \arrow[from=1-1, to=2-1]
        \arrow[from=1-2, to=2-2]
    \end{tikzcd}
\]
commute.

\end{document}

enter image description here


Below is the screenshot of my actual document (the space between the text and the footer is usually quite small, so there should be enough place for this single word):

enter image description here

Below is the result with a \enlargethispage:

enter image description here

11
  • 2
    What is the problem with saying \postdisplaypenalty=10000\relax immediately after \[ or before \] (for instance)? It certainly prevents the page break.
    – frougon
    Commented Jun 11, 2022 at 14:17
  • @frougon Thank you, the linked question suggested \postdisplaypenalty=100 which does not work for me, but \postdisplaypenalty=10000 works. However it moves the entire diagram alongside the paragraph before it to the next page, and leaves a huge space on the previous page. Perhaps \enlargethispage is the better solution after all...
    – Jinwen
    Commented Jun 11, 2022 at 14:23
  • This is beause the sample document leaves no other choice due to the \vspace*{24.5cm} followed by unbreakable material. The huge space on the second page is your \vspace*{24.5cm}...
    – frougon
    Commented Jun 11, 2022 at 14:24
  • 2
    I would reword the sentence and move the commute before the diagram. A sentence is easier to read if it isn't split by a large diagram. Commented Jun 11, 2022 at 14:50
  • 1
    @Jinwen I've typeset many documents with math displays and I've never incurred in such a problem. This is exactly a place for \enlargethispage.
    – egreg
    Commented Jul 11, 2022 at 17:26

1 Answer 1

0

Elements you want be typeset together can be enclosed in minipage but it sometimes produces unexpected gaps. It's probably better to use floats.

EDIT. In the example below, if I remove the second The diagram, everything is fit to one page as latex manages to shrink gaps in this particular example.

The example

\documentclass{article}

\usepackage[showframe,margin=0.5cm]{geometry}
\usepackage{tikz-cd}
\usepackage{graphicx}


\begin{document}

\noindent\includegraphics[width=\textwidth, height=24.5cm]{example-image}

The diagram

The diagram

\noindent
\begin{minipage}{\textwidth}
    \bgroup
        \centering
        \begin{tikzcd}
            \bullet & \bullet \\
            \bullet & \bullet
            \arrow[from=1-1, to=1-2]
            \arrow[from=2-1, to=2-2]
            \arrow[from=1-1, to=2-1]
            \arrow[from=1-2, to=2-2]
        \end{tikzcd}
        \par
    \egroup
    commute.
\end{minipage}

\end{document}
   
enter image description here enter image description here
2
  • Thank you for this. However this approach is not essentially better than the \postdisplaypenalty=10000 mentioned in the comments to the question, as they usually both leave a (potentially) huge space on the previous page. One is still recommended to: 1) use floats; 2) use \enlargethispage when necessary.
    – Jinwen
    Commented Jun 11, 2022 at 15:13
  • 1
    It is just an alternative way to already recommended approach with floats. I personally use them and re-phrase texts anytime I have to fit content to a page.
    – Celdor
    Commented Jun 11, 2022 at 15:17

You must log in to answer this question.

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