23

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:

\documentclass{article}
\usepackage{color}
\usepackage{listings}
\begin{document}
This is a test where \lstinline[backgroundcolor=\color{yellow}]{A=1} should have a yellow background.

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

Questions:

  1. Can it be done with listings? And how?
  2. What other ways to increase the distinguishability of inline verbatim do you use?
1
  • 1
    Yes, it would be nice, for consistency, if color options worked in \lstinline as they work in the lstlisting environment.
    – alfC
    Jul 19, 2013 at 0:46

2 Answers 2

18

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

5
  • 4
    @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. 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} 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
    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
    Mar 7, 2017 at 6:29
  • 1
    @Mico: okay, done. tex.stackexchange.com/questions/357227/…
    – guest_5758
    Mar 7, 2017 at 7:16
8

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:

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

You must log in to answer this question.

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