31

I know that I can use \overfullrule=5pt to get overfull lines marked with a black marker. I am using pdfLaTeX. Is there a way I can get this marker or something similar in magenta instead?

lockstep
  • 250,273
jonalv
  • 11,466
  • The macro \overfullrule=5pt only determines the width of the "slug," i.e., of the marker that's placed to the right of the text in overfull lines. I couldn't find a detailed-enough explanation in either "TeX by Topic" or in "TeX for the inpatient" to determine how and were TeX creates the slug. I hope somebody else can help you out. – Mico Sep 13 '11 at 09:43
  • 11
    The slug is inserted down in TeX's bowels, if \overfullrule is positive and an \hbox is overfull. Usually this happens during paragraphing, but not necessarily. I believe there's no way to set the color. – egreg Sep 13 '11 at 09:51

4 Answers4

30

I am not sure if this is possible with old fashioned TeX/pdfTeX, but it is with LuaTeX (of course, it is!)

\documentclass{article}
\usepackage{luatexbase,luacode}
\overfullrule 5pt
\begin{luacode}

local VLIST = node.id("vlist")
local HLIST = node.id("hlist")
local GLUE = node.id("glue")
local RULE = node.id("rule")

magentabox = function(head)
  while head do
    if head.id == VLIST or head.id == HLIST then
      -- go through the hlists (the rows)
      magentabox(head.head)

    -- if there's a rule after the rightskip, this is the overfull box
    -- node id 10 == glue, glue subtype 9 is rightskip, node id 2 is a rule

    elseif head.id == GLUE and head.subtype == 9 and head.next and head.next.id == RULE then
       -- this must be an overfull box
       local w1, w2
       w1 = node.new("whatsit","pdf_literal")
       w1.data = "q 1 0 1 rg"
       w1.mode = 1
       w2 = node.new("whatsit","pdf_literal")
       w2.data = " Q"
       w2.mode = 1

       w1.next = head.next -- the rule
       head.next = w1      -- color start
       w1.next.next = w2   -- color end

       node.slide(head)    -- adjust prev pointers
    end
    head = head.next
  end
  return true
end
luatexbase.add_to_callback("post_linebreak_filter",magentabox,"magentabox")

\end{luacode}

\begin{document}
\hsize 1.7in A verylongword verylongword verylongword
\end{document}

magenta overfull box

topskip
  • 37,020
  • 3
    Very nice. Yet a reason in the pile for switching to LuaTeX. I am afraid the pile is not big enough to warrant converting a 500 pages book project though. After all this is just a draft mode feature. I really would need it in pdfTeX for this project... – jonalv Sep 13 '11 at 11:40
  • 4
    @jonalv: LuaTeX is based on PDFTeX and largely compatible with pretty much everything PDFTeX. The question might be what is keeping you from switching, still? Have you tried processing your 500 pages book through lualatex, just to check? – raphink Sep 15 '11 at 09:49
  • @Patrick: this solution does color overfull boxes, but it also ruins the next lines. It seems to generate a negative increasing indentation for every line. – raphink Sep 15 '11 at 09:56
  • 1
    @Raphink: I have edited my solution (mode=1 for w1 and w2). This will change the positioning of the PDF instructions (pdf_literal). I still don't have a good feeling when other modes then 0 are necessary. – topskip Sep 15 '11 at 10:27
  • That works @Patrick, thanks. Going to make an overcolored package (unless someone finds a nicer name) :-) – raphink Sep 15 '11 at 12:18
  • @Raphink: Touché I guess. Honestly I have only tried XeLaTeX except for PDFTeX so far but I did have some trouble with that and I was under the impression that lualatex was newer and hence an even less mature project thus giving me (and all the not so computer savvy authors (some of which I am very happy are writing tex at all in the first place) potential headache with all the various packages currently being employed in that patch-work... So, I am not sure I dare, but no I haven't tried. :) I probably will dare to one day. – jonalv Sep 15 '11 at 16:02
  • @jonalv: While XeTeX was written from scratch (imo), LuaTeX is based on PDFTeX, and matured quite fast. It's currently a bêta (version 0.70 in TeXLive 2011) but is already stable enough to be used. That said, Aditya provided a more portable solution (which I have since packaged). – raphink Sep 15 '11 at 16:09
  • @cjorssen thanks for the »bug report«, I'll fix it. – topskip Sep 27 '16 at 09:40
  • @cjorssen I have edited the code, but it worked for me with TeXlive 2016 – topskip Sep 27 '16 at 09:54
25

Copied from a TeX pearl by Paweł Jackowski. In his words, never underestimate TeX's bells and whistles.

\def\ooops{\hbox to\wd0{\setbox0=\hbox to\wd0{\unhbox0}%
    \unhbox0 \ifnum\badness>10000 \rlap{\tiny\quad Ooops!}\fi}}

\interlinepenalty=-50000 % force the break between each two lines
\maxdeadcycles=50        % allow upto 50 \outputs with no \shipout
\newtoks\orioutput \orioutput=\output % wrap the original \output routine
\output
    {\ifnum\outputpenalty>-20000 \the\orioutput
     \else \ifnum\outputpenalty<-\maxdimen \the\orioutput
     \else
     \unvbox255        % flush the entire list back
     \setbox0=\lastbox % strip the very last box
     \nointerlineskip  % avoid doubled interline glue
     \ooops            % make the test and return the box back.
     \advance\outputpenalty by50000
     \penalty\outputpenalty % weak lie that nothing happened...
     \fi\fi}

\documentclass{article}
\usepackage[textwidth=1.5in,a6paper]{geometry}

\begin{document}
\pagestyle{empty}

This completely useless example shows a not-so-useless trick, which might be
used for quite advanced applications, such as line-numbering, some kind of
paragraph decoration, page optimization and probably many others. Things become
much more complicated if math displays, \verb|\marks|, \verb|\inserts| or
\verb|\vadjusts| come into play, but they don-F¢t spoil all of the game.
\end{document}

which gives

enter image description here

The definition of ooops can be changed to give a colored rule instead of the text. For example (thanks to Jake)

\def\ooops{\hbox to\wd0{\setbox0=\hbox to\wd0{\unhbox0}%
    \unhbox0 \ifnum\badness>10000 \rlap{\color{magenta}{\rule{0.5em}{3ex}}}\fi}}

and loading xcolor.

Aditya
  • 62,301
19

Aditya gave a very nice, portable solution (Patrick's is very nice, too, but less portable).

I've made a little package called overcolored out of it, which can currently be found on github:

\documentclass[pagesize=pdftex,paper=10cm:10cm]{scrbook}

\usepackage{lipsum}
\usepackage[color=blue,
            width=3pt,
            height=0.5\baselineskip]{overcolored}

\begin{document}

\lipsum[1-3]

\end{document}

enter image description here

Edit: I've seen quite a few shortcomings in complex environments using Aditya's code, while Patrick's code has no side effects that I could see. For this reason, the package now supports both implementations. When compiled with LuaTeX, it will use Patrick's code, otherwise, Aditya's code is used.

I still have to adapt the options to work with Patrick's code. Supporting the width parameter was quite straightforward, the height is a bit more tricky, and supporting color names with PDF annotations seems much more tricky still...

raphink
  • 31,894
  • I tried this package on the book but keep getting the following error on figures: `! Incompatible list can't be unboxed. } l.788 \end{figure}` I will try to come up with a minimal example and file as an issue on Github. – jonalv Sep 16 '11 at 13:53
  • @jonalv: using PDFTeX I guess? You didn't try the LuaTeX implementation? – raphink Sep 16 '11 at 13:54
  • Okey further studies has made me see "quite a few shortcomings in complex environments" as well. I suppose that one can't have everything... – jonalv Sep 16 '11 at 14:29
  • yea, actually I am gonna start looking for a LuaTex for dummies or something real-soon-now™ :) – jonalv Sep 16 '11 at 14:31
  • 2
    @jonalv: On the other hand, the LuaTeX implementation works great for me. All it requires is to use another rendering engine with the same package. – raphink Sep 16 '11 at 14:31
7

Much has happened since topskip gave a LuaTeX solution. Relevant to us is the new callback hpack_quality. Just turn off the \overfullrule and replace topskip's luatex block with the following, adapting b.width and the colour in a.data to taste. Also, if you prefer overfullrules of fixed height and width, just assign those to b.height and b.depth:

\begin{luacode}
  ofbox = function(incident, detail, head, first, last)
    if incident == "overfull" then
      local a, b, c
      b = node.new("rule",0)
      b.width = tex.sp("1mm")
      a = node.new("whatsit","pdf_literal")
      a.data = "q 1 0 1 rg"
      a.mode = 1
      c = node.new("whatsit","pdf_literal")
      c.data = " Q"
      c.mode = 1
      a.next = b
      b.next = c
      node.slide(a)
      return a
    end
  end
  luatexbase.add_to_callback("hpack_quality",ofbox,"ofbox")
\end{luacode}

This has the added benefit of working with overfull display math as well.

  • This is my very first attempt at writing lua code. Many thanks to @topskip for giving me the idea, and some code to look at! – Harald Hanche-Olsen Aug 03 '17 at 13:53
  • Added bonus: The code is easily adapted to make underfullrules as well! (With a different colour, of course.) – Harald Hanche-Olsen Aug 03 '17 at 14:31
  • Nice use case for the hpack_quality callback! – topskip Nov 27 '17 at 11:51
  • @HaraldHanche-Olsen that's amazing, thanks so much! However, its seems to remove the warning about overfull boxes from log (at least the one I see in texstudio). Is that intended? Can that be restored? – bonanza May 18 '18 at 14:39
  • @bonanza You'll have to write your own warning. I have been using this code: texio.write_nl("### overfull: " .. string.format("%.2f",detail/186467) .. "mm at line " .. first .. "-" .. last) followed by another texio.write_nl("") inside the if block. The detail variable measures the overfullness in scaled points sp. I chose to convert it to millimeters; you may prefer points. – Harald Hanche-Olsen May 22 '18 at 13:37