我想要用来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}