![突出显示列表中 [ 和 ] 之间的字符串](https://linux22.com/image/242708/%E7%AA%81%E5%87%BA%E6%98%BE%E7%A4%BA%E5%88%97%E8%A1%A8%E4%B8%AD%20%5B%20%E5%92%8C%20%5D%20%E4%B9%8B%E9%97%B4%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2.png)
我想要用来lstlisting
呈现这样的文件:
#comment
[keyword]
key1=value1
key2=value with RegEx
[keyword:NewKeyword]
key1 = value1
key2=value with RegEx
这是我的语言定义:
\lstdefinelanguage{myLang}{
sensitive=false,
morestring=[b]",
keywords={=},
morecomment=[l]{\#},
}
我怎样才能提取[keyword]
和[keyword:NewKeyword]
?
答案1
如果您想突出显示带有 的单词[...]
,则可以使用moredelim
(as with myLangStyleA
),它会丢弃分隔符并应用给定的样式。如果您想保留括号,可以使用morecomment
(as with myLangStyleB
)
\documentclass[border=2pt]{standalone}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{filecontents}% only need to provide the file foo.mylang
\begin{filecontents*}{foo.mylang}% provide file foo.mylang
#comment
[keyword]
key1=value1
key2=value with RegEx
[keyword:NewKeyword]
key1 = value1
key2=value with RegEx
\end{filecontents*}
\lstdefinelanguage{myLang}{
sensitive=false,
morestring=[b]",
keywords={=},
morecomment=[l]{\#},
}
\lstdefinestyle{myLangStyleA}{
language=myLang,
moredelim=[is][\color{blue}\ttfamily]{[}{]}
}
\lstdefinestyle{myLangStyleB}{
language=myLang,
morecomment=[s][\color{red}\ttfamily]{[}{]},
}
\begin{document}
Using \textbf{style=myLangStyleA}:
\lstinputlisting[style=myLangStyleA]{foo.mylang}
Using \textbf{style=myLangStyleB}:
\lstinputlisting[style=myLangStyleB]{foo.mylang}
\end{document}