# Coloring maths inside a tcolorbox

My TeX level isn't the greatest, I am trying hard to improve but I am always struggling.

I would like to change the color of text inside a tcolorbox and that this color formatting survives a pagebreak. This doesn't seem to be possible according to what I found here : Maintaining text colour change in a breakable tcolorbox but it seems to be possible under XeTeX using fontspec. I have one last problem before I finally manage to solve this problem I have add for more than a year and many hours of swearing, this doesn't seem to apply to text put in math mode.

Here is a MWE based on what the link above :

\documentclass{article}
\usepackage{mathspec}
\setmainfont{Palatino}
\usepackage{blindtext}
\usepackage[breakable]{tcolorbox}

\begin{document}

\begin{tcolorbox}[breakable]
\blindtext[3]
\blindtext[3]
\blindtext
\blindtext
$$F=ma$$
\blindtext[3]
\blindtext[3]
\blindtext
\blindtext
\end{tcolorbox}

\end{document}


Is it possible to get the $$F=ma$$ in brown ? Of course, any solution using the xcolor package won't work because of the incompatibility with tcolorbox, so I really need a font-associated solution. I had a look at the mathspec package but couldn't find the solution in the documentation. I somehow feel it shouldn't be that hard though.

• Don't use $$...$$ - that's deprecated
– ufo
Apr 16, 2019 at 12:00
• how can tcolorbox be incompatible with xcolor? It is automatically loaded when you sue tcolorbox. There is no way to use tcolorbox without xcolor
– ufo
Apr 16, 2019 at 12:03
• @ufo the problem is not with xcolor per se but with \color at pagebreaks. Apr 16, 2019 at 12:22
• The local formatting breaks if the page breaks. It happens to me quite often that the local formatting by using something like {extras={colupper=red}} is lost once the pages breaks. Apr 16, 2019 at 12:23

As it is rather improbable that there are page breaks in the math you could simply use \color:

\documentclass{article}
\usepackage{mathspec}
\setmainfont{TeX Gyre Pagella}
\usepackage{blindtext}
\usepackage[breakable]{tcolorbox}

\begin{document}

\begin{tcolorbox}[breakable]
\blindtext[3]
\blindtext[3]
\blindtext
\blindtext\color{brown}
$F=ma$
\blindtext[3]
\blindtext[3]
\blindtext
\blindtext
\end{tcolorbox}

\end{document}


With lualatex instead of xelatex you could use the luacolor package. This will solve both problems:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{TeX Gyre Pagella}
\setmathfont{TeX Gyre Pagella Math}
\usepackage{blindtext}
\usepackage[breakable]{tcolorbox}
\usepackage{luacolor}

\begin{document}

\begin{tcolorbox}[breakable]
\blindtext[3]
\color{blue}
\blindtext[3]
\color{red}
\blindtext
\color{brown}
\blindtext
$F=ma$
\blindtext[3]
\color{blue}
\blindtext[3]
\color{red}
\blindtext
\color{brown}
\blindtext
\end{tcolorbox}

\end{document}

• The pagebreaks is sometimes an issue, the main problem being that it's unpredictable. But the main issue is that I want conditional coloring of my text : a version for me and one for my students with some part of the text hidden, and therefore the maths must be the same colour than the main text. The LuaTeX solution works fine, but if possible I would like to avoid using it and stick to LaTeX or XeTex. Apr 16, 2019 at 12:21
• Sure I did understand this. But with the font method you will have to reset all math fonts before the math, \addfontfeatures won't be taken into account. With unicode-math you could do something like \setmathfont{TeX Gyre Pagella Math}[Color=blue], I don't know if mathspec has something similar. With lualatex and luacolor it is really easier. Apr 16, 2019 at 12:30
• Well I guess I will try with the LuaTex method which seems indeed very easy to use. Im always worried to change to LuaTeX because of the many documents I have running on regular LaTeX. Is there some kind of guide with the differences/advantages of those 3 computational modes (LaTeX, XeTeX and LuaTeX ?). Thanks for the answer anyway ! Apr 16, 2019 at 13:46
• @HerculePoivrot there are several questions on the site about this, many of those questions are a bit old but you could look at for example tex.stackexchange.com/questions/126206/…. Apr 16, 2019 at 15:31

With XeLaTeX, you could patch the math commands (both display and inline) to include the color command. This ensures that the color is set on the page where the math starts.

This does not work with the TeX commands  and  but only with the LaTeX equivalents  and . It also does not work if your math crosses a page break (but of course it will work if a new math expression starts on the next page). If you want to use it with other math-like environments (equation, align, etc) then you should patch those separately. Note that you should set \mycolor (or use \boxcolor) before you use math, otherwise you'll get an error. Also note that this takes effect outside of the tcolorbox as well, so you should probably put \def\mycolor{black} after each tcolorbox (or do that automatically with \AtEndEnvironment). With a small macro you can set \color and \addfontfeatures with a single command. MWE: \documentclass{article} \usepackage{mathspec} \setmainfont{Linux Libertine O} \usepackage{etoolbox} \pretocmd{$}{\color{\mycolor}}{}{} \pretocmd{$$}{\color{\mycolor}}{}{} \newcommand{\boxcolor}[1]{\def\mycolor{#1}\addfontfeatures{Color=\mycolor}} \usepackage{blindtext} \usepackage[breakable]{tcolorbox} \begin{document} \begin{tcolorbox}[breakable] \blindtext[3] \boxcolor{red} \blindtext[3] \boxcolor{orange} \blindtext[3] \[F=m\times a$ \blindtext[3] \boxcolor{blue} \blindtext[3] \(1+1=2$$ \boxcolor{red} \blindtext \boxcolor{brown} \blindtext \end{tcolorbox} \end{document}  Result: • Thank you for the answer. However, this will not work the way I want it to, because I have some regular math expressions (which should not colored) and a few documents of around 200 pages and I don't want to have to rework all of them with a special math mode, that would take way too much time. I will favor the LuaTeX version which will take less time I guess. But thanks for the detailled answer ! Apr 16, 2019 at 13:48 • I understand that this solution can be a bit impractical at times, I just added it to have a XeLaTeX approach that is a bit more robust and automatic than setting the color yourself on every page. Note however that it is still possible to set the color at any time, so you can put \color{black} inside an expression that should not be colored. Also, the special math mode is actually not so special but should be default,  as you used is actively discouraged (tex.stackexchange.com/questions/503/why-is-preferable-to) and  is still widely used but technically superseded by . Apr 16, 2019 at 15:10