0

When I use post_linebreak_filter to change the color of hlists, some equations are only partially colored! Here's the image, and complete code of the example. As you can see below, the horizontal division lines are still black...

enter image description here


\documentclass[notitlepage,letterpaper]{article}

%\usepackage{lua-visual-debug}
\usepackage[absolute]{textpos}
\usepackage[letterpaper,left=2in,right=2in,top=1in,bottom=0in]{geometry}
\usepackage[expansion=alltext,shrink=20,stretch=20]{microtype}
\usepackage{fontspec}
\usepackage{unicode-math}
\setmainfont{TeX Gyre Pagella}
\setmathfont{TeX Gyre Pagella Math}
\usepackage{blindtext}

\directlua{
    function my_post_lb_filter(head,groupcode)
      local HLIST = node.id("hlist")
      local WHAT = node.id("whatsit")
      local COL = node.subtype("pdf_colorstack")
      local color_push = node.new(WHAT,COL)
      local color_pop = node.new(WHAT,COL)
      color_push.stack = 0
      color_pop.stack = 0
      color_push.data = "1 0 0 rg"
      color_push.command = 1
      color_pop.command = 2
      for n in node.traverse(head) do % For every subnode within node head
        if n.id==HLIST then % If its a line of text
          n.head = node.insert_before(n.list, n.head, node.copy(color_push))
          n.head = node.insert_after(n.list, node.tail(n.head), node.copy(color_pop))
        end
      end
      return head
    end
  luatexbase.add_to_callback('post_linebreak_filter', my_post_lb_filter, 'Play with luatex node library')
}

\begin{document}

\blindtext[1]\vspace*{\baselineskip}

$\displaystyle\int_a^b x^2\;\mathrm{d}x= \tfrac{1}{3} x^3 \Big|_a^b$\vspace*{\baselineskip}

\blindtext[1]\vspace*{\baselineskip}

$a_0+\cfrac{1}{a_1+\cfrac{1}{a_2+\cfrac{1}{a_3+\cdots}}}$\vspace*{\baselineskip}


\end{document}

1 Answer 1

3

This is a typical issue where a minimal example can help you narrow down the problem:

\documentclass{article}
\begin{document}
\showoutput

\pdfextension colorstack 0 push {0.5 0.2 0.3 rg}

$\frac ab$

\end{document}

enter image description here

This shows that is is not related to the Lua code at all. This leads to the important question: What is 0.5 0.2 0.3 rg doing? It is a PDF literal, so we have to look it up in the PDF specs. PDF specification v1.7, Appendix A (Operator summary) says about rg:

rg setrgbcolor "Set RGB color for nonstroking operations"

Drawing a character is a nonstroking operation by default (the outline of a character is filled, not stroked), but are drawn by stroking operations (if they are not too thick). So how do we change the color for stroking operations?

RG setrgbcolor "Set RGB color for stroking operations"

To ensure consistent coloring, both stroking and non-stroking colors are normally set together in LaTeX:

\documentclass{article}
\begin{document}
\showoutput

\pdfextension colorstack 0 push {0.5 0.2 0.3 rg 0.5 0.2 0.3 RG}

$\frac ab$

\end{document}

enter image description here

3
  • Thanks for the explanation and fix! I had never heard of this. Usually I set colors with fontspec. I did some testing with fontspec's Color options like \defaultfontfeatures{Color=red} before set/mainmathfont (requires \usepackage{xcolor}), it doesn't set color for stroking lines either... And \addfontfeatures{color=blue} changes color of only text fonts, and not math fonts... Do you think its advisable to move to \pdfextension colorstack 0 push instead of using fontspec's addfontfeatures? It requires one to manually specify pop at the end of the group (unlike addfontfeatures)
    – reportaman
    Apr 24, 2020 at 10:45
  • @reportaman Even if fontspec would set the stroking color (it will soon), it still wouldn't affect fraction lines because it by design only affects the font. There is a system in place in LaTeX to automatically set colors correctly: The xcolor package. It allows you to just use e.g. \color{red} or \textcolor{red}{Text to be colored} and it automatically deals with inserting pdfextension colorstack in all the right places. It is also compatible with other engines, DVI output, etc. You should never need to manually use a colorstack command. Apr 24, 2020 at 11:17
  • In LuaTeX, you additionally want to load the package luacolor which modifies xcolor to use another mechanism than colorstack nodes to avoid some nasty side-effects. Apr 24, 2020 at 11:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.