\lstdefinestyle 语法高亮

\lstdefinestyle 语法高亮

我是 Latex 新手,遇到了以下问题。我定义了一个用于 XML 语法突出显示的列表样式,如下所示:

在此处输入图片描述

我怎样才能在属性颜色(橙色)中获取等号?我想这样做

\lstdefinestyle{XML}{
    morekeywords={xmlns=,attribute\equals}
}

是否存在某种转义序列可以实现这一点,或者这是不可能的?


干得好:

\documentclass[a4paper, 12pt]{article}

\usepackage{listings, color}

\definecolor{forestgreen}{RGB}{34,139,34}
\definecolor{orangered}{RGB}{239,134,64}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{gray}{rgb}{0.4,0.4,0.4}

\lstdefinestyle{XML} {
    language=XML,
    extendedchars=true, 
    breaklines=true,
    breakatwhitespace=true,
    emph={},
    emphstyle=\color{red},
    basicstyle=\ttfamily,
    columns=fullflexible,
    commentstyle=\color{gray}\upshape,
    morestring=[b]",
    morecomment=[s]{<?}{?>},
    morecomment=[s][\color{forestgreen}]{<!--}{-->},
    keywordstyle=\color{orangered},
    stringstyle=\ttfamily\color{black}\normalfont,
    tagstyle=\color{darkblue}\bf,
    morekeywords={attribute,xmlns,version,type,release},
}

\begin{document}

\begin{lstlisting}[style=XML]
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<note attribute="main" xmlns="http://www.w3.org/1999/xhtml">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Dont forget me this weekend!</body>
</note>
\end{lstlisting}

\end{document}

答案1

实现此目的的一种方法是otherkeywords针对这些情况进行定义。

otherkeywords={attribute=, xmlns=}

在此处输入图片描述

代码:

\documentclass[a4paper, 12pt]{article}

\usepackage{listings, color}

\definecolor{forestgreen}{RGB}{34,139,34}
\definecolor{orangered}{RGB}{239,134,64}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{gray}{rgb}{0.4,0.4,0.4}

\lstdefinestyle{XML} {
    language=XML,
    extendedchars=true, 
    breaklines=true,
    breakatwhitespace=true,
    emph={},
    emphstyle=\color{red},
    basicstyle=\ttfamily,
    columns=fullflexible,
    commentstyle=\color{gray}\upshape,
    morestring=[b]",
    morecomment=[s]{<?}{?>},
    morecomment=[s][\color{forestgreen}]{<!--}{-->},
    keywordstyle=\color{orangered},
    stringstyle=\ttfamily\color{black}\normalfont,
    tagstyle=\color{darkblue}\bf,
    morekeywords={attribute,xmlns,version,type,release},
    otherkeywords={attribute=, xmlns=},
}

\begin{document}

\begin{lstlisting}[style=XML]
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<note attribute="main" xmlns="http://www.w3.org/1999/xhtml">
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Dont forget me this weekend!</body>
</note>
\end{lstlisting}

\end{document}

相关内容