I'd like to test if the last paragraph has any overfull line.

The naive approach of looking at the \badness once the paragraph is finished does not work, as it will only give the badness of the last line. For instance, the following code shows a badness of 1000000 after Hello, world! but 0 after Hello, world, X!, simply because the last line (X!) is less than 15pt wide.

Hello, world!\par   \immediate\write16{BADNESS=\the\badness} % 1000000
Hello, world X!\par \immediate\write16{BADNESS=\the\badness} % 0

I use this to do trial typesettings with different line-lengths until finding a line-length which is big enough to avoid overfull lines. Afterwards, I rescale the result to make it fit in the page. This would of course be a terrible idea for text, but the material I'm typesetting is maths over which I have no control and which may occasionally not split nicely into short lines. It is better to get something that is hard to read (small) rather than have it get out of the page. Because of this application, I'm also interested in a way to know the longest line in a paragraph.

  • 2
    Happy new year! Not too sure if it works ok, but give it a try github.com/yannisl/phd/blob/master/test-badness.tex
    – yannisl
    Commented Jan 2, 2015 at 9:05
  • @YiannisLazarides Thanks! If I understand correctly you split the paragraph into lines (with \lastbox, \unskip, \unpenalty) and re-typeset each line into a box of width \hsize to test their badness? You're actually more than half-way to an answer to the question «Obtain \badness or glue adjustment for each line». (I'd say it's more natural to initialize \maxbad globally since later assignments are global.) Commented Jan 2, 2015 at 9:28
  • Why is there \hss in \eatlines? It is not there in \printlines. Commented Jan 2, 2015 at 9:42
  • It is not needed. This is some old code from Will Robertson. \begin{minipage}[t]{55pt} \mbox{} \trypar\hyphenpenalty=-500\looseness=1.
    – yannisl
    Commented Jan 2, 2015 at 10:20
  • 2
    1. While not providing information in the log file, \hfuzz=0pt \overfullrule=7pt would give a visual clue for overfull lines in the document. 2. The ctan.org/pkg/adjustbox package might be helpful to solve the original problem (fit some math to some width).
    – Stephen
    Commented Jun 21, 2015 at 14:08

1 Answer 1


Since \badness is overwritten at each line break, the only way to keep track of line badness is to let TeX format a paragraph and then imitating it and reformat it manually line by line keeping track of what we're interested in.

This kind of decomposition-recomposition is not particularly hard to do, the only annoying part being restoring exact vertical spacing (in fact I still haven't got it quite right). A few questions already show the technique, see putting a box around each line of text in a paragraph and the linked ones.

But I won't bore you with some straightforward code cleanup from another answer. It seems to me that it would be most interesting (= fun for me) to automate the process you are doing by trial and error, instead of just facilitating it.

I wrote a macro that takes a paragraph and a line width range. Exploring it with a given step, it searches for the one minimizing the maximum badness present in the paragraph. Then it sets the paragraph at the optimal width, rescaled to fit the original one.

Here is the result:


On the left there's the ratio of original and optimal line width, i.e. the scale factor. On the right there's the badness of each line; the maximum is bold.

This is a bit more than what you asked for, but to do it I had to find the badness of each line (and keep track of the maximum). This is exactly what you have to do to find wether a paragraph contains an overfull line (by checking the maximum or maybe more elegantly breaking the recursion if one is found). The basic technique is there.

However I think that it's a bit more convenient to have something to search for the optimal width and output it instead of you doing it manually.

Also, there's another answer you might be interested in here.

Here is the code:

% Unessential but I don't want to
% reimplement \resizebox from scratch.
\input miniltx
\input graphics.sty

% internal variables

% line width variables:
\newdimen\lwMIN % range minimum
\newdimen\lwMAX % range maximum
\newdimen\lwINC % increase step
\newdimen\lwCUR % currently evaluated
\newdimen\lwOPT % optimum

% line badness variables:
\newcount\lbCUR % current line badness
\newcount\lbMAX % max of the paragraph
\newcount\lbOPT % optimum

% \fit formats the following paragraph
% finding the line width in the given
% range by minimizing the maximum line
% badness and rescaling in to fit.
  % read parameters
  % find optimum
  % format optimum
  % place optimum

% \find searches line widths in the
% given range to find the optimum to
% format the passed paragraph
  % prepare measure at current line width
  % measure paragraph's maximum badness
  % if in valid range
    % update optima
    % evaluate next line width

% \eat eats lines of the last paragraph
% either measuring its maximum badness
% or re-setting it with annotations.
  % eat line and measure its badness
  \setbox0=\hbox to \hsize{\unhcopy\linebox}
  % if measuring update the maximum badness of the paragraph
  % unless paragraph is over
    % nest call and process preceeding line
    % unless measuring
      % set current line
      % if verbose annotate badnesses


\input zapf\par\fit{200pt}{600pt}{.1pt}\input zapf\par
\input zapf\par\fit{250pt}{350pt}{10pt}\input zapf\par
\input zapf\par\fit{600pt}{800pt}{50pt}\input zapf\par
\input zapf\par

You must log in to answer this question.

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