# Changing color of text snippet in a verbatim environment

Is it possible to change color of part of the text that is in a verbatim environment?

Lets say I have this verbatim section. I want to change the color of the marked lines into blue.

\begin{verbatim}
BOT: (Begin of Transaction)

SQL-DML-1; // update Kunden (...)
--> (C1, C2, C3)  /* MAKE THIS RED */
SET CONSTRAINTS C4 IMMEDIATE;
--> (C4)          /* MAKE THIS RED */
SET CONSTRAINTS C3 DEFERRED;

SQL-DML-2; // update Produkte (...)
--> (C1, C2, C4)  /* MAKE THIS RED */
SQL-DML-3; // update Bestellungen (...)
--> (C1, C7, C4)  /* MAKE THIS RED */
EOT: (End of Transaction) (COMMIT) /* INSERT MATH SYMBOL HERE */
--> (C3, C5, C6)  /* MAKE THIS RED */
RBT: Roll-Back-Transaction (alles rückgängig gemacht)
\end{verbatim}


Additionally, is it possible to make mathematical symbols in a verbatim environment? I know that this is contradictory per se, but maybe there's a kind of hack to to this.

-
Take a look at the listings package, it most definitely will provide what you need. –  mafp Jan 10 '13 at 23:34

fancyvrb will do this very easily. To do math, just give all special math characters you need their appropriate catcodes. Also, you must avoid using any spaces in math mode, because they will be interpreted literally.

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{fancyvrb}
\usepackage{color}

\begin{document}

\begin{Verbatim}[commandchars=\\\{\},codes={\catcode$=3\catcode^=7\catcode_=8}] BOT: (Begin of Transaction) SQL-DML-1; // update Kunden (...) \color{red} --> (C1, C2, C3) /* MAKE THIS RED */ SET CONSTRAINTS C4 IMMEDIATE; \color{red} --> (C4) /* MAKE THIS RED */ SET CONSTRAINTS C3 DEFERRED; SQL-DML-2; // update Produkte (...) \color{red} --> (C1, C2, C4) /* MAKE THIS RED */ SQL-DML-3; // update Bestellungen (...) \color{red} --> (C1, C7, C4) /* MAKE THIS RED */ EOT: (End of Transaction) (COMMIT) /* INSERT MATH SYMBOL HERE$f(x)=x_a^2\$ */
\color{red}            --> (C3, C5, C6)  /* MAKE THIS RED */
RBT: Roll-Back-Transaction (alles rückgängig gemacht)
\end{Verbatim}

\end{document}

-
Thanks for that, it works. But I have the problem that the whole environment float to the right. Is this a missbehaviour? –  Valentino Ru Jan 12 '13 at 15:49
@ValentinoRu The spaces at the beginning of each line are treated as literal, so that would produce an indent to the right. If you don't want that, you can either remove the leading 4 spaces that each line has, or use the environment option gobble=4, which will automatically do the same thing. The \color commands at the beginning of some lines won't affect spacing. If you use this environment much, you might look into fancyvrb's \DefineVerbatimEnvironment, which lets you define a new environment with custom default options based off of Verbatim`. –  G. Poore Jan 12 '13 at 16:45