0

I compile using lualatex (2023, Linux) but I believe this is a general question that also applies to pdflatex:

In my usage situation, a horizontal overfull badbox is indeed bad. So there are overfull rules, messages in the log file, and I can even use the wonderful lua-typo package when needed.

But what I want to do is simply count the number of overfull horizontal lines. This is in running text (possibly also footnotes), but not equations, tables, or graphics. If there are no badboxes, good. If there is at least one, I want to provide a prominent message, reminding the user to look at the log file. If I can get the count of badboxes, then I know how to create the message. In this MWE, instead of a prominent message, it simply types the count:

\documentclass{article}
%\usepackage{fontspec} % Uncomment if compiling with lualatex
\newcounter{hoverfulls}
% This is the kind of code I seek:
%%%%% \AtOverfullHorixBadbox{\addtocounter{hoverfulls}
\AtEndDocument{\typeout{Detected \value{hoverfulls} overfull lines.}}
\begin{document}
LaTeX puts me to sleep, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz\par
But expl3 makes me scream eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\par
\end{document}

Expected message: Detected 2 overfull lines. How to get the count? Surely it can be done somehow, since TeXworks (and presumably other GUI) can do it. But I use command line.

rallg
  • 2,379
  • ! Undefined control sequence. \AtOverfullHorixBadbox – David Carlisle Mar 13 '24 at 23:34
  • on the commandline it's trivial (if the tex document doesn't stop with an error before typesetting anything) just run$ grep -c 'Overfull \hbox' file.log` and it will return a count – David Carlisle Mar 13 '24 at 23:36
  • @DavidCarlisle I should have mentioned: That is the code I need! I know it does not exist. I willo edit the question to make that clear. – rallg Mar 13 '24 at 23:37
  • @DavidCarlisle Yes, on command line the grep is trivial (for me). But I am trying to make it in-your-face for less experienced users. – rallg Mar 13 '24 at 23:39

1 Answers1

3

I wouldn't do this from Lua, I'd simply run

grep -c 'Overfull \\hbox' file.log

after running lualatex,

but this Lua callback produces

Detected 2 overfull lines.

in the terminal and log

\documentclass{article}
%\usepackage{fontspec} % Uncomment if compiling with lualatex

% Start pseudo-code: \directlua{ ovcount = 0 local function foo (inc,d,h,f,l) if inc == "overfull" then ovcount = ovcount + 1 end texio.write("\string\n" ..inc .. " box on line " .. l .. "\string\n") end luatexbase.add_to_callback("hpack_quality",foo,"overfull hbox counter") } \AtEndDocument{\typeout{Detected \directlua{tex.print(ovcount)} overfull lines.}} % End pseudo-code. \begin{document} LaTeX puts me to sleep, zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz\par But expl3 makes me scream eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee\par \end{document}

Note that the above only makes an abbreviated message and only for overfull box messages not other types. Unfortunately as far as I can see luatex does not give access to the default message if this callback is used so a full version would have to re-create the message for all types using the information provided in the arguments. It would be useful if you could return a value to flag that you wanted the default message in addition to the custom callback behaviour but unless I missed something that is not provided.

David Carlisle
  • 757,742
  • Works great. As you noted, grep is the easy way, but I am testing methods that will provide messages to users who do not read instructions. The actual message, after some parsing that I know how to do, will go into a separate summary file. In the end, I may require that the command line call a shell script (which I know how to write) that pre-processed the document, processes it with lualatex, then post-processes the log file, all at once. Although I do not use Windows, it helps that MikTeX has dropped support for W<10,. so I can ask for the Linux subshell. – rallg Mar 14 '24 at 00:04
  • Ooops... Unexpected behavior. When I use the above code, it does keep count. But then the visible overfull rule vanishes!. I would have thought that add_to_callback did just that, adding something without removing something else. But apparently that is not what Lua does there. – rallg Mar 18 '24 at 03:15
  • @rallg yes.... I forgot to mention that. above. I just test for "overfull" and do nothing at all for any other kind of box message and for overfull only do abbreviated actions. what would be nice would be if luatex exposed a lua function that did the normal thing 9or as in some other callbacks) allwed a return of nil to trigger the default behavior but it seems not, I think you have to recreate all the message by hand which is a pain. You could make a feature request on luatex list.... I'll add a note to the answer – David Carlisle Mar 18 '24 at 09:31
  • @rallg note added. – David Carlisle Mar 18 '24 at 09:33
  • Thanks. The behavior is surprising, but not show-stopper. In final mode (no overfull boxes shown) the above message code is important. I can wrap it in a boolean so that the message is not activated in draft mode, which shows the overfull boxes. – rallg Mar 18 '24 at 12:39