1

Is it possible to force a newline in a specific position of a paragraph but only if this does not increase the total number of lines?

I tried by using \penalty with different values, and found that -10000 always forces the newline while -9999 does not enable the newline even if the total number of line breaks is the same.

Here is a possible test document:

\documentclass{article}
\newcommand\X[1]{
  \penalty-9999 % please break here if you need to break somewhere                     
  \textbf{(#1)}
}
\begin{document}
\begin{minipage}{5cm}
  Which number?
  \X{A} 5
  \X{B} 7
  \X{C} 4
  \X{D} 3
\end{minipage}
\end{document}
3
  • not really. If \penalty-9999 does not cause a line break then the white space in the paragraph must be very rigid such that the rest of the paragraph can not be stretched/shrunk to cause a break at that spot. If it infinitely bad to break there it won't break unless you adjust the horizontal space or tolerances Commented Oct 8, 2016 at 9:40
  • @DavidCarlisle so i could add some very stretchable glue to get the result? How that is achieved? I added some sample code... Commented Oct 8, 2016 at 9:42
  • \sloppy makes the white space stretchy which makes breaking at the penalty far more likely) (you may also want to increase \linepenalty so tex is less likely to make an extra line) Commented Oct 8, 2016 at 9:46

1 Answer 1

2

enter image description here

\documentclass{article}
\newcommand\X[1]{% keep egreg happy
  \unskip % remove any space before this command, so we add our own
  \hskip 0pt plus .2\hsize % end line up to 20% short
  \penalty-50 % please break here if you need to break somewhere  
  \hskip 0pt plus -.2\hsize\relax % cancel skip if don't break
  \space  % and do normal space                
  \textbf{(#1)}% as above
}
\begin{document}
\begin{minipage}{5cm}
  Which number?
  \X{A} 5
  \X{B} 7
  \X{C} 4
  \X{D} 3
\end{minipage}
\end{document}
1
  • doesn't work. that is, it will not prevent an increase in number of lines. to test: add three more items, and increase the width of the minipage to 5.2cm. then \renewcommand{\X}[1]{\textbf{(#1)}} and reiterate the example. with default breaking, 2 lines; with "controlled" breaking, 3 lines. Commented Oct 8, 2016 at 17:21

You must log in to answer this question.

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