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. Butamsmath
typesets things in a, hmm, complicated way. In particular using\everydisplay
to add a\color
command seems difficult (it works whenamsmath
is not loaded). Anyway, here is a work-around:Output:
Update: adding to the code above in the preamble
will make the displayed
hello
also green. Note though that withamsmath
this works only because its environments have been patched here to annihilate\SetColor
. Withamsmath
,\everydisplay {\color{green}}
causes an error. So here inalign*
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.