# Adding a highlight to \texttt blocks

I'm trying the add a grey highlight to the \texttt command to essentially achieve the same effect as the code blocks on here.

This is something I attempted to do when I first started writing my current document. I don't remember how I got there but the renew command I wrote looks like this:

\definecolor{Light}{gray}{.90}
\sethlcolor{Light}

\renewcommand{\texttt}{\hl}


This gives me the grey highlight, but the font it no longer monospace.

Can anyone help me?

-
See this answer for verbatim mode, but it could be what you want –  egreg Jan 31 '12 at 19:10

You need to make a small tweak:

\let\OldTexttt\texttt
\renewcommand{\texttt}[1]{\OldTexttt{\hl{#1}}}


to get normal text \texttt{foo bar} normal text to yield:

## Alternative Solution:

Note that the above solution will effect all uses of \texttt{}. If that is not desired, an alternative solution would be to define a custom macro:

\newcommand{\hltexttt}[1]{\texttt{\hl{#1}}}


and then use \hltexttt{} when you want both the \hl and \texttt effect.

## Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{soul}

\definecolor{Light}{gray}{.90}
\sethlcolor{Light}

\let\OldTexttt\texttt
\renewcommand{\texttt}[1]{\OldTexttt{\hl{#1}}}% will affect all \texttt

\begin{document}
normal text \texttt{foo bar} normal text
\end{document}

-
Great, thanks a lot! –  Jivings Jan 31 '12 at 19:48