\lstinline & \colorbox

\lstinline & \colorbox

为什么下面的命令不起作用?我该如何修复它?

\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}

答案1

内部参数的使用\lstinline是实验性的(参见5.1 参数内的列表清单文件)并且很容易被破坏;修改马丁·沙雷尔的回答lstinline 的不同背景颜色可以用来解决您的问题:

\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}

在此处输入图片描述

相关内容