TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

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 would like to be able to make a single word in a text look like a coded word. Is there any command such as \code{...} which allows me to do so?

(basically, I want to produce something like above for the "\code{...}" part)

share|improve this question
Do you want it to change the background to grey too? – N.N. Nov 24 '11 at 11:15
@N.N : it could have been even better yeah, but it's not extremely important. – SRKX Nov 24 '11 at 12:46
up vote 29 down vote accepted

Normally a monospaced font is used for this. This is accomplished with \texttt{...}. If you want to use code, you can use \def\code#1{\texttt{#1}}. From that point on you can write \code{...} to get monospaced output.

share|improve this answer
Yes, that's the way to go. Put it semantically correct (by calling it code) and use texttt inside this until you come across a better formatting. – wal-o-mat Nov 24 '11 at 11:15
I'd prefer \newcommand{\code}[1]{\texttt{#1}} as you're dealing with LaTeX. Even better \newcommand{\code}{\texttt}, but this is quite advanced. – egreg Nov 24 '11 at 11:40
@egreg I'm not so sure about the last approach (yes, I know why you're doing it, but from an interface POV I prefer macros where the arguments required are clear). – Joseph Wright Nov 24 '11 at 11:41
@JosephWright That's why I wrote that it's advanced. :) – egreg Nov 24 '11 at 11:50
I would also wrap a \mbox around the \texttt, to avoid the word being hyphenated if it happens to fall near the end of a line. – Karl Ove Hufthammer Nov 24 '11 at 11:51

\verb|code| or \verb#code# also works. It creates characters in monospace, although its primary utility to enter commands that the compiler wont confuse as tex commands.

share|improve this answer

If you want a single word to look like a coded word and also to have a light-gray background as in StackExchange you can predefine a color \definecolor{light-gray}{gray}{0.95} and then define a new command: \newcommand{\code}[1]{\colorbox{light-gray}{\texttt{#1}}}.

From this point on you can use \code{word} to get monospaced words with gray backgroud.

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.