# How to change color in an environment?

I have an environment defined as follows:

\usepackage{framed}
\usepackage{listings}
\usepackage{color}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{backgrounds}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\definecolor{gray}{gray}{0.3}
\definecolor{darkgreen}{rgb}{0,0.55,0}
\definecolor{purple}{rgb}{0.5,0,1}

\newenvironment{comment}{
\medskip
\begin{framed}
\color{red}
{\textbf{I say: }}
}{
\end{framed}
\medskip
}


After leaving the environment the color is not restored and so it is still red. Should I restore it explicitly somewhere?

-
can you make a complete document showing all packages used. It is very hard to debug a fragment. Looks like a bug in something not being "color safe" as defined by the color package documentation. –  David Carlisle Sep 13 '12 at 12:44
Perhaps you may have a look to the mdframed package to define your own colored environment. –  Claudio Fiandrino Sep 13 '12 at 12:48
I can't used mdframed because of tex.stackexchange.com/questions/71345/… –  Manuel Selva Sep 13 '12 at 12:49

It seems that framed messes up with colors; just declare the color before entering the environment:

\newenvironment{comment}
{\par\medskip
\color{red}%
\begin{framed}
\textbf{I say: }\ignorespaces}
{\end{framed}
\medskip}


I've reformatted the code and inserted the important \ignorespaces; remember that spaces (or end-of-lines) that don't follow a control sequence are significant. For the same reason, any environment whose "begin" part starts a paragraph should issue \ignorespaces: this gobbles the space after \begin{comment} (it's an end-of-line, but it's the same to TeX).

This definition will produce a red frame. If you want a black frame, then

\newenvironment{comment}
{\par\medskip
\begin{framed}
\begingroup\color{red}%
\textbf{I say: }\ignorespaces}
{\endgroup\end{framed}
\medskip}


will do.

-
why did you add \par ? –  Manuel Selva Sep 13 '12 at 14:13
@ManuelSelva It's true that framed starts a new paragraph, but the \medskip command is usually better placed between paragraphs. The \par ensures correct behavior even if you don't leave an empty line before \begin{comment}. –  egreg Sep 13 '12 at 14:18

The quickest fix seems to be insert the colored text inside a group.

The code:

\documentclass{article}

\usepackage{framed}
\usepackage{color}
\definecolor{gray}{gray}{0.3}
\definecolor{darkgreen}{rgb}{0,0.55,0}
\definecolor{purple}{rgb}{0.5,0,1}

\newenvironment{comment}{
\medskip
\begin{framed}
\bgroup\color{red}
{\textbf{I say: }}
}{
\egroup\end{framed}
\medskip
}

\begin{document}
Foo.
\begin{comment}
Here is the content of the \verb!comment! environment.
\end{comment}
Foo.
\end{document}


The result:

Is this what you want to achieve?

-

An easy option is to make color environments, even nested and mixed with \textcolor:

\documentclass{article}
\usepackage{framed}
\usepackage{color}
\definecolor{purple}{rgb}{0.5,0,1}

\newenvironment{comment}{
\medskip
\begin{color}{red}
\begin{framed}
\begin{color}{blue}
\textcolor{purple}{\textbf{I say: }}
}{
\end{color}
\end{framed}
\end{color}
\medskip
}

\begin{document}
Normal text before
\begin{comment}
... Hello
\end{comment}
Normal text after
\end{document}

-