# Trapping LaTeX error/warning

Is there a way to trap an error/warning in LaTeX during compile time? I'm thinking of something similar to the VB script On Error Goto <blah>, but this time for LaTeX in something like \OnErrorExecute{<command>} and \OnWarningExecute{<command>}.

Sadly enough there are no standard error/warning-codes provided (AFAIK), since packages report warnings/error via the commands

\PackageError{<message>}
\PackageWarning{<message>}


By codes I mean, as an example,

• Warning 1: Underfull \hbox...;
• Warning 2: Overfull \hbox...;
• ...
• Error 1: No \begin{document};
• Error 2: Perhaps a missing \item...;
• Error 3: File ended while scanning..., etc.

since one would ideally want to condition on the type of error/warning that is produced. Understandably a new error/warning reporting mechanism would be required, since package authors are allowed to issue warnings/errors as they please. So, there warnings/errors could be made package-specific with some number prefix (say amsmath.warning.1 for warning 1 using the amsmath package).

If not in this version (probably), what about LaTeX3 (hopefully)?

-
Try e.g. using \si{\gram\kilo} in a document loading the siunitx package to see what LaTeX3 error messages look like: in particular, they have the module name and a name for the message. For documentation on it, you can look at the part of source3.pdf about the l3msg module. There are some possibilities to redirect some messages, and change their behaviour, but I find it unpractical, and suggestions on what is needed are welcome :). –  Bruno Le Floch Aug 24 '11 at 23:24
@Bruno: This is great news, but it will take me some time to get used to LaTeX3 syntax/usage. My motivation stems from Fit text into given box by adjusting the fontsize. Conditioning on an Overfull \hbox... warning might lead to making a better choice of how to fit the text into the given box dimensions. Does 'unpractical' refer to a personal preference? –  Werner Aug 24 '11 at 23:39
Overfull \hbox (and some others) is a TeX built-in warning, there is, imo, no way to detect it from inside. The rules how badness is calculated are all described in the TeXbook and others, so it should be possible to calculate the badness of the line manually and compare it to \tolerance. (I'm no expert at this.) –  Andrey Vihrov Aug 25 '11 at 7:29
You could catch LaTeX warnings and errors by redefining the mentioned macros, but I doubt it is possible to handle them correctly in any general case. It is not possible to catch TeX build-in messages like Underfull ... and Overfull ... warnings or syntax errors. (La)TeX is simply not made with this in mind. –  Martin Scharrer Aug 25 '11 at 7:51
Your Warning 1 and 2, and Error 3 come from TeX and cannot be redirected. On the other hand, things like Error 1 and 2 are controlled by LaTeX macros, and in principle could be redirected as Martin says by redefining macros, and in LaTeX3 we can hope to control them much better. Specifically on Under- or Overfull boxes, you should set the \hbadness and \vbadness to a high value, typeset, then consult the \badness. –  Bruno Le Floch Aug 25 '11 at 8:48

(Partly taking the comments to make an answer.)

There are various different types of errors, warnings and messages that come from a LaTeX run. At the TeX level, you can get a warning like Underfull \hbox... or and error such as File ended while scanning.... These cannot be altered at the LaTeX end.*

At the LaTeX level, most messages are generated using \PackageError and similar macros. You can redefine these, but an easier way would be to use the silence package. It provides a pre-built set of macros to do this redefinition in a selective way, thus allowing 'filtering out' of unwanted messages.

Turing to LaTeX3, the approach taken in the code there is to separate definition of messages (of all types) from their use. This means that each message has a 'name', which can be used to alter the behaviour of the message when it is given. Thus we might have

\msg_new:nnnn { module } { my-message } { Some~text } { Some~more~text }


to define a message, with

\msg_error:nn { module } { my-message }


when it is used. With no filtering, this will raise an error. However, we could alter the behaviour with

\msg_redirect_class:nn { error } { warning }


to turn all errors into warnings, or with

\msg_redirect_module:nnn { module } { error } { warning }


to alter just those messages for module, or even

\msg_redirect_name:nnn { module } { my-message } { warning }


to target just one message. As Bruno notes, the filtering behaviour may not currently be ideal, but I think that the separation idea is worth having. There is still a need to write a 'user level' interface for filtering in this way. (Note. Redirection can be applied before modules are loaded: useful to get rid of load-time messages. The mechanism used keeps the message text definition and and redirection separate.)

[*] Altering how the engine behaves is possible with LuaTeX. I'm not sure if there are appropriate hooks at the moment for the messages mentioned, but I'd imagine that this is possible. I'm assuming in the rest of my answer that we are talking about a cross-engine solution.

-
I'm looking forward to read more about "Turing to LaTeX3" in your blog. ;-) –  lockstep Sep 5 '11 at 17:20
Thanks for the very nice overview. I find it clearer than l3msg.dtx. Perhaps this should be reused somewhere in the LaTeX3 doc? Also, can messages be redirected before the module is loaded? –  Bruno Le Floch Sep 5 '11 at 17:55
@Bruno: Remember that l3msg is supposed to be a reference manual, so it's rather formalised. I'll see if I can improve any of it. On the load-order question, see the edit. –  Joseph Wright Sep 5 '11 at 18:57
@Joseph: I realized that this post doesn't fit very well in source3, but perhaps for the missing "learn to program in LaTeX3" document? I hadn't checked whether redirections were possible before loading, thanks for the info. On the fact that the filtering behavior is not ideal I'm mostly quoting you from a github issue. –  Bruno Le Floch Sep 6 '11 at 2:38