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)

  • 4
    Do you want it to change the background to grey too?
    – N.N.
    Nov 24 '11 at 11:15
  • 1
    @N.N : it could have been even better yeah, but it's not extremely important.
    – SRKX
    Nov 24 '11 at 12:46
  • 3
    The answer is texttt{} how that is an unhuman command. How is anyone suppose to remember this? If you figure out what the ttt's stand for please tell me! Oct 15 '19 at 16:12
  • 6
    @CharlieParker I'm pretty sure texttt stands for text teletype. Similarly there are for example \textrm, where rm stands for roman, and \textsf where sf stands for serif. Jan 30 '20 at 13:56

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.

  • 31
    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
  • 5
    @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
  • 3
    @JosephWright That's why I wrote that it's advanced. :)
    – egreg
    Nov 24 '11 at 11:50
  • 6
    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. Nov 24 '11 at 11:51
  • 2
    @wh1t3 Would you really want them hyphenated? That could become very confusing for the reader, especially for languages that use commands that can contain hyphens. For function names with arguments, I would define a separate command that handles this in a proper way, using penalties to get the optimal wrapping (e.g., never break the function name, but make is possible (but bad) to break at the comma between arguments). Nov 24 '11 at 12:58

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 mono-spaced words with gray background.

Of course for this to work you will need to load the xcolor package before \definecolor.

A full example would look like this:

% Better inline directory listings
  • 1
    Awesome, that's what I was looking for. Thanks :D
    – Sidahmed
    Feb 7 '17 at 11:41
  • 3
    This is what the people want!
    – sunspots
    Feb 12 '18 at 7:51
  • Hi. I've tried to do this in a document, but my document won't compile unless I surround the word like \code{$word$}, and then it puts everything in math font. When I don't it says "! Missing $ inserted.". Any ideas? Sep 10 '18 at 19:22
  • @lafemmecosmique Did your 'word' contain underscores?
    – Ilya Popov
    Sep 27 '18 at 16:02
  • 1
    Awesome. I just coded this exact solution myself (color and all) before searching for community solutions. Great minds think alike!
    – delrocco
    Aug 8 '19 at 15:10

I can't believe nobody mentioned the listings package. It provides a command called \lstinline{your_code} which can even highlight keywords for you.

See also this question: Should I use \lstinline for the language keywords embedded in text?

  • 2
    Syntax: \lstinline[⟨key=value list⟩]⟨character⟩⟨source code⟩⟨same character⟩ Also, some engines support a wider working range of working <character> delimiters. For exampe, I found ¿ to work with xelatex and not with pdflatex.
    – l --marc l
    Feb 18 '19 at 6:35
  • This should be the answer. No hands-on formatting where it can be avoided in LaTeX. Sep 28 '20 at 12:45

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

  • 2
    Why does \verb use | or # to start and end code rather than the usual { and }? Nov 29 '16 at 14:52
  • 1
    @user3728501 what if I wanted to give an example LaTeX command using verbatim? I'd need different delimiters to indicate when to start/stop the verbatim text (since the { and } would need to be rendered as verbatim text). Aug 6 '18 at 13:44

I would recommend my package ffcode, which makes it as simple as this:

The function \ff{foo} can be used in a loop:
while (true) {

The package uses minted for code blocks and tcolorbox for individual words.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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