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 make inline code more distinguishable from the text surrounding it, and I thought that using a background might work (like it is done on the stackexchange pages.)

Since I use the listings package for my other code samples, I tried the inline version of listings, but the key=value pairs seem not to work in the inline version (although the documentation seems to indicate that they should).

Full example:

This is a test where \lstinline[backgroundcolor=\color{yellow}]{A=1} should have a yellow background.

This, on the other hand, actually works:
    A = 1


  1. Can it be done with listings? And how?
  2. What other ways to increase the distinguishability of inline verbatim do you use?
share|improve this question
Yes, it would be nice, for consistency, if color options worked in \lstinline as they work in the lstlisting environment. – alfC Jul 19 '13 at 0:46
up vote 13 down vote accepted

I'm not familiar with most of the color- and highlighting-related options of the lstlistings package. However, the \colorbox macro, which is defined in the color package and takes two arguments (the color and the item to be placed in the colored box), will do a nice job of highlighting a piece of inline text. The following MWE builds directly on your code:

This is to test whether \colorbox{yellow}{\lstinline{A=1}} 
has a yellow background.

enter image description here

Addendum: As @MartinScharrer has pointed out in a comment to my original posting, the \colorbox command won't get the job done if the argument of your \lstinline command contains "real" verbatim material -- which may well feature some TeX "special" characters such as %, _, &, and so on. For that contingency, you should load the realboxes package and employ its \Colorbox macro:

This is to test whether \Colorbox{yellow}{\lstinline{A=@#$%^&*()1}} has a yellow background.

enter image description here

share|improve this answer
Thanks, that is a good solution. – xubuntix Sep 12 '11 at 9:40
@xubuntix: Actually it doesn't work with real verbatim code, because you can't have it in an argument of another macro. E.g. replace A=1 with A=1% and see how it breaks. You can however use \Colorbox from the realboxes package (requires xcolor too) which reads the content as a box, not as a macro argument. – Martin Scharrer Apr 9 '12 at 11:10
\documentclass{article} \usepackage{xcolor} \usepackage{listings} \usepackage{realboxes} \begin{document} This is to test whether \Colorbox{yellow}{\lstinline{A=%1}} has a yellow background. \end{document} – Martin Scharrer Apr 9 '12 at 11:11
THANKS, great solution! +1 – AndreasT Jul 30 '13 at 15:20

You could also use the solution from How to redefine \lstinline to automatically highlight or draw frames around all inline code snippets?

enter image description here

The highlighting code is from Highlight text in code listing while also keeping syntax highlighting

Known Issues:

  • This is intended for use with short inline code snippets that do not span line breaks.




% change this to customize the appearance of the highlight
\tikzstyle{highlighter} = [
  yellow,% set the color for inline listings here.
  line width = \baselineskip,

% enable these two lines for a more human-looking highlight
%\tikzstyle{highlighter} += [decorate, decoration = random steps]

% implementation of the core highlighting logic; do not change!
\newcommand{\tikzhighlightanchor}[1]{\ensuremath{\vcenter{\hbox{\tikz[remember picture, overlay]{\coordinate (#1 highlight \arabic{highlight});}}}}}
\AtBeginShipout{\AtBeginShipoutUpperLeft{\ifthenelse{\value{highlight} > 0}{\tikz[remember picture, overlay]{\foreach \stroke in {1,...,\arabic{highlight}} \draw[highlighter] (begin highlight \stroke) -- (end highlight \stroke);}}{}}}

\makeatletter %   Redefine macros from listings package:

    \leavevmode\bgroup\toggletrue{@InInlineListing}\bh % \hbox\bgroup --> \bgroup
      \@ifnextchar\bgroup{\afterassignment\lst@InlineG \let\@let@token}%



This is a test where \lstinline{A=1} should have a yellow background.

This, on the other hand, actually works:
    A = 1
share|improve this answer
This is great. However, when I used it at some pages there appear spureous highligths at places where there is not lstinline, or some highlights do not appear. Apparently what happens is that highlighs pertaining to a page appear in the previous page. This is related to highlights near the page break, and in combination with footnotes in the same page. Do you have a clue about why this happens and how to fix it? – JLDiaz Jul 15 '13 at 9:06
@JLDiaz: Added a Known Issues to note that this won't work across line breaks (and also page breaks). – Peter Grill Jul 15 '13 at 12:33
Thank you, but the problem is not due to the code being too long. It is indeed a short snippet (\lstinline!print!), but it happens at the end of a paragraph which latex decided to break among pages, perhaps as a consequence of a footnote or, the starting of a new section after that paragraph or this kind of reasons. Apparently, the paragraph is broken after the tikzmark is written in the aux file, and thus the information about the page in which should appear is wrong. If you want, I can post a new question with a MWE showing the problem. – JLDiaz Jul 15 '13 at 15:25
@JLDiaz: Yeah, I think posting a new question makes sense. – Peter Grill Jul 15 '13 at 16:40
Done: tex.stackexchange.com/questions/124115/… – JLDiaz Jul 15 '13 at 17:06

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.