I'm working on applying the answer to my question about color and ligatures and have run into a problem when the color change is supposed to happen inside a discretionary. Boiled down to a MWE and it looks like this:
\documentclass[12pt]{article} % use larger type; default would be 10pt
\usepackage{xcolor}
\usepackage{luacolor}
\definecolor{grebackgroundcolor}{RGB}{255,200,0}
\listfiles
\def\foobar#1#2{%
\hbox to 0pt{%
{\color{grebackgroundcolor}#2}%
}%
#1%
\relax %
}%
\begin{document}
\noindent\foobar{test}{\rule[-2pt]{36pt}{12pt}}
\noindent\discretionary{}{}{\foobar{test}{\rule[-2pt]{36pt}{12pt}}}
\end{document}
If you compile the MWE, you'll see that the first instance of \foobar works fine, but in the second, the rule ends up being black instead of orange. Remove the loading of luacolor and both instances work fine. How do I change color such that the color change will happen inside a \discretionary when luacolor loaded?

{\color{grebackgroundcolor}#2\hss}would probably be better inside\foobarto avoid an overfull\hbox(you don't even need the outer braces since\hboxdefines a group). – frougon May 28 '19 at 22:17\hss. It got stripped out during the reduction to MWE. – rpspringuel May 28 '19 at 22:26\textcolor) otherwise the colour restore will be outside the box. – David Carlisle May 31 '19 at 11:37#2is the last remaining thing in the\hbox, once we have removed the extra group? – frougon May 31 '19 at 11:42choicesenvironment (not an actual argument, because I want catcode-sensitive stuff to work there; color handling is in commands\ryanjform@startchoices@insidebox@hookand\ryanjform@startchoices@aftergroup@hook). It works with colored material even if I remove the extra\bgroupand\egroupI added because of your above comments. – frougon Jun 30 '19 at 09:52luacolor). Can turn this into a new question if this is worthy. Many thanks! MWE: `\documentclass{article} \usepackage{xcolor}\begin{document}
\color{red!50!black} This is a \hbox{\color{blue}foo} test.
\end{document}`.
– frougon Jun 30 '19 at 09:52\hboxis OK, the color restore comes outside the box (via aftergroup) but that is OK, change \hbox{..} to\sbox0\hbox{...}\box0and now you get the colour restore before the colour push and things go arbitrarily bad depending on where it is – David Carlisle Jun 30 '19 at 10:28\setboxinstead of\sbox. With\setbox, I have the example I was looking for. :-) That's weird stuff indeed, thanks for pointing it out! If I understand correctly, inside\box0, there is a “push blue” instruction (\special) but no corresponding pop (unless additional braces are put inside the box), and this push lasts after the box when\box0is used in the document. – frougon Jun 30 '19 at 10:41sboxso I ought to know the difference:-) – David Carlisle Jun 30 '19 at 11:00