15

I'd like to change the color of all math in my document. I understand that several packages and some engines provide support for changing the color of all math, and I understand that something like

\everymath{\color{green}}

almost works, but also ends up coloring things like URLs and captions, while failing to color displayed equations.

Is there a reliable way to change the color of just math, in pdflatex, without relying on special packages or classes?

orome
  • 10,459
  • easy-peasy with xelatex or lualatex ... – Andrew Stacey Mar 15 '12 at 22:32
  • 1
    This needs a Minimal non-Working Example that shows the mal-colored captions, URLs and display math. Btw, it's strongly discouraged to use $$...$$, better switch to \[...\]. – yo' Mar 15 '12 at 22:39
  • @tohecz : What is the difference between the two? I am an ignorant who just doesn't know and I wonder. I must say I use the cash's all the time so I guess it's good I ask – Patrick Da Silva Mar 15 '12 at 22:59
  • @tohecz: Sorry, I was being sloppy about $$. That's a shorthand for what I mean. Fixed above. – orome Mar 15 '12 at 23:24
  • 2
    The $ ... $ for inline and $$ ... $$ for displayed come from TeX and are not recommended for LaTeX. They do work 'most' of the time. However the do interfere with several packages (and worse the error is ofter very hard to spot. e.g. they conflict with fleqn from amsmath. I teach my students to use ( ..) and [ ...] respectively, and I get fewer LaTeX questions the night before something is due. – R. Schumacher Mar 16 '12 at 00:15
  • 2
    For more details see http://tex.stackexchange.com/questions/503/why-is-preferable-to – R. Schumacher Mar 16 '12 at 00:22
  • @R.Schumacher: I’m being picky here, but “[$$] come from TeX… they do interfere with… they conflict with…” is disturbingly backwards; as LaTeX is a macro package built on TeX, whatever's done with it in an “interfering” or “incompatible” way to TeX's, is just that. Not vice versa. – morbusg Mar 16 '12 at 06:58

1 Answers1

16
\everymath{\color{green}}
\everydisplay{\color{green}}
\def\m@th{\normalcolor\mathsurround\z@}

will get you most of the way there, although you are still likely to need some special fixup for individual commands here and there that use math internally without using the \m@th command which I redefine above to reset the color. Most "internal" uses of math for vertical centering tables or superscripting footnote marks etc, use that macro but that convention (from Plain TeX originally) is not enforced anywhere, and also redefining it might not be safe in all contexts (\edef for example) so some uses may need more explicit redefinitions to reset the colour.

David Carlisle
  • 757,742
  • What if I just want the references to my equations to be written in green without automatically changing the color of my glossary or my figure references? – Wallflower Apr 16 '23 at 11:49
  • @Wallflower this post is unrelated to references. post a new question if you have a question – David Carlisle Apr 16 '23 at 11:51
  • I did yesterday https://tex.stackexchange.com/questions/683022/personalize-the-color-of-the-references-glossary-citation-figure-and-equation but had no return... – Wallflower Apr 16 '23 at 19:38