2

I am trying to colorize equations (especially inline ones) so that they standout from the rest of the text and thus have used the \everymath{} and \everydisplay{} commands, however that also seems to affect tables. Text is not affected though. Any idea why this happening or how to get around it?

\documentclass{article}
\usepackage{xcolor}
\everymath{\color{teal}}
\everydisplay{\color{magenta}}

\begin{document}

Some text. Some text. Some text. Some text. $x=\int_{-\infty}^{\infty}\frac{\sin}{x}\mathrm{d}x=\frac{\pi}{2}$ Some text. Some text. Some text. Some text. [ x=\int_{-\infty}^{\infty}\frac{\sin}{x}\mathrm{d}x=\frac{\pi}{2} ]

\begin{table}[!h] % also colored? \centering \begin{tabular}{ll} \hline Animal & Legs \ \hline Dogs & 4 \ Human & 1 \ \hline \end{tabular} \end{table} \end{document}

enter image description here

  • 5
    You have discovered that tabular uses math mode. – egreg Aug 23 '21 at 21:12
  • 3
    math is used in lots of non-mathematical use, for tables it is used to get vertical centering which is the default alignment – David Carlisle Aug 23 '21 at 21:25
  • 1
    As suggested in https://tex.stackexchange.com/questions/100263/everymath-and-author-color, adding \makeatletter \def\m@th{\mathsurround\z@\color{black}} \makeatother should do the trick. – citsahcots Aug 23 '21 at 22:39

1 Answers1

5

enter image description here

\documentclass{article}
\usepackage{xcolor}
\everymath{\color{teal}}
\everydisplay{\color{magenta}}
%%%%%%%%%%%%%
\makeatletter
\def\m@th{\mathsurround\z@\color{black}}
\makeatother
%%%%%%%%%%%%%
\begin{document}

Some text. Some text. Some text. Some text. $x=\int_{-\infty}^{\infty}\frac{\sin}{x}\mathrm{d}x=\frac{\pi}{2}$ Some text. Some text. Some text. Some text. [ x=\int_{-\infty}^{\infty}\frac{\sin}{x}\mathrm{d}x=\frac{\pi}{2} ]

\begin{table}[!h] % also colored? \centering \begin{tabular}{ll} \hline Animal & Legs \ \hline Dogs & 4 \ Human & 1 \ \hline \end{tabular} \end{table} \end{document}

citsahcots
  • 7,992
  • Thanks! I followed the link and if I understand correctly, this workaround works under the assumption that environments like tabular or whatever that use math mode actually use the \m@th command? If this is not true is there any package that allows styling of all mathematics throughout the document in isolation safely? – First User Aug 24 '21 at 12:01
  • 1
    @FirstUser, sorry that I am not aware of such a package.. – citsahcots Aug 24 '21 at 12:19
  • This solution is risky. For example, it also changes the color of the \substack command. It may be safer to write a new command that does: \everymath{\color{black}} \begin{tabular} ... \end{tabular} \everymath{\color{teal}} – Mister Da Sep 08 '23 at 07:59