TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

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}
share|improve this question
up vote 3 down vote accepted

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

share|improve this answer
    
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? – Malte Schwerhoff May 4 at 8:24

Your Answer

 
discard

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.