Coloring maths inside a tcolorbox












4















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]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
blindtext
$$F=ma$$
blindtext[3]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
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.



Thanks in advance !










share|improve this question


















  • 1





    Don't use $$...$$ - that's deprecated

    – ufo
    3 hours ago






  • 2





    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
    3 hours ago













  • @ufo the problem is not with xcolor per se but with color at pagebreaks.

    – Ulrike Fischer
    3 hours ago











  • 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.

    – HerculePoivrot
    3 hours ago
















4















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]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
blindtext
$$F=ma$$
blindtext[3]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
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.



Thanks in advance !










share|improve this question


















  • 1





    Don't use $$...$$ - that's deprecated

    – ufo
    3 hours ago






  • 2





    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
    3 hours ago













  • @ufo the problem is not with xcolor per se but with color at pagebreaks.

    – Ulrike Fischer
    3 hours ago











  • 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.

    – HerculePoivrot
    3 hours ago














4












4








4








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]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
blindtext
$$F=ma$$
blindtext[3]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
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.



Thanks in advance !










share|improve this question














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]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
blindtext
$$F=ma$$
blindtext[3]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
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.



Thanks in advance !







fontspec tcolorbox






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked 4 hours ago









HerculePoivrotHerculePoivrot

519




519








  • 1





    Don't use $$...$$ - that's deprecated

    – ufo
    3 hours ago






  • 2





    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
    3 hours ago













  • @ufo the problem is not with xcolor per se but with color at pagebreaks.

    – Ulrike Fischer
    3 hours ago











  • 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.

    – HerculePoivrot
    3 hours ago














  • 1





    Don't use $$...$$ - that's deprecated

    – ufo
    3 hours ago






  • 2





    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
    3 hours ago













  • @ufo the problem is not with xcolor per se but with color at pagebreaks.

    – Ulrike Fischer
    3 hours ago











  • 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.

    – HerculePoivrot
    3 hours ago








1




1





Don't use $$...$$ - that's deprecated

– ufo
3 hours ago





Don't use $$...$$ - that's deprecated

– ufo
3 hours ago




2




2





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
3 hours ago







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
3 hours ago















@ufo the problem is not with xcolor per se but with color at pagebreaks.

– Ulrike Fischer
3 hours ago





@ufo the problem is not with xcolor per se but with color at pagebreaks.

– Ulrike Fischer
3 hours ago













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.

– HerculePoivrot
3 hours ago





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.

– HerculePoivrot
3 hours ago










2 Answers
2






active

oldest

votes


















9














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]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
blindtextcolor{brown}
[F=ma]
blindtext[3]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
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}





share|improve this answer


























  • 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.

    – HerculePoivrot
    3 hours ago













  • 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.

    – Ulrike Fischer
    3 hours ago











  • 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 !

    – HerculePoivrot
    1 hour ago











  • @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/….

    – Marijn
    9 mins ago



















3














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 defmycolor{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]{defmycolor{#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=mtimes a]
blindtext[3]
boxcolor{blue}
blindtext[3]
(1+1=2)
boxcolor{red}
blindtext
boxcolor{brown}
blindtext
end{tcolorbox}

end{document}


Result:



enter image description here






share|improve this answer


























  • 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 !

    – HerculePoivrot
    1 hour ago











  • 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 ().

    – Marijn
    30 mins ago












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "85"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f485130%2fcoloring-maths-inside-a-tcolorbox%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









9














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]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
blindtextcolor{brown}
[F=ma]
blindtext[3]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
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}





share|improve this answer


























  • 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.

    – HerculePoivrot
    3 hours ago













  • 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.

    – Ulrike Fischer
    3 hours ago











  • 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 !

    – HerculePoivrot
    1 hour ago











  • @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/….

    – Marijn
    9 mins ago
















9














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]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
blindtextcolor{brown}
[F=ma]
blindtext[3]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
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}





share|improve this answer


























  • 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.

    – HerculePoivrot
    3 hours ago













  • 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.

    – Ulrike Fischer
    3 hours ago











  • 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 !

    – HerculePoivrot
    1 hour ago











  • @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/….

    – Marijn
    9 mins ago














9












9








9







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]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
blindtextcolor{brown}
[F=ma]
blindtext[3]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
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}





share|improve this answer















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]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
blindtextcolor{brown}
[F=ma]
blindtext[3]
addfontfeatures{Color=blue}
blindtext[3]
addfontfeatures{Color=red}
blindtext
addfontfeatures{Color=brown}
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}






share|improve this answer














share|improve this answer



share|improve this answer








edited 3 hours ago

























answered 3 hours ago









Ulrike FischerUlrike Fischer

199k9306692




199k9306692













  • 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.

    – HerculePoivrot
    3 hours ago













  • 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.

    – Ulrike Fischer
    3 hours ago











  • 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 !

    – HerculePoivrot
    1 hour ago











  • @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/….

    – Marijn
    9 mins ago



















  • 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.

    – HerculePoivrot
    3 hours ago













  • 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.

    – Ulrike Fischer
    3 hours ago











  • 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 !

    – HerculePoivrot
    1 hour ago











  • @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/….

    – Marijn
    9 mins ago

















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.

– HerculePoivrot
3 hours ago







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.

– HerculePoivrot
3 hours ago















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.

– Ulrike Fischer
3 hours ago





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.

– Ulrike Fischer
3 hours ago













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 !

– HerculePoivrot
1 hour ago





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 !

– HerculePoivrot
1 hour ago













@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/….

– Marijn
9 mins ago





@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/….

– Marijn
9 mins ago











3














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 defmycolor{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]{defmycolor{#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=mtimes a]
blindtext[3]
boxcolor{blue}
blindtext[3]
(1+1=2)
boxcolor{red}
blindtext
boxcolor{brown}
blindtext
end{tcolorbox}

end{document}


Result:



enter image description here






share|improve this answer


























  • 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 !

    – HerculePoivrot
    1 hour ago











  • 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 ().

    – Marijn
    30 mins ago
















3














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 defmycolor{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]{defmycolor{#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=mtimes a]
blindtext[3]
boxcolor{blue}
blindtext[3]
(1+1=2)
boxcolor{red}
blindtext
boxcolor{brown}
blindtext
end{tcolorbox}

end{document}


Result:



enter image description here






share|improve this answer


























  • 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 !

    – HerculePoivrot
    1 hour ago











  • 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 ().

    – Marijn
    30 mins ago














3












3








3







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 defmycolor{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]{defmycolor{#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=mtimes a]
blindtext[3]
boxcolor{blue}
blindtext[3]
(1+1=2)
boxcolor{red}
blindtext
boxcolor{brown}
blindtext
end{tcolorbox}

end{document}


Result:



enter image description here






share|improve this answer















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 defmycolor{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]{defmycolor{#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=mtimes a]
blindtext[3]
boxcolor{blue}
blindtext[3]
(1+1=2)
boxcolor{red}
blindtext
boxcolor{brown}
blindtext
end{tcolorbox}

end{document}


Result:



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 2 hours ago









MarijnMarijn

8,579639




8,579639













  • 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 !

    – HerculePoivrot
    1 hour ago











  • 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 ().

    – Marijn
    30 mins ago



















  • 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 !

    – HerculePoivrot
    1 hour ago











  • 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 ().

    – Marijn
    30 mins ago

















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 !

– HerculePoivrot
1 hour ago





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 !

– HerculePoivrot
1 hour ago













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 ().

– Marijn
30 mins ago





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 ().

– Marijn
30 mins ago


















draft saved

draft discarded




















































Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f485130%2fcoloring-maths-inside-a-tcolorbox%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Сан-Квентин

8-я гвардейская общевойсковая армия

Алькесар