3

Using LuaLaTeX in texlive2017, but I assume that this would apply generally.

Question: Is it possible to set a custom error message, that will be shown when a bad value is used as the argument to a length? The custom message would be defined as a string, which can be changed at various places in the document.

The usual error message is ``Missing number, treated as zero''. In my case, using a custom document class, I want to be able to guide the user to a specific place in the documentation. MWE:

\documentclass{article}
\newlength\mylength
\def\notalengthvalue{Hello!}
\def\isalengthvalue{20.6pt}
\begin{document}
\def\myerrmsg{Read documentation section 1.2.3.}
\setlength\mylength{\notalengthvalue} % error message is \myerrmsg
\def\myerrmsg{Read documentation section 3.7.2.}
\setlength\mylength{\notalengthvalue} % error message is \myerrmsg
\setlength\mylength{\isalengthvalue} % no error
\end{document}

If TeX had a Try/Catch routine, that would be easy. Lacking Try/Catch, is there another way to do it? That is, one that does not involve brute force pre-parsing of the argument for all possible strings that might be a length.

  • not really, this is a low level tex parse error not an error generated by latex, so you can not customise the error, you can just try to avoid the error condition, as you say. But redefining \setlength to do that may well be incompatible with all sorts of things, setlength is used in a lot of places... – David Carlisle Jan 7 '18 at 23:19
  • @DavidCarlisle Sounds like I'm out of luck. I was not planning on redefining \setlength. Rather, I thought that the error message might be contained in a command string such as \@err@when@bad@length@foo that could be renewed. – user139954 Jan 7 '18 at 23:45
  • 1
    no it is in the pascal source of tex-the-program – David Carlisle Jan 7 '18 at 23:48
  • @DavidCarlisle That's actually a complete answer (Pascal code). Post for acceptance. Indirectly, it pre-answers some other question I might have had, along the same topic of customizing standard TeX messages. Others might be interested in knowing this. – user139954 Jan 8 '18 at 1:37
3

This is a low level tex parse error the wording comes from tex-the-program, traditionally it would have been visible in the pool file, but modern implementations do not use a separate pool file).

As such it can not be changed from tex code, all you could do is modify (say) \setlength to detect non-length input and avoid attempting a primitive length assignment.


For luatex you can do this:

\documentclass{article}

\directlua{
function my_error ()
local e =status.lasterrorstring
if(e=='! Missing number, treated as zero') then
  texio.write_nl ('! Missing number,\string\n\space\space perhaps you should look at page 6')
else if(e=='! Illegal unit of measure (pt inserted)') then
  texio.write_nl ('! Illegal unit of measure\string\n\space\space you really should look at page 6 of the documentaion')
else
  texio.write_nl(e)
end
end
end
luatexbase.add_to_callback('show_error_message',my_error,'error message hook')
}
\begin{document}

\setlength\oddsidemargin{x}

\newcommand\cos{\sin}

\def  x {y}

\end{document}

which produces a terminal output of:

! Missing number,
  perhaps you should look at page 6.
<to be read again> 
x
l.19 \setlength\oddsidemargin{x}

? 
! Illegal unit of measure
  you really should look at page 6 of the documentaion.
<to be read again> 
x
l.19 \setlength\oddsidemargin{x}

? 

! LaTeX Error: Command \cos already defined.
               Or name \end... illegal, see p.192 of the manual.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.21 \newcommand\cos{\sin}

? 
  • actually this is the answer for classic tex, you can probably do more in luatex with the show_error_hook and show_error_message callbacks, I'll delete this if someone posts a lua answer or I may answer later but not today – David Carlisle Jan 8 '18 at 1:46
  • If the Lua answer is relatively no-brainer, that would be good. But when I asked the question, I envisioned that it was a simple matter of knowing which TeX command could be re-set. – user139954 Jan 8 '18 at 3:40
  • @RobtAll updated for luatex – David Carlisle Jan 8 '18 at 8:08
  • I will give your Lua code a good, long test. If I don't report back, it means that it works great (I trust you). – user139954 Jan 8 '18 at 15:19
  • It seems that if I wish to change the message from time to time, I have to use luatexbase.remove_from_callback to get rid of a prior message, before adding a new one. The tactic of passing in an argument such as \mymessage only works if the message is defined prior to the definition of the Lua code. This is still workable, but I would probably wish to gather the Lua code in a component *.sty file, so that it does not clutter the main code. Then I would call the separate LaTeX/Lua macros by name, when needed. More for me to investigate! – user139954 Jan 8 '18 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy