[Tex/LaTex] Changing color of text snippet in a verbatim environment

colormath-modeverbatim

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.

Best Answer

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.

enter image description here

\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}