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.

  • 1
    Welcome to TeX.sx! I removed the beamer references in your question because I'm pretty sure what you want to achieve can be done in beamer just like in any other document. Hence, the question could be made more widely applicable, which is desirable on tex.sx.
    – doncherry
    Feb 5, 2012 at 12:08

2 Answers 2


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.

\usepackage{xcolor, url}








For the verbatim material I want to show two other solutions:

The first one is the package fancyvrb, where you can set an option for active characters (commandchars) and in this way let execute macros inside of the Verbatim environment, and you can define custom verbatim environments with \DefineVerbatimEnvironment.

The second does not only work in verbatim environments, but is a general one: The package etoolbox provides hooks for environments, see below in the example for \BeforeBeginEnvironment and \AfterEndEnvironment. I used fancyvrb’s Verbatim environment for that:


\usepackage[english]{babel} % needed for "blindtext"






Verbatim text with by hook predefined colour:
This blindtext is no pangram.

Verbatim text with individual colour settings:

Custom verbatim environment with predefined colour:

... and with individual colour settings:



Output of the example

  • Thanks! The above code is giving appropraite output. How ever, [pangram] option with blindtext package did not work. Error is: ! LaTeX Error: Unknown option pangram' for package blindtext'.
    – manjusha
    Feb 6, 2012 at 18:04
  • @manjusha The pangram option has been introduced recently with version 2.0 (Jan 6 2012) of blindtext. You should update your TeX distribution.
    – Speravir
    Feb 6, 2012 at 20:48
  • Thanks for pointing out! I will update TeX distribution on my laptop.
    – manjusha
    Feb 7, 2012 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .