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)?
\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:24Overfull \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:39Overfull \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 07:29Underfull ...andOverfull ...warnings or syntax errors. (La)TeX is simply not made with this in mind. – Martin Scharrer Aug 25 '11 at 07:51\hbadnessand\vbadnessto a high value, typeset, then consult the\badness. – Bruno Le Floch Aug 25 '11 at 08:48show_error_hookcallback, but you can't access too much information from it as far as I can see. At least resuming execution might be possible, but depends on the kind of error. – user202729 Mar 29 '22 at 06:24