# How can I ensure all teletype/monospaced text is in a certain color?

I'm trying to include some monospaced text, and I'd like it to be in a different color than the normal text. There will be several (read: hundreds) of instances of monospaced text, and I want it all to be in a certain color. I'm relatively new to LaTeX (only started using it in the last 8 months or so), so I can't offer much in the way of research.

I've imported the color package, so far. I'm looking for all instances of \texttt and \verb to be in a certain color, without having to type \color in front of all of them.

-
Welcome to TeX.SE. It is always best to compose a fully compilable MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. –  Peter Grill Nov 29 '11 at 1:48
Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. –  Werner Nov 29 '11 at 1:48

You could redefine \texttt to take an optional argument that allows you to switch colour if necessary, and if not, stick to some default colour.

In the minimal example below, \texttt[<color>]{<stuff>} has been redefined to take an optional <color> (default is black).

\documentclass{article}
\usepackage{xcolor}% http://ctan.org/pkg/xcolor
\let\oldtexttt\texttt% Store \texttt
\renewcommand{\texttt}[2][black]{\textcolor{#1}{\ttfamily #2}}% \texttt[<color>]{<stuff>}
\begin{document}
Here is some \texttt{monospaced} text that has
\texttt[blue]{a different colour}. You can even use
\texttt[green!30!red]{colour mixtures}.
\end{document}


The use of xcolor allows you to mix/blend colours as well rather than sticking to some fixed colour model.

The command \let\oldtexttt\texttt "stores" \texttt in \oldtexttt if you want to use it for something else. It's wasn't necessary for my example, but may have an impact in your larger document.

-
Thanks for this! Exactly what I wanted. –  gatesphere Nov 29 '11 at 2:29

You can redefine the \texttt and \verb commands:

\documentclass{article}
\usepackage{xcolor}

\definecolor{ttcolor}{RGB}{255,110,120}

% redefinition of \texttt
\let\Oldtexttt\texttt
\renewcommand\texttt[1]{{\ttfamily\color{ttcolor}#1}}

% redefinition of \verb
\makeatletter
\def\verb{\relax\ifmmode\hbox\else\leavevmode\null\fi
\bgroup\color{ttcolor}
\verb@eol@error \let\do\@makeother \dospecials
\verbatim@font\@noligs
\@ifstar\@sverb\@verb}
\makeatother

\begin{document}

\texttt{test monospaced} and some normal font

\verb!test verbatim! and some normal font

\end{document}


-
Why not acting on \verbatim@font, so that also the verbatim environment would be affected? –  egreg Nov 29 '11 at 7:32

If you use xelatex or lualatex then the fontspec package has commands that allow you to specify the colour of a font. As an example:

\documentclass[border=10]{standalone}
\usepackage{fontspec}
\usepackage{xcolor}
\setmainfont{texgyrebonum-regular.otf}
\setmonofont[Colour=blue]{Inconsolata}
\newfontface\redverbfont[Colour=red]{Inconsolata}

\newcommand\redtt[1]{{\redverbfont #1}}
\makeatletter
\newcommand\redverb{\begingroup\let\verbatim@font=\redverbfont\let\verb@egroup=\colverb@egroup\verb} %}
\def\colverb@egroup{\global\let\verb@balance@group\@empty\egroup\endgroup}
\makeatother
\begin{document}

A surprisingly useful feature of the \texttt{fontspec} package is that it allows one to specify a colour for a font.
So by using it with the \verb+\setmonofont+ command, all monospaced text can be coloured.

It's even possible to define other \redtt{coloured monospace} using the \redverb+\newfontface+ command.
\end{document}


This produces:

Note that the only complicated code is hacking the \verb command so that \redverb is a special "do the same as \verb but make the font red instead.".

-