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?
  • 1
    Yes, it would be nice, for consistency, if color options worked in \lstinline as they work in the lstlisting environment.
    – alfC
    Commented Jul 19, 2013 at 0:46

4 Answers 4


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, 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

  • 5
    @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. Commented Apr 9, 2012 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} Commented Apr 9, 2012 at 11:11
  • @MartinScharrer: is it possible to achieve the same effect just using \lstinline alone (without \Colorbox)? I use xcolor, define \definecolor{mygray}{rgb}{0.8,0.8,0.8}, and \lstset{basicstyle=\ttfamily, breaklines = true, backgroundcolor=\color{mygray}}but I doesn't work for \lstinline. This is \lstinline|my code|.
    – guest_5758
    Commented Mar 7, 2017 at 6:23
  • @guest_5758 - May I suggest that you re-post your inquiry as a new question (possibly with a link to this older question)? That way, it'll b seen by lots or people. Thanks.
    – Mico
    Commented Mar 7, 2017 at 6:29
  • 1
    @Mico: okay, done. tex.stackexchange.com/questions/357227/…
    – guest_5758
    Commented Mar 7, 2017 at 7:16

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
  • 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
    Commented Jul 15, 2013 at 9:06
  • @JLDiaz: Added a Known Issues to note that this won't work across line breaks (and also page breaks). Commented Jul 15, 2013 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
    Commented Jul 15, 2013 at 15:25
  • @JLDiaz: Yeah, I think posting a new question makes sense. Commented Jul 15, 2013 at 16:40
  • Done: tex.stackexchange.com/questions/124115/…
    – JLDiaz
    Commented Jul 15, 2013 at 17:06

One could also declare a new command (or document commant) \lsti:


% The following doesn't work in all cases, as pointed out by matexmatics
% \newcommand{\lsti}[1]{\colorbox{yellow}{\lstinline|#1|}}

% This works in more cases, as pointed out by mbert

This is a test where \lsti|A=1| should have a yellow background.

It should also work with \lsti|A=1%| or \lsti|A={}| or \lsti|{{{|.

enter image description here


You can combine lua-el (to highlight lines) and piton (for syntactic coloring of informatic listings). Both packages require a compilation with LuaLaTeX.

It's possible to allow line breaks with the key break-lines-in-piton.





\PitonOptions{break-lines-in-piton,language = OCaml}

Here is an example in OCaml.

If you want to test whether a year is a leap year : 
\InlineCode{let leap n = n mod 4 = 0  && ( n<=1582 || n mod 100 <> 0 || n mod 400 =0 ) ;;}


Output of the above code

The package piton is able to work with all the languages supported by listings.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .