TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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 '12 at 12:08
up vote 13 down vote accepted

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}







share|improve this answer

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

share|improve this answer
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 '12 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 '12 at 20:48
Thanks for pointing out! I will update TeX distribution on my laptop. – manjusha Feb 7 '12 at 18:59

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.