lstlisting 环境标题中的 \lstinline 问题

lstlisting 环境标题中的 \lstinline 问题

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor} 

\lstset{
    backgroundcolor=\color{gray},  
    emph={FMD},
    emphstyle={\color{orange}},
    emph={[2]RAW},
    emphstyle={[2]\color{blue}}
}

\begin{document}
\begin{lstlisting}[caption={Caption with {\lstinline[emphstyle=\ttfamily,basicstyle=\ttfamily]+FMD+} and {\lstinline[emphstyle=\ttfamily,basicstyle=\ttfamily]+RAW+}}]
This line inclues FMD and RAW.
\end{lstlisting}
\end{document}

我明白了

在此处输入图片描述

当有多个级别的强调样式时,如何禁用标题中的突出显示?

答案1

\lstinline在标题中使用这里没有意义:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor} 

\lstset{
    backgroundcolor=\color{gray},  
    emph={FMD},
    emphstyle={\color{orange}},
    emph={[2]RAW},
    emphstyle={[2]\color{blue}}
}

\begin{document}
\begin{lstlisting}[caption={Caption with {\ttfamily FMD} and {\ttfamily RAW}}]
This line inclues FMD and RAW.
\end{lstlisting}

\begin{lstlisting}[caption={Caption with {\lstinline[emphstyle=\ttfamily,basicstyle=\ttfamily]+FMD+} and {\lstinline[emphstyle=\ttfamily,basicstyle=\ttfamily]+RAW+}}]
This line inclues FMD and RAW.
\end{lstlisting}

\end{document}

在此处输入图片描述

答案2

解决方案其实很简单:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor} 

\lstset{
    backgroundcolor=\color{gray},  
    emph={FMD},
    emphstyle={\color{orange}},
    emph={[2]RAW},
    emphstyle={[2]\color{blue}}
}

\newcommand{\inline}[1]{\lstinline[emphstyle={\ttfamily},emphstyle={[2]\ttfamily},basicstyle=\ttfamily][#1[}

\begin{document}

\begin{lstlisting}[caption={Caption with \inline{FMD} and \inline{RAW}}]
This line inclues FMD and RAW.
\end{lstlisting}

\end{document}

相关内容