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

This question already has an answer here:

How do I show a code snippet directly in my text? I dont want to have an extra box for it like lstlisting or verbatim provide it. My example may should look like this:

The class List.class is...

Should I make a new command for grey background and code like font? Or is there a simpler way? If not, how to write a new command for that?

share|improve this question

migrated from stackoverflow.com Jul 31 '12 at 2:17

This question came from our site for professional and enthusiast programmers.

marked as duplicate by Stefan Kottwitz Dec 20 '15 at 12:40

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Welcome to TeX.sx! Your question was migrated here from Stack Overflow. Please register on this site, too, and make sure that both accounts are associated with each other (by using the same OpenID), otherwise you won't be able to comment on or accept answers or edit your question. – Werner Jul 31 '12 at 2:31
up vote 28 down vote accepted

Since LaTeX is meant primarily intended for printed text I would strongly recommend you to not use e.g. grey background, but just use a typewriter font, e.g.

The class \texttt{List.class} is \ldots

You can and should wrap this in a new command to be free to change the visual appearance later, e.g.


so you can use a form which is free of formating distractions and conveys intention more clearly

The class \code{List.class} is \ldots

If, however, you are intent upon marking the text with a gray background, you can extend the above format with a color definition and colorbox from the colors or xcolors package, e.g.


For more on the colors, see this tutorial for some other usage examples.

share|improve this answer

The listings package has a command \lstinline{snippet} that is used exactly for that. You can configure its appearance (including syntax highlighting) using the package options.

share|improve this answer
But backgroundcolor is not available for \lstinline. – kiss my armpit Jul 31 '12 at 9:32