Take the 2-minute tour ×
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.

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
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 '12 at 12:08
add comment

2 Answers

up vote 12 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.

\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}
share|improve this answer
add comment

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:

\documentclass{article}

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

\usepackage{xcolor}

\usepackage{fancyvrb}
\DefineVerbatimEnvironment{ColorVerbatim}{Verbatim}%
  {formatcom=\color{purple},commandchars=\\\{\}}

\usepackage{etoolbox}
\BeforeBeginEnvironment{Verbatim}{\begingroup\color{green}}%
\AfterEndEnvironment{Verbatim}{\endgroup}%

\begin{document}

\blindtext[1]

\begin{Verbatim}
Verbatim text with by hook predefined colour:
This blindtext is no pangram.
\end{Verbatim}

\begin{Verbatim}[commandchars=\\\{\}]
Verbatim text with individual colour settings:
{\color{red}\blindtext[1]}
\textcolor{blue}{\blindtext[1]}
\end{Verbatim}

\begin{ColorVerbatim}
Custom verbatim environment with predefined colour:
\blindtext[1]

... and with individual colour settings:
\textcolor{orange}{\blindtext[1]}
{\color{teal}\blindtext[1]}
\end{ColorVerbatim}

\blindtext[1]

\end{document}

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
add comment

Your Answer

 
discard

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.