[Tex/LaTex] How to color certain commands/environments

colorenvironments

My goal is to achieve colorings in the output document, that are similar to the syntax highlighting I use in my editor, so switching between the editing and viewing the document becomes easier on the eyes.

I figured I can use the color package to define some colors using \definecolor and then set the foreground and backgroundcolors using \color{fg} and \pagecolor{bg}, respectively.

What I'd like to do now, is assing certain colors to certain commands and environments. For simple commands like \emph I can do

\let\oldemph\emph
\renewcommand\emph[1]{{\color{emph}\oldemph{#1}}}

but I haven't figured out how to change the color of environments like align* and others.

Any help on how I could achieve this? Maybe theres even a generic way to do this, a package for semantic coloring like this would be cool.

EDIT: I managed to color environments using

\usepackage{etoolbox}
\AtBeginEnvironment{align*}{\color{equation}}
\AtBeginEnvironment{align}{\color{equation}}
\AtBeginEnvironment{equation*}{\color{equation}}
\AtBeginEnvironment{equation}{\color{equation}}

Inline-math can be colored using \everymath{\color{inlinemath}}, but this overrides the colors of the align* environment (not of the equation* environment though). How to fix this?

EDIT: If I'm correct, align*-environments switch between mathmode and non-mathmode to obtain the correct spacing. So if I use \everymath to set a color, it will be set inside align* environments, overriding what I did with \AtBeginEnvironment. For equation* it works, because there is only a single math block and \AtBeginEnvironment will put the color setting inside. The only way I can think of to fix this, would be somehow setting a color only for $...$, instead of using \everymath. Is this possible?

Best Answer

Update below: one way to deal with \intertext in this method.

Normally \everymath only regards inline, and there is \everydisplay for display math. But amsmath typesets things in a, hmm, complicated way. In particular using \everydisplay to add a \color command seems difficult (it works when amsmath is not loaded). Anyway, here is a work-around:

\documentclass{article}
\usepackage{amsmath}
\usepackage{color}
\usepackage{etoolbox}

\makeatletter
\AtBeginEnvironment{align*}{\let\SetColor\@gobble \color{display}}
\AtBeginEnvironment{align}{\let\SetColor\@gobble \color{display}}
\AtBeginEnvironment{equation*}{\let\SetColor\@gobble \color{display}}
\AtBeginEnvironment{equation}{\let\SetColor\@gobble \color{display}}
\makeatother

% maybe some other nice package has set-up things in \everymath!

\everymath\expandafter{\the\everymath\SetColor{inline}}

\definecolor{display}{rgb}{0,1,0}
\definecolor{inline}{rgb}{0,0,1}

\let\SetColor\color

\begin{document}

$xyz$

\begin{align*}
  E &= mc^2\\
  x^n+y^n&= z^n
\end{align*}

$$hello $$ %% for this we would need \everydisplay
%% but some interference with amsmath makes its use
%% a bit delicate.

\begin{equation}
  E = mc^2
\end{equation}

\end{document}

Output:

colored math

Update: adding to the code above in the preamble

\everydisplay\expandafter{\the\everydisplay\SetColor{display}}

will make the displayed hello also green. Note though that with amsmath this works only because its environments have been patched here to annihilate \SetColor. With amsmath, \everydisplay {\color{green}} causes an error. So here in align* the \SetColor does not do anything and it is the \color inserted at the begin of the environment which sets the color.


To deal with \intertext. One possibility.

\documentclass{article}
\usepackage{color}
\usepackage{amsmath}
\usepackage{etoolbox}

\makeatletter
\AtBeginEnvironment{align*}{\let\SetColor\@gobble \color{display}%
                            \Patchintertext}
\AtBeginEnvironment{align}{\let\SetColor\@gobble \color{display}\Patchintertext}
\AtBeginEnvironment{equation*}{\let\SetColor\@gobble \color{display}}
\AtBeginEnvironment{equation}{\let\SetColor\@gobble \color{display}}

\def\Patchintertext{\let\oldintertext@\intertext@
                    \def\intertext@{\oldintertext@\Patchintertext@}}
\def\Patchintertext@ {\let\oldintertext\intertext
                      \def\intertext ##1{\oldintertext
                      {\color{black}\let\SetColor\color ##1}}}
\makeatother


\everymath\expandafter{\the\everymath\SetColor{inline}}
\everydisplay\expandafter{\the\everydisplay\SetColor{display}}

\definecolor{display}{rgb}{.6,.6,.2}
\definecolor{inline}{rgb}{0,0,1}

\let\SetColor\color

\begin{document}

here is some text before with $xyz$ math inline,
\begin{align*}
  E &= mc^2\\
\intertext {this is some intertext with math $a^n+b^n=c^n$ inside,}
  x^n+y^n&= z^n\quad \text{(display)}
\end{align*}
and here is some more text after with again $A^B$ math inline.

$$this is some standard math display$$ 

\begin{equation}
  E = mc^2
\end{equation}

\end{document}

color certain B