Coloring maths inside a tcolorboxWhy is [ … ] preferable to $$ … $$?Why choose LuaLaTeX over XeLaTeX?Maintaining text colour change in a breakable tcolorboxAdding an Image inside tcolorboxUse algorithm2e inside tcolorboxmath inside title in tcolorboxItemize inside TcolorboxClip background image inside tcolorboxsetting text size inside tcolorboxMulticolumns inside tcolorbox breakable boxParagraph breaks inside tcolorboxtodo notes inside tcolorbox

What is expected in a 5 minute postdoc interview presentation?

Tuning fork in last two episodes of Legion S02

Found a minor bug, affecting 1% of users. What should QA do?

How is погода (weather) a count noun?

Unable to delete GeoTIFF after adding it to GeoServer

Is it possible for both sides of an encounter to be surprised?

What would the EU’s position be with respect to a United Ireland?

Could Boris Johnson face criminal charges for illegally proroguing Parliament?

What is the difference between increasing volume and increasing gain?

Booting Ubuntu from USB drive on MSI motherboard -- EVERYTHING fails

Lighthouse Alternatives

As a girl, how can I voice male characters effectively?

C Clef Showing Middle C on the Second Space: Printing Error, or Obscure Clef?

Anonymous reviewer disclosed his identity. Should I thank him by name?

Can/should you swim in zero G?

How is this situation not a checkmate?

Tikz background color of node multilayer

Job interview by video at home and privacy concerns

Present participles of the verb esse

Why is there such a singular place for bird watching?

Realistically, how much do you need to start investing?

Bothered by watching coworkers slacking off

How Concerning Is This X-CU-modified: FAKECU Text Attack?

Sci-fi story about aliens with cells based on arsenic or nitrogen, poisoned by oxygen



Coloring maths inside a tcolorbox


Why is [ … ] preferable to $$ … $$?Why choose LuaLaTeX over XeLaTeX?Maintaining text colour change in a breakable tcolorboxAdding an Image inside tcolorboxUse algorithm2e inside tcolorboxmath inside title in tcolorboxItemize inside TcolorboxClip background image inside tcolorboxsetting text size inside tcolorboxMulticolumns inside tcolorbox breakable boxParagraph breaks inside tcolorboxtodo notes inside tcolorbox






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









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 :



documentclassarticle
usepackagemathspec
setmainfontPalatino
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
$$F=ma$$
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
endtcolorbox

enddocument


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
    Apr 16 at 12:00






  • 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
    Apr 16 at 12:03












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

    – Ulrike Fischer
    Apr 16 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.

    – HerculePoivrot
    Apr 16 at 12:23

















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 :



documentclassarticle
usepackagemathspec
setmainfontPalatino
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
$$F=ma$$
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
endtcolorbox

enddocument


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
    Apr 16 at 12:00






  • 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
    Apr 16 at 12:03












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

    – Ulrike Fischer
    Apr 16 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.

    – HerculePoivrot
    Apr 16 at 12:23













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 :



documentclassarticle
usepackagemathspec
setmainfontPalatino
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
$$F=ma$$
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
endtcolorbox

enddocument


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 :



documentclassarticle
usepackagemathspec
setmainfontPalatino
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
$$F=ma$$
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
endtcolorbox

enddocument


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 Apr 16 at 11:28









HerculePoivrotHerculePoivrot

5110 bronze badges




5110 bronze badges










  • 1





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

    – ufo
    Apr 16 at 12:00






  • 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
    Apr 16 at 12:03












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

    – Ulrike Fischer
    Apr 16 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.

    – HerculePoivrot
    Apr 16 at 12:23












  • 1





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

    – ufo
    Apr 16 at 12:00






  • 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
    Apr 16 at 12:03












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

    – Ulrike Fischer
    Apr 16 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.

    – HerculePoivrot
    Apr 16 at 12:23







1




1





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

– ufo
Apr 16 at 12:00





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

– ufo
Apr 16 at 12:00




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
Apr 16 at 12:03






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 at 12:03














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

– Ulrike Fischer
Apr 16 at 12:22





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

– Ulrike Fischer
Apr 16 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.

– HerculePoivrot
Apr 16 at 12:23





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
Apr 16 at 12:23










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:



documentclassarticle
usepackagemathspec
setmainfontTeX Gyre Pagella
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtextcolorbrown
[F=ma]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
endtcolorbox

enddocument


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



documentclassarticle
usepackageunicode-math
setmainfontTeX Gyre Pagella
setmathfontTeX Gyre Pagella Math
usepackageblindtext
usepackage[breakable]tcolorbox
usepackageluacolor

begindocument

begintcolorbox[breakable]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
[F=ma]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
endtcolorbox

enddocument





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
    Apr 16 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 setmathfontTeX Gyre Pagella Math[Color=blue], I don't know if mathspec has something similar. With lualatex and luacolor it is really easier.

    – Ulrike Fischer
    Apr 16 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 !

    – HerculePoivrot
    Apr 16 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/….

    – Marijn
    Apr 16 at 15:31


















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 defmycolorblack after each tcolorbox (or do that automatically with AtEndEnvironment).



With a small macro you can set color and addfontfeatures with a single command.



MWE:



documentclassarticle
usepackagemathspec
setmainfontLinux Libertine O
usepackageetoolbox
pretocmd[colormycolor
pretocmd(colormycolor
newcommandboxcolor[1]defmycolor#1addfontfeaturesColor=mycolor
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
boxcolorred
blindtext[3]
boxcolororange
blindtext[3]
[F=mtimes a]
blindtext[3]
boxcolorblue
blindtext[3]
(1+1=2)
boxcolorred
blindtext
boxcolorbrown
blindtext
endtcolorbox

enddocument


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
    Apr 16 at 13:48






  • 1





    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 colorblack 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
    Apr 16 at 15:10












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/4.0/"u003ecc by-sa 4.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:



documentclassarticle
usepackagemathspec
setmainfontTeX Gyre Pagella
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtextcolorbrown
[F=ma]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
endtcolorbox

enddocument


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



documentclassarticle
usepackageunicode-math
setmainfontTeX Gyre Pagella
setmathfontTeX Gyre Pagella Math
usepackageblindtext
usepackage[breakable]tcolorbox
usepackageluacolor

begindocument

begintcolorbox[breakable]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
[F=ma]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
endtcolorbox

enddocument





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
    Apr 16 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 setmathfontTeX Gyre Pagella Math[Color=blue], I don't know if mathspec has something similar. With lualatex and luacolor it is really easier.

    – Ulrike Fischer
    Apr 16 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 !

    – HerculePoivrot
    Apr 16 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/….

    – Marijn
    Apr 16 at 15:31















9
















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



documentclassarticle
usepackagemathspec
setmainfontTeX Gyre Pagella
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtextcolorbrown
[F=ma]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
endtcolorbox

enddocument


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



documentclassarticle
usepackageunicode-math
setmainfontTeX Gyre Pagella
setmathfontTeX Gyre Pagella Math
usepackageblindtext
usepackage[breakable]tcolorbox
usepackageluacolor

begindocument

begintcolorbox[breakable]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
[F=ma]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
endtcolorbox

enddocument





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
    Apr 16 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 setmathfontTeX Gyre Pagella Math[Color=blue], I don't know if mathspec has something similar. With lualatex and luacolor it is really easier.

    – Ulrike Fischer
    Apr 16 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 !

    – HerculePoivrot
    Apr 16 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/….

    – Marijn
    Apr 16 at 15:31













9














9










9









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



documentclassarticle
usepackagemathspec
setmainfontTeX Gyre Pagella
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtextcolorbrown
[F=ma]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
endtcolorbox

enddocument


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



documentclassarticle
usepackageunicode-math
setmainfontTeX Gyre Pagella
setmathfontTeX Gyre Pagella Math
usepackageblindtext
usepackage[breakable]tcolorbox
usepackageluacolor

begindocument

begintcolorbox[breakable]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
[F=ma]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
endtcolorbox

enddocument





share|improve this answer















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



documentclassarticle
usepackagemathspec
setmainfontTeX Gyre Pagella
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtextcolorbrown
[F=ma]
blindtext[3]
addfontfeaturesColor=blue
blindtext[3]
addfontfeaturesColor=red
blindtext
addfontfeaturesColor=brown
blindtext
endtcolorbox

enddocument


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



documentclassarticle
usepackageunicode-math
setmainfontTeX Gyre Pagella
setmathfontTeX Gyre Pagella Math
usepackageblindtext
usepackage[breakable]tcolorbox
usepackageluacolor

begindocument

begintcolorbox[breakable]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
[F=ma]
blindtext[3]
colorblue
blindtext[3]
colorred
blindtext
colorbrown
blindtext
endtcolorbox

enddocument






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 16 at 12:14

























answered Apr 16 at 12:07









Ulrike FischerUlrike Fischer

217k9 gold badges325 silver badges726 bronze badges




217k9 gold badges325 silver badges726 bronze badges















  • 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
    Apr 16 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 setmathfontTeX Gyre Pagella Math[Color=blue], I don't know if mathspec has something similar. With lualatex and luacolor it is really easier.

    – Ulrike Fischer
    Apr 16 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 !

    – HerculePoivrot
    Apr 16 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/….

    – Marijn
    Apr 16 at 15:31

















  • 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
    Apr 16 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 setmathfontTeX Gyre Pagella Math[Color=blue], I don't know if mathspec has something similar. With lualatex and luacolor it is really easier.

    – Ulrike Fischer
    Apr 16 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 !

    – HerculePoivrot
    Apr 16 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/….

    – Marijn
    Apr 16 at 15:31
















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
Apr 16 at 12:21






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
Apr 16 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 setmathfontTeX Gyre Pagella Math[Color=blue], I don't know if mathspec has something similar. With lualatex and luacolor it is really easier.

– Ulrike Fischer
Apr 16 at 12:30





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 setmathfontTeX Gyre Pagella Math[Color=blue], I don't know if mathspec has something similar. With lualatex and luacolor it is really easier.

– Ulrike Fischer
Apr 16 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 !

– HerculePoivrot
Apr 16 at 13:46





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
Apr 16 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/….

– Marijn
Apr 16 at 15:31





@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
Apr 16 at 15:31













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 defmycolorblack after each tcolorbox (or do that automatically with AtEndEnvironment).



With a small macro you can set color and addfontfeatures with a single command.



MWE:



documentclassarticle
usepackagemathspec
setmainfontLinux Libertine O
usepackageetoolbox
pretocmd[colormycolor
pretocmd(colormycolor
newcommandboxcolor[1]defmycolor#1addfontfeaturesColor=mycolor
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
boxcolorred
blindtext[3]
boxcolororange
blindtext[3]
[F=mtimes a]
blindtext[3]
boxcolorblue
blindtext[3]
(1+1=2)
boxcolorred
blindtext
boxcolorbrown
blindtext
endtcolorbox

enddocument


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
    Apr 16 at 13:48






  • 1





    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 colorblack 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
    Apr 16 at 15:10















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 defmycolorblack after each tcolorbox (or do that automatically with AtEndEnvironment).



With a small macro you can set color and addfontfeatures with a single command.



MWE:



documentclassarticle
usepackagemathspec
setmainfontLinux Libertine O
usepackageetoolbox
pretocmd[colormycolor
pretocmd(colormycolor
newcommandboxcolor[1]defmycolor#1addfontfeaturesColor=mycolor
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
boxcolorred
blindtext[3]
boxcolororange
blindtext[3]
[F=mtimes a]
blindtext[3]
boxcolorblue
blindtext[3]
(1+1=2)
boxcolorred
blindtext
boxcolorbrown
blindtext
endtcolorbox

enddocument


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
    Apr 16 at 13:48






  • 1





    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 colorblack 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
    Apr 16 at 15:10













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 defmycolorblack after each tcolorbox (or do that automatically with AtEndEnvironment).



With a small macro you can set color and addfontfeatures with a single command.



MWE:



documentclassarticle
usepackagemathspec
setmainfontLinux Libertine O
usepackageetoolbox
pretocmd[colormycolor
pretocmd(colormycolor
newcommandboxcolor[1]defmycolor#1addfontfeaturesColor=mycolor
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
boxcolorred
blindtext[3]
boxcolororange
blindtext[3]
[F=mtimes a]
blindtext[3]
boxcolorblue
blindtext[3]
(1+1=2)
boxcolorred
blindtext
boxcolorbrown
blindtext
endtcolorbox

enddocument


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 defmycolorblack after each tcolorbox (or do that automatically with AtEndEnvironment).



With a small macro you can set color and addfontfeatures with a single command.



MWE:



documentclassarticle
usepackagemathspec
setmainfontLinux Libertine O
usepackageetoolbox
pretocmd[colormycolor
pretocmd(colormycolor
newcommandboxcolor[1]defmycolor#1addfontfeaturesColor=mycolor
usepackageblindtext
usepackage[breakable]tcolorbox

begindocument

begintcolorbox[breakable]
blindtext[3]
boxcolorred
blindtext[3]
boxcolororange
blindtext[3]
[F=mtimes a]
blindtext[3]
boxcolorblue
blindtext[3]
(1+1=2)
boxcolorred
blindtext
boxcolorbrown
blindtext
endtcolorbox

enddocument


Result:



enter image description here







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 16 at 13:30

























answered Apr 16 at 13:24









MarijnMarijn

13k1 gold badge7 silver badges42 bronze badges




13k1 gold badge7 silver badges42 bronze badges















  • 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
    Apr 16 at 13:48






  • 1





    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 colorblack 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
    Apr 16 at 15:10

















  • 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
    Apr 16 at 13:48






  • 1





    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 colorblack 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
    Apr 16 at 15:10
















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
Apr 16 at 13:48





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
Apr 16 at 13:48




1




1





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 colorblack 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
Apr 16 at 15:10





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 colorblack 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
Apr 16 at 15:10


















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

Distance measures on a map of a game The 2019 Stack Overflow Developer Survey Results Are Inmin distance in a graphShortest distance path on contour plotHow to plot a tilted map?Finding points outside of a diskDelaunay link distanceAnnulus from GeoDisks: drawing a ring on a mapNegative Correlation DistanceFind distance along a path (GPS coordinates)Finding position at given distance in a GeoPathMathematics behind distance estimation using camera

How to get a smooth, uniform ParametricPlot of a 2D Region?How to plot a complicated Region?How to exclude a region from ParametricPlotHow discretize a region placing vertices on a specific non-uniform gridHow to transform a Plot or a ParametricPlot into a RegionHow can I get a smooth plot of a bounded region?Smooth ParametricPlot3D with RegionFunction?Smooth border of a region ParametricPlotSmooth region boundarySmooth region plot from list of pointsGet minimum y of a certain x in a region

Genealogie vun de Merowenger Vum Merowech bis zum Chilperich I. | Navigatiounsmenü