5

Why the command below don't work? How can I fix it?

\documentclass[11pt]{book}

\usepackage[dvipsnames]{xcolor}

\usepackage{listings}

\lstset{language=[LaTeX]Tex,
    basicstyle=\normalfont\ttfamily,
    stringstyle=\rmfamily,
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frameround=ftff,
    frame=lines,
    morekeywords={RequirePackage,ProvidesPackage,NeedsTeXFormat},
    backgroundcolor=\color{background}
}

\definecolor{background}{HTML}{EEEEEE}

\newcommand*{\inlinecode}[1]{%
                            \colorbox{background}%
                            {%
                            \lstinline!#1!%
                            }
                            }

\begin{document}

ipsum dolor sit amet \inlinecode{%\@#&\newcommand=_^*} ipsum dolor sit amet

\end{document}
0

1 Answer 1

3

The use of \lstinline inside arguments is experimental (see Section 5.1 Listings inside arguments of the listings documentation) and can easily break; a modification of Martin Scharrer's answer to Different background colors for lstinline can be used to solve your problem:

\documentclass[11pt]{book}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\usepackage{newverbs}

\definecolor{keywords}{HTML}{8A4A0B}
\definecolor{background}{HTML}{EEEEEE}

\lstset{language=[LaTeX]Tex,
    basicstyle=\normalfont\ttfamily,
    stringstyle=\rmfamily,
    texcsstyle=*\color{keywords},
    numbers=left,
    numberstyle=\scriptsize,
    stepnumber=1,
    numbersep=8pt,
    showstringspaces=false,
    breaklines=true,
    frameround=ftff,
    frame=lines,
    morekeywords={RequirePackage,ProvidesPackage,NeedsTeXFormat},
    backgroundcolor=\color{background}
}

\makeatletter
\newcommand\inlinecode[1][background]{%
    \Collectverb{\@inlinecode{#1}}%
}
\def\@inlinecode#1#2{%
    \colorbox{#1}{\lstinline|#2|}%
}
\makeatother

\begin{document}

\inlinecode|\@#&\newcommand=_^*|

\end{document}

enter image description here

1
  • The provided solution doesn't support listings' mathescape option: \lstset{mathescape=true} followed by \inlinecode|$e$.f == 0| causes the error (using pdflatex) ! Undefined control sequence. \lst@arg ->$e. Can mathescape be supported? Commented May 4, 2016 at 8:24

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