# Use color in verbatim environment

Inside the verbatim environment, is it possible to use a different color, please? For example, I want a to be normal, i.e., black. But I want b to be red.

\documentclass[]{article}

\begin{document}

\begin{verbatim}
a (black)
b (I want this letter to be red)
\end{verbatim}

\end{document}


Follow-up Question:

The solution provided by Ignasi was neat. However, there is a problem. If I need to write the entire verbatim in a non-black color, then it will not work.

\documentclass[]{article}
\usepackage{fancyvrb}
\usepackage{xcolor}

\begin{document}

I want the following to look exactly like the way I put, i.e., like an align. But it displays in one line. How to fix?

\begin{Verbatim}[commandchars=\\\{\}]
\textcolor{red}{
a = b + 1
= x + y
= s + t
= c - d
= o * p
}
\end{Verbatim}

\end{document}


Not exactly the same solution pointed by flav:

\documentclass[]{article}
\usepackage{fancyvrb}
\usepackage{xcolor}

\begin{document}

\begin{Verbatim}[commandchars=\\\{\}]
a (black)
\textcolor{red}{b} (I want this letter to be red)
\end{Verbatim}

\end{document}


Note: Credits to fancyvrb documentation

Answer to follow-up question:

To change the color for all contents of verbatim use a colored scope.

\documentclass[]{article}
\usepackage{fancyvrb}
\usepackage{xcolor}

\begin{document}

\begin{Verbatim}[commandchars=\\\{\}]
a (black)
\textcolor{red}{b} (I want this letter to be red)
\end{Verbatim}

{
\color{blue}
\begin{Verbatim}[commandchars=\\\{\}]
a (black)
\textcolor{red}{b} (I want this letter to be red)
\end{Verbatim}
}

\end{document}


• I would assume that this idea can be extended to other common font. For example, if I want to use bold face for certain letter, then I can just use \textbf{b}. Right? – LaTeXFan May 18 '17 at 9:22
• @LaTeXFan Yes. Did you try it? – Ignasi May 18 '17 at 9:44
• Yes. It worked. Cheers. – LaTeXFan May 18 '17 at 10:54
• I have a follow-up question for you. Thanks. – LaTeXFan May 19 '17 at 1:14
• @LaTeXFan {\color{red}\begin{Verbatim}...\end{Verbatim}} – Ignasi May 19 '17 at 6:50

A verbatimbox approach, using < and > as active delimiters of the red text. Invoked by providing the \vbdelim macro as the optional argument to the verbnobox environment.

\documentclass{article}
\usepackage{xcolor,verbatimbox}
\catcode>=\active %
\catcode<=\active %
\def\openesc{\color{red}}
\def\closeesc{\color{black}}
\def\vbdelim{\catcode<=\active\catcode>=\active%
\def<{\openesc}
\def>{\closeesc}}
\catcode>=12 %
\catcode<=12 %
\begin{document}
\begin{verbnobox}[\vbdelim]
a (black)
<b> (I want this letter to be <red>)
\end{verbnobox}
\end{document}


Here is a more general version in which the color can be provided as an optional argument to <

\documentclass{article}
\usepackage{xcolor,verbatimbox}
\catcode>=\active %
\catcode<=\active %
\newcommand\openesc[1][red]{\color{#1}}
\def\closeesc{\color{black}}
\def\vbdelim{\catcode<=\active\catcode>=\active%
\def<{\openesc}
\def>{\closeesc}}
\catcode>=12 %
\catcode<=12 %
\begin{document}
\begin{verbnobox}[\vbdelim]
a (black)
<b> (I want this letter to be <red>)
<[blue!45]c> and this one to be <[blue!45]blue!45>
\end{verbnobox}
\end{document}


• This is wonderful. But the previous one does look tider. – LaTeXFan May 18 '17 at 10:55

If your verbatim environment doesn't really contain stuff that requires it (that is, it's just regular text), consider using the alltt environment (from alltt):

The alltt pack­age de­fines the alltt en­vi­ron­ment which is like the ver­ba­tim en­vi­ron­ment ex­cept that \ and braces have their usual mean­ings. Thus, other com­mands and en­vi­ron­ments can ap­pear within an alltt en­vi­ron­ment.

\documentclass{article}

\usepackage{alltt,xcolor}

\begin{document}

\begin{alltt}
a (black)
\textcolor{red}{b} (I want this letter to be red)
\end{alltt}

\end{document}