Colored background in inline listings

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:

\documentclass{article}
\usepackage{color,listings}
\begin{document}
This is to test whether \colorbox{yellow}{\lstinline{A=1}} 
has a yellow background.
\end{document}

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


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.

References:

  • See follow up question: Problems at page boundaries trying to highlight `\lstinline`

Code:

\documentclass{article}
\usepackage{etoolbox}
\usepackage{atbegshi,ifthen,listings,tikz}

% 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
%\usetikzlibrary{decorations.pathmorphing}
%\tikzstyle{highlighter} += [decorate, decoration = random steps]

% implementation of the core highlighting logic; do not change!
\newcounter{highlight}[page]
\newcommand{\tikzhighlightanchor}[1]{\ensuremath{\vcenter{\hbox{\tikz[remember picture, overlay]{\coordinate (#1 highlight \arabic{highlight});}}}}}
\newcommand{\bh}[0]{\stepcounter{highlight}\tikzhighlightanchor{begin}}
\newcommand{\eh}[0]{\tikzhighlightanchor{end}}
\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:
\newtoggle{@InInlineListing}%
\togglefalse{@InInlineListing}%

\renewcommand\lstinline[1][]{%
    \leavevmode\bgroup\toggletrue{@InInlineListing}\bh % \hbox\bgroup --> \bgroup
      \def\lst@boxpos{b}%
      \lsthk@PreSet\lstset{flexiblecolumns,#1}%
      \lsthk@TextStyle
      \@ifnextchar\bgroup{\afterassignment\lst@InlineG \let\@let@token}%
                         \lstinline@}%

\def\lst@LeaveAllModes{%
    \ifnum\lst@mode=\lst@nomode
        \expandafter\lsthk@EndGroup\iftoggle{@InInlineListing}{\eh{}}{}%
    \else
        \expandafter\egroup\expandafter\lst@LeaveAllModes
    \fi%
    }
\makeatother


\lstset{backgroundcolor=\color{green!10}}%


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

This, on the other hand, actually works:
  \begin{lstlisting}[backgroundcolor=\color{green}]
    A = 1
  \end{lstlisting}
\end{document}