0

I defined a new command that would "clamp" text together to prevent it from breaking across a page.

I wanted to be able to switch from clamped to unclamped text with no jarring issues with line spacing, and allow myself to have to clamp globs of text next to each other (on top of one another; I don't intend to transition from one clamped block of text to another within a line) with normal line spacing.

Adjusting the spacing at first seemed to work but I realized that it only worked if the clamped paragraph was not too close to the end or beginning of a page; if the leftover page height is just too small it seems that a clamped paragraph can get pushed up or down slightly.

Here is my command and its support so far:

\newlength{\parsk}
\newlength{\parin}
\setlength{\parsk}{\parskip}
\setlength{\parin}{\parindent}
\newbox\linebox
\setbox\linebox=\hbox{gh}
\newcommand{\clasp}[1]{\noindent\begin{minipage}[b]{\textwidth}
\setlength{\parskip}{\parsk}
\setlength{\parindent}{\parin}
\vspace{1\dp\linebox}
#1
\end{minipage}}

Does anyone know how to fix this problem?

Here is a test document that shows a situation in which the problem occurs:

\documentclass{article}

\newlength{\parsk}
\newlength{\parin}
\setlength{\parsk}{\parskip}
\setlength{\parin}{\parindent}
\newbox\linebox
\setbox\linebox=\hbox{gh}

\newcommand{\clamp}[1]{\noindent\begin{minipage}[b]{\textwidth}
\setlength{\parskip}{\parsk}
\setlength{\parindent}{\parin}
\vspace{1\dp\linebox}
#1
\end{minipage}}

\newcommand{\text}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque dignissim justo eget est viverra eleifend. Fusce sed felis vulputate, tempor lacus et, iaculis nisi. Sed ultricies mi neque, ut euismod dui rhoncus id. Cras fringilla, turpis in fringilla rutrum, nulla elit porttitor tellus, eu finibus sapien turpis hendrerit odio. In malesuada metus ac justo placerat fermentum. Cras ac turpis mattis, ornare arcu lobortis, cursus nisi. Vestibulum nec cursus ante. Vivamus quis consectetur mauris.}
\begin{document}

\text

\clamp{\text}

\text

\text

\clamp{\text}

\clamp{\text}

\clamp{\text}
\end{document}
3
  • I googled how to mark code and edited it at almost exactly the same time you did looks like you hit "send" a little faster than me. – Bailis Cremey Sep 10 '17 at 7:42
  • Don't worry, I'm glad that you were searching for how to format correctly (many users don't). – CarLaTeX Sep 10 '17 at 7:43
  • Related/duplicate: Absolutely, definitely, preventing page break – egreg Sep 10 '17 at 9:55
1

The default parskip and spacing around headings and almost all other vertical space in article class is stretchy and so will stretch or shrink to account for large boxes on a page. Also getting the space above and below minipages to match the space that would have been used if the minipage had not been there is tricky (almost impossibly so without major changes to latex).

So I would not use a box to keep your text together but instead arrange the penalties stop line breaking within the marked paragraph by using \begin{samepage} ... \end{samepage}.

\documentclass{article}


\newcommand{\clamp}[1]{{\par\samepage#1\par}}


\newcommand{\text}{Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque dignissim justo eget est viverra eleifend. Fusce sed felis vulputate, tempor lacus et, iaculis nisi. Sed ultricies mi neque, ut euismod dui rhoncus id. Cras fringilla, turpis in fringilla rutrum, nulla elit porttitor tellus, eu finibus sapien turpis hendrerit odio. In malesuada metus ac justo placerat fermentum. Cras ac turpis mattis, ornare arcu lobortis, cursus nisi. Vestibulum nec cursus ante. Vivamus quis consectetur mauris.}
\begin{document}

\text

\clamp{\text}

\text

\text

\clamp{\text}

\clamp{\text}

\clamp{\text}
\end{document}
4
  • Wow, such a simple solution! I wonder why a minipage solution was given on the other threads asking how to prevent page breaks. Is surrounding the paragraph and commands with another pair of brackets necessary here? – Bailis Cremey Sep 10 '17 at 15:49
  • @BailisCremeywell yes otherwise the rest of the document will be in the scope of \samepage – David Carlisle Sep 10 '17 at 15:51
  • It seems in some cases the penalty imposed by \samepage isn't high enough. Is there a way to change this? – Bailis Cremey Sep 10 '17 at 19:10
  • @BailisCremey well that depends: all the penalties it sets are 10000 so as high as possible but it may be that some construct you use in the content adds a break point that is not redefined by \sampepage especially if anything uses low level tex primitves that are not affected by the latex declaration. That is the advantage of a box like minipage that never breaks but you almost always pay a price in worse spacing. If you can add an actual failing case it's usually easy to debug but hard to give general advice. – David Carlisle Sep 10 '17 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.