Take the 2-minute tour ×
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.

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
add comment

1 Answer 1

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
add comment

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.