列表中的分隔符颜色与内容不同

列表中的分隔符颜色与内容不同

我正在尝试在 XML 列表中实现语法突出显示。作为这个答案我曾尝试将分隔符本身变灰,但尝试了几次都没有成功:

\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{listings}
\usepackage{xcolor}

% colours
\colorlet{cdel}{blue}   % delimiters
\colorlet{ckey}{green}  % keywords
\colorlet{ccom}{gray}   % comments
\colorlet{cstr}{brown}  % strings
\colorlet{cide}{orange} % identifiers
\colorlet{ctxt}{black}  % text

% sets a function for moredelim to try to add
%   colored delimiters to the start and end
%      see: https://tex.stackexchange.com/a/147848/1232
\def\beginlstdelim#1#2#3#4%
{%
    \def\endlstdelim{#2\egroup}%
    \color{#3}#1\bgroup\color{#4}\aftergroup\endlstdelim%
}

\lstdefinestyle{lst}{
    frame={single},
    basewidth=0.5em,    
    basicstyle={\ttfamily\footnotesize},   
    identifierstyle=\color{cide},
    keywordstyle=\color{ckey},
    stringstyle=\color{cstr},
    commentstyle=\color{ccom},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    columns=fixed,
    basewidth=0.5em
}

\lstdefinelanguage{xml}{ 
    keywords={xml},
    %literate={<}{{{\color{cdel}<}}}{1} {>}{{{\color{cdel}>}}}{1} {:}{{{\color{cdel}:}}}{1} {/}{{{\color{cdel}/}}}{1} {?}{{{\color{cdel}?}}}{1} {=}{{{\color{cdel}=}}}{1},  
       %^^ attempt 1: delimiters don't work
    %moredelim=**[is][\beginlstdelim{>}{<}{cdel}{ctxt}]{>}{<}, 
       %^^ attempt 2: messes with formatting    
    morestring=[s][\color{ctxt}]{>}{<},
    morestring=[b]",
    morecomment=[s]{!--}{--},
    morecomment=[s]{?}{?}
}

\begin{document}

\begin{lstlisting}[style=lst,language=xml]
<?xml version="1.0" encoding="UTF-8"?>
<mwe>
  <num>1</num>  <!-- the number is black -->
  <string xml:lang="en">Hello there</string>
  <comment>Why is this not black?</comment>
</mwe>
\end{lstlisting}
\end{document}

上面的代码给出:

在此处输入图片描述

(不用担心,这些不是我最终会用到的颜色,而只是用来区分不同的部分。)

我想要的是将分隔符(例如,,,,等等)变灰。<代码还包括我所做的几次(失败的)尝试。>/

第一次尝试是使用literate在分隔符后附加颜色,但代码的其他部分无法找到这些分隔符。第二次尝试是尝试适应这个答案使用自定义函数 onmoredelim=**[is]来附加和添加彩色分隔符,但这会严重扰乱格式,而且会遗漏开头<字符。我知道我可以通过设置“basicstyle”灰色来将分隔符变成灰色,但这样文本也会变成灰色。


关于如何将分隔符与其内容分开格式化,您有什么想法吗?

答案1

如果你想每一个 <为了>获得相同的颜色,您可以使用低级格式化:

\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{listings}
\usepackage{xcolor}

% colours
\colorlet{cdel}{blue}   % delimiters
\colorlet{ckey}{green}  % keywords
\colorlet{ccom}{gray}   % comments
\colorlet{cstr}{brown}  % strings
\colorlet{cide}{orange} % identifiers
\colorlet{ctxt}{black}  % text

% sets a function for moredelim to try to add
%   colored delimiters to the start and end
%      see: https://tex.stackexchange.com/a/147848/1232
\makeatletter
\lst@CCPutMacro
    \lst@ProcessOther {"3C}{\lst@ttfamily{\textcolor{blue}{<}}{\textcolor{blue}{<}}}
     \lst@ProcessOther
     {"3E}{\lst@ttfamily{\textcolor{blue}{>}}{\textcolor{blue}{>}}}
    \@empty\z@\@empty
\makeatother

\lstdefinestyle{lst}{
    frame={single},
    basewidth=0.5em,
    basicstyle={\ttfamily\footnotesize},
    identifierstyle=\color{cide},
    keywordstyle=\color{ckey},
    stringstyle=\color{cstr},
    commentstyle=\color{ccom},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    columns=fixed,
    basewidth=0.5em
}

\lstdefinelanguage{xml}{
    keywords={xml},
    morestring=[s][\color{ctxt}]{>}{<},
    morestring=[b]",
    morecomment=[s]{!--}{--},
    morecomment=[s]{?}{?}
}

\begin{document}

\begin{lstlisting}[style=lst,language=xml]
<?xml version="1.0" encoding="UTF-8"?>
<mwe>
  <num>1</num>  <!-- the number is black -->
  <string xml:lang="en">Hello there</string>
  <comment>Why is this not black?</comment>
</mwe>
\end{lstlisting}
\end{document}

在此处输入图片描述

相关内容