[Tex/LaTex] How to word-wrap LaTeX warning/error output

documentclass-writingerrorspackage-writingwarnings

This question led to a new package:
hardwrap

In writing error messages and warnings for a package/class, I find it a pain to manually insert \MessageBreaks so that words don't get cruelly bisected because of TeX's max_print_line value (which typically is set to 79). In some cases it's not possible to predict where the \MessageBreak should go because a macro in the warning/error message may be expanded to any length of string.

I'd like to have a macro that would take my warning/error message, expand it (so that any macros are expanded to plain strings), and insert \MessageBreaks in the appropriate places based on the value of max_print_line and LaTeX's per-line prefixes (Package <XXX> Warning: and similar).

(If the value of max_print_line can't be accessed from within TeX, I'd settle for just implementing a greedy word-wrap algorithm with the default line length values.)

Follow-up: Will Robertson and I created the hardwrap package to achieve the desired affect.

Best Answer

I never published the solution I wrote some time ago, but perhaps I should. Here's the link to an example: http://gist.github.com/605753 It's written in the plain old LaTeX programming style; it was a while ago so there are probably a few things in there that I now know how to do in a better way.

There are three examples where the broken text is typeset at different line lengths, and then a fourth example in which it is printed to the terminal. This isn't expandable (if it's possible, it would be difficult to say the least), but for many purposes that won't be an issue.

The code is undocumented, but you should be able to see the gist of it by ignoring everthing before \begin{document}. If you find it useful, I suggest we write a package for CTAN with some documentation and a better interface.

Related Question