[Tex/LaTex] How to change color of some particular environment for entire document

colorenvironmentsfontsmath-modeverbatim

I want to change color of particular environment in document.
For example wherever I found verbatim environment or equation environment appears in my tex file. With some global settings, I want to change it to some particular color effect should be limited to that document.

For example all the verbatim should change to yellow background. All of the math mode should have text color red.

Doing it for each of the text or modifying the environment is possible, how ever I am interested in setting it once in preamble.

Best Answer

There is no general solution. It depends on the enviroment. Some have dedicated hooks, other you will have to redefine. With verbatim you could e.g. change \verbatim@font. For math you could e.g. use everymath - but as you can see in the example it can have side effects as math is sometimes used in unexpected places like url.

\documentclass{article}
\usepackage{xcolor, url}
\makeatletter
\renewcommand\verbatim@font{\color{red}\normalfont\ttfamily}
\makeatletter

\everymath{\color{green}}
\begin{document}
blub

\begin{verbatim}
abx
\end{verbatim}

blub


$x=y$

bla

\(sum=c\)

\url{www.abc}
\end{document}