1

How can I prohibit page break at \\? I tried \\* and it sometimes worked but sometimes not.

Here is a non-working example:

\documentclass{article}

\usepackage{geometry}
\geometry{textheight=2cm}

\begin{document}

1st line

2nd line

% Expected page break is here

3rd line\\*
4th line\\*% This line is on the 1st page
5th line% But this line appears on the 2nd page

\end{document}

I want to use \\ in description of file format. I can't use verbose or lstlisting because I want to insert special elements. Here is a simplified example:

\documentclass{article}

\usepackage{textcomp}

\newcommand{\meta}[1]{\textnormal{#1}}
\newcommand{\variable}[1]{\textnormal{\textlangle\textit{#1}\textrangle}}
\newenvironment{file}{\begin{quote}\normalfont\ttfamily}{\end{quote}}

\begin{document}

\begin{file}
  [\variable{section}]\\
  \variable{key} = \variable{value}\\
  \variable{key} = \variable{value}\\
  \meta{...}
\end{file}

\end{document}
a user
  • 1,502
  • 2
    Breaking lines of text with \\\ is almost never right. If you were to explain what your use case is, someone might be able to suggest a better approach. – Ian Thompson Nov 18 '14 at 13:27
  • @IanThompson Thanks for a comment. I've added explanation. – a user Nov 19 '14 at 02:36

2 Answers2

4

\\* adds a penalty of 100000 (infinite) to break at that point but if something else allows breaking, or there is no other choice, it can be overridden. in this case the vertical list looks like

....\glue(\rightskip) 0.0
...\penalty 10000
...\penalty 150
...\glue(\baselineskip) 5.05556

so immediately after the 10000 is a widow penalty of 150 which is there to discourage breaks at this point but in this case allows a break.

Almost always use of \\ within the document outside of its use in alignments is wrong so this isn't normally an issue but if you absolutely don't want a block of text to break, put it in a box:

\documentclass{article}

\usepackage{geometry}
\geometry{textheight=2cm}

\begin{document}
%\showoutput
1st line

2nd line

% Expected page break is here

\noindent\parbox[t]{\textwidth}{%
3rd line\\*
4th line\\*% This line is on the 1st page
5th line% But this line appears on the 2nd page
}

\end{document}

In this case it all fits on the page.

David Carlisle
  • 757,742
1

What happens in your case is a simple page break within paragraph. This is not optimal, however, if the whole paragraph is moved to the next page the first one would be too empty (and thus get a penality).

What you could do to fix this in your case is to set the penality for the single line on the next page to a large number, cf. How do I prevent widow or orphan lines

\widowpenalty10000