Take the 2-minute tour ×
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.

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?

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

1 Answer 1

up vote 7 down vote accepted

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:

enter image description here

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
%\newcommand{\hltexttt}[1]{\texttt{\hl{#1}}}% comment above \renewcommand if want this

\begin{document}
normal text \texttt{foo bar} normal text 
\end{document}
share|improve this answer
    
Great, thanks a lot! –  Jivings Jan 31 '12 at 19:48

Your Answer

 
discard

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.