Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need a LaTeX macro which can be used by this:

\IfFull{TEXT}{THEN}{ELSE}

It would typeset TEXT in a \vbox (which it will discard), and if there was an overfull hbox or an underfull hbox, it would run THEN, otherwise it would run ELSE. It wouldn't report the Overfull \hbox or Underfull \hbox warnings on the console or in the log file.

Is there such a macro already? If not, how could it be defined?

If this is too complicated, it would be enough to do it for a single paragraph.

Minimal working example, which gives the wrong result, because it always executes #3, but in the example it should have executed #2:

\documentclass{article}
\def\IfFull#1#2#3{#3}
\begin{document}
\IfFull{\hsize1pt\noindent zzzzz xxxxx}{CORRECT}{BAD}
\end{document}

This should create document with the word CORRECT in it, because #1 has an overfull hbox.

share|improve this question
    
is luatex a possibility? (I'm not sure it's really feasible otherwise) –  David Carlisle Feb 19 at 0:26
    
With a positive attitude everything is possible :-). Thanks for the \lastbox solution. Feel free to give a luatex solution, but most probably we'll go with \lastbox to reduce dependencies. –  pts Feb 19 at 1:51

1 Answer 1

up vote 7 down vote accepted

Like any code using a \lastbox loop this will fail badly if there is any unexpected material in the vertical list being measured. specials, inserts, writes, rules, will "hide" any material above the unremovable item.

\documentclass{article}

\makeatletter
\def\IfFull#1{%
  \global\let\@IfFull\@secondoftwo
  \setbox\z@\vbox{%
\hbadness\maxdimen
\hfuzz\maxdimen
#1\par
  \loop
  \unskip\unpenalty\unskip\unpenalty
  \setbox\z@\lastbox
  \ifvoid\z@
  \else
     \setbox0\hbox to \hsize{\unhbox\z@}%
     \ifnum\badness>\tolerance
       \global\let\@IfFull\@firstoftwo
       \typeout{bad: \the\badness}% debug message only
     \fi
  \repeat}%
  \@IfFull}

\begin{document}
\IfFull{\hsize1pt\noindent zzzzz xxxxx}{CORRECT}{BAD}
\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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