我将显示的设置listings package
,如下所示 MWE:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
backgroundcolor=\color{yellow!10},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{green},
deletekeywords={...},
escapeinside={\%*}{*)},
frame=leftline,
keepspaces=true,
keywordstyle=\color{blue},
language={[LaTeX]{TeX}},
morekeywords={documentclass,document,book,article,usepackage,begin,end,newcommand,renewcommand,table,tabular,figure,toprule,midrule,bottomrule},
numbers=left,
numbersep=5pt,
numberstyle=\ttfamily\color{blue},
rulecolor=\color{blue},
showlines=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{blue},
tabsize=2,
title=\lstname
}
\begin{document}
\begin{lstlisting}[caption=Listings Package Setting]
\lstset{
backgroundcolor=\color{yellow!10},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{green},
deletekeywords={...},
escapeinside={\%*}{*)},
frame=leftline,
keepspaces=true,
keywordstyle=\color{blue},
language={[LaTeX]{TeX}},
morekeywords={documentclass,document,book,article,usepackage,begin,end,newcommand,renewcommand,table,tabular,figure,toprule,midrule,bottomrule},
numbers=left,
numbersep=5pt,
numberstyle=\ttfamily\color{blue},
rulecolor=\color{blue},
showlines=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{blue},
tabsize=2,
title=\lstname
}
\end{lstlisting}
\end{document}
但没有显示我想要的
答案1
这是因为您将设置复制到了所有设置都适用的列表。因此,您不仅将列表定义为escape
inside
一组元素,而且还提供这些元素。
我只是暂时禁用escapeinside
包含您的设置的列表的键值:
\begin{lstlisting}[caption=Listings Package Setting, escapeinside={}{}]
这提供了输出:
答案2
这是因为escapeinside
它会为你创建不在列表区域的区域,你可以LaTeX
在那里编写代码,例如
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
backgroundcolor=\color{yellow!10},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{green},
deletekeywords={...},
escapeinside={\%*}{*)},
frame=leftline,
keepspaces=true,
keywordstyle=\color{blue},
language={[LaTeX]{TeX}},
morekeywords={documentclass,document,book,article,usepackage,begin,end,newcommand,renewcommand,table,tabular,figure,toprule,midrule,bottomrule},
numbers=left,
numbersep=5pt,
numberstyle=\ttfamily\color{blue},
rulecolor=\color{blue},
showlines=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{blue},
tabsize=2,
title=\lstname
}
\begin{document}
\begin{lstlisting}[caption=Listings Package Setting]
\lstset
backgroundcolor=\color{yellow!10},
basicstyle=\ttfamily\footnotesize,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{green},
deletekeywords={...},
escapeinside={%* book,article are keywords but here!!\\
I can use \LaTeX codes here like $\Sigma$
*)},
frame=leftline,
keepspaces=true,
keywordstyle=\color{blue},
language={[LaTeX]{TeX}},
morekeywords={documentclass,document,book,article,usepackage,begin,end,newcommand,renewcommand,table,tabular,figure,toprule,midrule,bottomrule},
numbers=left,
numbersep=5pt,
numberstyle=\ttfamily\color{blue},
rulecolor=\color{blue},
showlines=true,
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{blue},
tabsize=2,
title=\lstname
\end{lstlisting}
\end{document}
因此 escapeinside 之间的代码%*[Code here]*)
将是 LaTeX 代码,当您复制它时}{
会导致错误,因为它是 LaTeX 文档中的错误。
如果你想要精确的文本,请使用这个escapeinside=%*\{\textbackslash \%\textasteriskcentered\}\{\textasteriskcentered)\}*)