TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
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
up vote 12 down vote accepted

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).

enter image description here

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

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.

share|improve this answer
Thanks for this! Exactly what I wanted. – gatesphere Nov 29 '11 at 2:29

You can redefine the \texttt and \verb commands:



% redefinition of \texttt

% redefinition of \verb
    \verb@eol@error \let\do\@makeother \dospecials


\texttt{test monospaced} and some normal font

\verb!test verbatim! and some normal font


enter image description here

share|improve this answer
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:


\newcommand\redtt[1]{{\redverbfont #1}}
\newcommand\redverb{\begingroup\let\verbatim@font=\redverbfont\let\verb@egroup=\colverb@egroup\verb} %}

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.

This produces:

coloured monospace font

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.".

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.