如何修改键值选项
\lstdefinelanguage{mybnf}{
morestring = [b]''
}
得到以下结果:
也就是说,能够以与内容不同的样式(在本例中是不同的颜色)定义分隔符?
以下是我的 MWE:
\documentclass{article}
\usepackage{fontspec}
\usepackage{longtable, booktabs}
\usepackage{listings, listings-ext, listingsutf8}
\usepackage{xcolor}
\setmainfont{Tisa OT}
\setsansfont{TeX Gyre Heros Cn}
\setmonofont{Fira Code}
%
% general listing colors
%
\definecolor{listing-background}{HTML}{F7F7F7}
\definecolor{listing-rule}{HTML}{B3B2B3}
\definecolor{listing-numbers}{HTML}{B3B2B3}
\definecolor{listing-text-color}{HTML}{000000}
\definecolor{listing-keyword}{HTML}{435489}
\definecolor{listing-keyword-2}{HTML}{1284CA} % additional keywords
\definecolor{listing-keyword-3}{HTML}{9137CB} % additional keywords
\definecolor{listing-identifier}{HTML}{435489}
\definecolor{listing-string}{HTML}{00999A}
\definecolor{listing-comment}{HTML}{8E8E8E}
\lstdefinestyle{etturaz_listings_style}{
language = java,
alsolanguage = scala,
numbers = left,
xleftmargin = 2.7em,
framexleftmargin = 2.5em,
backgroundcolor = \color{white},
basicstyle = \color{listing-text-color}\linespread{1.0}\small\ttfamily,
breaklines = true,
frame = single,
framesep = 0.19em,
rulecolor = \color{listing-rule},
frameround = ffff,
tabsize = 3,
numberstyle = \color{listing-numbers},
aboveskip = 1.0em,
belowskip = 0.1em,
abovecaptionskip = 0em,
belowcaptionskip = 1.0em,
keywordstyle = {\color{listing-keyword}\bfseries},
keywordstyle = {[2]\color{listing-keyword-2}\bfseries},
keywordstyle = {[3]\color{listing-keyword-3}\bfseries\itshape},
sensitive = true,
identifierstyle = \color{listing-identifier},
commentstyle = \color{listing-comment},
stringstyle = \color{listing-string},
showstringspaces = false,
escapeinside = {/*@}{@*/}, % Allow LaTeX inside these special comments
literate = {«}{{\guillemotleft}}1 {»}{{\guillemotright}}1
{…}{{\ldots}}1 {≥}{{>=}}1 {≤}{{<=}}1 {„}{{\glqq}}1 {“}{{\grqq}}1
{”}{{''}}1 {<\%}{{{\color{listing-keyword}<\%}}}2
}
\lstset{style=etturaz_listings_style}
\lstdefinelanguage{Scala}{
morekeywords={
% normal keywords (without data types)
abstract, case, catch, class, def, do, else, extends, false, final,
finally, for, forSome, if, implicit, import, lazy, macro, match, new,
null, object, override, package, private, protected, return, sealed,
super, this, throw, trait, try, true, type, val, var, while, with, yield,
_, :, ;, =, =>, <-, <:
},
morekeywords={[2] % data types
String, Boolean,Byte,Character,Double,Float,Integer,Long,Short, Number,BigDecimal,BigInteger
},
morekeywords={[2] % data types
Seq, List, Map
},
sensitive,
morecomment = [l]//,
morecomment = [s]{/*}{*/},
morecomment = [s]{/**}{*/},
%
morestring = [b]',
morestring = [s][\color{black}]{"}{"}
}
\lstdefinelanguage{xbnf}{
morekeywords={
% normal keywords (without data types)
::=
}
sensitive,
morecomment = [l]//,
morecomment = [s]{(*}{*)},
morestring = [b]',
morestring = [s][\color{black}]{"}{"}
}
\begin{document}
% \lstset{language=Scala}
\begin{lstlisting}[language=Scala]
def stringDivideBy(aStr: String, bStr: String): Option[Int] =
parseInt(aStr).flatMap { aNum =>
parseInt(bStr).flatMap { bNum =>
divide(aNum, bNum)
}
}
\end{lstlisting}
\begin{lstlisting}[language=xbnf]
json ::= element
value ::= object | array | string | number | "true" | "false" | "null"
(* So here the two apostrophes in '{' should be green and the bracket black *)
object ::= '{' ws '}' | '{' member {',' member} '}'
member ::= ws string ws ':' element
array ::= '[' ws ']' | '[' element {',' element} ']'
element ::= ws value ws
(* Here, again, two green black and the double quote black*)
string ::= '"' '"' | character {character} '"'
character ::= 'A' | ... | 'Z' | 'a' | ... | 'z' | escape
\end{lstlisting}
\end{document}
谢谢
答案1
好吧,由于某种原因,我错过了一个相关的问答:强调(颜色)列表中两个分隔符之间的内容,但不强调分隔符本身其中包含了我部分问题的答案。因此,我对 MWE 进行了如下修改:
\documentclass{article}
\usepackage{fontspec}
\usepackage{longtable, booktabs}
\usepackage{listings, listings-ext, listingsutf8}
\usepackage[
usenames,%
svgnames,%
dvipsnames,%
x11names]{xcolor}
\setmainfont{Tisa OT}
\setsansfont{TeX Gyre Heros Cn}
\setmonofont{Fira Code}
%
% general listing colors
%
\definecolor{listing-background}{HTML}{F7F7F7}
\definecolor{listing-rule}{HTML}{B3B2B3}
\definecolor{listing-numbers}{HTML}{B3B2B3}
\definecolor{listing-text-color}{HTML}{000000}
%\definecolor{listing-keyword}{HTML}{435489}
\definecolor{listing-keyword}{RGB}{95,95,211}
\definecolor{listing-keyword-2}{HTML}{1284CA} % additional keywords
\definecolor{listing-keyword-3}{HTML}{9137CB} % additional keywords
\definecolor{listing-identifier}{HTML}{435489}
\definecolor{listing-string}{HTML}{00999A}
\definecolor{listing-comment}{HTML}{8E8E8E}
%===============================%
%% Here the suggested solution %%
%
\def\beginlstdelim#1#2#3%
{%
\def\endlstdelim{#2\egroup}%
\small\ttfamily#1\bgroup\color{#3}\aftergroup\endlstdelim%
}
%===============================%
%
% (X)BNF Colors
%
\definecolor{xbnf-symbol}{RGB}{95,95,211}
\lstdefinestyle{etturaz_listings_style}{
language = scala,
numbers = left,
xleftmargin = 2.7em,
framexleftmargin = 2.5em,
backgroundcolor = \color{white},
basicstyle = \color{listing-text-color}\linespread{1.0}\small\ttfamily,
breaklines = true,
frame = single,
framesep = 0.19em,
rulecolor = \color{listing-rule},
frameround = ffff,
tabsize = 3,
numberstyle = \color{listing-numbers},
aboveskip = 1.0em,
belowskip = 0.1em,
abovecaptionskip = 0em,
belowcaptionskip = 1.0em,
keywordstyle = {\color{listing-keyword}\bfseries},
keywordstyle = {[2]\color{listing-keyword-2}\bfseries},
keywordstyle = {[3]\color{listing-keyword-3}\bfseries\itshape},
sensitive = true,
identifierstyle = \color{listing-identifier},
commentstyle = \color{listing-comment},
stringstyle = \color{listing-string},
showstringspaces = false,
escapeinside = {/*@}{@*/}, % Allow LaTeX inside these special comments
% mathescape = true,
literate = {«}{{\guillemotleft}}1 {»}{{\guillemotright}}1
{…}{{\ldots}}1 {≥}{{>=}}1 {≤}{{<=}}1 {„}{{\glqq}}1 {“}{{\grqq}}1
{”}{{''}}1
}
\lstset{style=etturaz_listings_style}
\lstdefinelanguage{Scala}{
morekeywords={
% normal keywords (without data types)
abstract, case, catch, class, def, do, else, extends, false, final,
finally, for, forSome, if, implicit, import, lazy, macro, match, new,
null, object, override, package, private, protected, return, sealed,
super, this, throw, trait, try, true, type, val, var, while, with, yield,
_, :, ;, =, <-, <:
},
morekeywords={[2] % data types
String, Boolean,Byte,Character,Double,Float,Integer,Long,Short, Number,BigDecimal,BigInteger
},
morekeywords={[2] % data types
Seq, List, Map
},
otherkeywords = {=>, {, }, (, ) },
sensitive,
morecomment = [l]//,
morecomment = [s]{/*}{*/},
morecomment = [s]{/**}{*/},
%
morestring = [b]',
morestring = [s]{"}{"},
alsoletter = {\%},
literate =
{+=}{{{\color{listing-keyword}+=}}}2
{=>}{{{\color{listing-keyword}=>}}}2
{<\%}{{{\bfseries\color{listing-keyword}<\%}}}2
{\{ }{{{\bfseries\color{listing-keyword}\{}}}1
{[}{{{\bfseries\color{listing-keyword}[}}}1
{()}{{{\bfseries\color{listing-keyword}(}}}1
{)}{{{\bfseries\color{listing-keyword})}}}1
{]}{{{\bfseries\color{listing-keyword}]}}}1
{\} }{{{\bfseries\color{listing-keyword}\}}}}1
{:}{{{\bfseries\color{listing-keyword}:}}}1
{=}{{{\bfseries\color{listing-keyword}=}}}1
}
\lstdefinelanguage{xbnf}{
keywordstyle = {\color{xbnf-symbol}\bfseries},
morekeywords={
% normal keywords (without data types)
{::=}
}
sensitive,
morecomment = [l]//,
morecomment = [s]{(*}{*)},
morestring = [b]',
%===============================%
%% Here the suggested solution %%
%
moredelim = **[is][{\color{red}\beginlstdelim{"}{"}{listing-identifier}}]{"}{"},
moredelim = **[is][{\color{red}\beginlstdelim{'}{'}{listing-identifier}}]{'}{'},
%===============================%
literate =
% {::=}{{{\bfseries\color{xbnf-symbol}::=}}}3
{|}{{{\bfseries\color{xbnf-symbol}|}}}1
{\{ }{{{\bfseries\color{xbnf-symbol}\{}}}1
{[}{{{\bfseries\color{xbnf-symbol}[}}}1
{()}{{{\bfseries\color{xbnf-symbol}(}}}1
{)}{{{\bfseries\color{xbnf-symbol})}}}1
{]}{{{\bfseries\color{xbnf-symbol}]}}}1
{\} }{{{\bfseries\color{xbnf-symbol}\}}}}1
}
\begin{document}
% \lstset{language=Scala}
\begin{lstlisting}[language=Scala]
/**
* A small snippet taken from Cat UG
*/
def stringDivideBy(aStr: String, bStr: String ) : Option[Int] =
parseInt(aStr).flatMap { aNum =>
parseInt(bStr).flatMap { bNum =>
divide(aNum, bNum)
}
}
val str: String = "This is a string"
\end{lstlisting}
\begin{lstlisting}[language=xbnf]
json ::= element
value ::= object | array | string | number | "true" | "false" | "null"
(* So here the two apostrophes in '{' should be green and the bracket black *)
object ::= '{tests' ws '}' | '{' member {',' member} '}'
member ::= ws string ws ':' element
array ::= '[' ws ']' | '[' element {',' element} ']'
element ::= ws value ws
(* Here, again, two green black and the double quote black*)
string ::= '"' '"' | character {character} '"'
character ::= 'A' | ... | 'Z' | 'a' | ... | 'z' | escape
\end{lstlisting}
\end{document}
不过,正如您所看到的,仍然存在一些问题:
- 起始分隔符后面是一个非常讨厌的空格,我无法弄清楚为什么,即使当我运行从所提问题的解决方案中获取的片段时,一切正常;
- 双引号和单引号一旦定义为分隔符,就不能用作常规文本。
有什么想法吗?谢谢