使用宏改变列表中字符串分隔符的颜色

使用宏改变列表中字符串分隔符的颜色

问题:

我已经设法使用包为 CSS 获得颜色突出显示listings,但有一个小细节尚未解决。字符串分隔符是:;。我希望分隔符:为蓝色,而不是字符串的颜色。

问题:

:有没有办法使用宏覆盖列表中的颜色?所有:颜色都应该是蓝色。

最小工作示例(MWE):

\documentclass{scrreprt}
\usepackage[dvipsnames,cmyk]{xcolor}
\usepackage{listings}
% --------------------------------------------------------------
% Colors
% --------------------------------------------------------------
\definecolor{editorGray}{cmyk}{0, 0, 0, 0.2}
\definecolor{editorBlue}{cmyk}{1, 0.35, 0, 0}
\definecolor{editorPink}{cmyk}{0, 1, 0, 0}
\definecolor{editorDarkOrange}{cmyk}{0, 0.8, 0.9, 0}
\definecolor{editorPurple}{cmyk}{0.75, 1, 0, 0}
% ----------------------------------------------------------------------
%  CSS
% ----------------------------------------------------------------------
\newcommand{\CodeSymbol}[1]{\textcolor{editorPink}{#1}}

\lstdefinestyle{css}
{
  stringstyle=\color{editorPurple},
  commentstyle=\color{editorGray},
  keywordstyle=\color{editorPink},
  ndkeywordstyle=\color{editorBlue},
  identifierstyle=\color{editorDarkOrange},
  commentstyle=\color{editorGray},
  sensitive=true,
  % Line numbers
  xleftmargin={14pt},
  numbers=left,
  stepnumber=1,
  firstnumber=1,
  numberfirstline=true,
  numberstyle=\color{black},
  frame=l,
  % Keywords
  ndkeywords={color, text-decoration},
  morecomment=[l][\color{darkgray}]{//},
  morecomment=[s][\color{darkgray}]{/*}{*/},
  alsoletter={.\#},
  morestring=[s]{:}{;},
  alsodigit={-;:},
  literate=*{\{}{{\CodeSymbol{\{}}}1
           {\}}{{\CodeSymbol{\}}}}1
           {>}{{\CodeSymbol{>}}}1
}

\lstdefinelanguage{CSS3}{style=css}
\lstMakeShortInline[language=CSS3]^

% ----------------------------------------------------------------------
%  Code style
% ----------------------------------------------------------------------
\lstset{%
  % General design
  inputencoding=utf8,
  backgroundcolor=\color{white},
  basicstyle=\normalsize\ttfamily\upshape\lst@ifdisplaystyle\tiny\fi,
  frame=none,
  escapeinside=`',
  % Languages
  language=CSS3,
  % Code
  tabsize=2,
  showtabs=false,
  showspaces=false,
  showstringspaces=false,
  extendedchars=true,
  breaklines=false
}

\begin{document}

\noindent Inline CSS

\noindent ^.p { color: #ffffff; }^

\noindent CSS

\begin{lstlisting}[language=CSS3]
// Classes
.p { color: #ffffff; }
header > .nav {
  color: #ffffff;
  text-decoration: none;
}
/* Ids */
#test {
  color: #ffffff;
  text-decoration: none;
}
\end{lstlisting}

\end{document}

期望输出:

将颜色:从紫色改为蓝色。

更新 1:

感谢 @egreg,我找到了一个用于切换:为蓝色的正则表达式,但我似乎无法将新命令与合并\lstMakeShortInline[language=CSS3]^。有什么想法吗?

\ExplSyntaxOn
\NewDocumentCommand{\colorswitch}{ O{editorBlue} m }
 {
  \css_colorswitch:nn { #1 } { #2 }
 }

\tl_new:N \l__css_colorswitch_input_tl
\cs_new_protected:Npn \css_colorswitch:nn #1 #2
 {
  % store the string in a variable for usage with \regex_replace_all:nnN
  \tl_set:Nn \l__css_colorswitch_input_tl { #2 }
  \regex_replace_all:nnN
   { ([:]) } % search a colon
   { \c{textcolor}\cB\{#1\cE\}\cB\{\1\cE\} } % replace the match with \textcolor{#1}{<match>}
   \l__css_colorswitch_input_tl
  \tl_use:N \l__css_colorswitch_input_tl
 }
\ExplSyntaxOff

答案1

更新2(解决方案):

感谢令人惊叹的 Sašo (https://tex.stackexchange.com/users/16819/sašo-Živanović),现在有一个针对此问题的有效解决方案。此代码允许将 HTML 和 CSS 标记为内联和代码块。请参阅下面的最小工作示例。

Sašo 的解决方案是添加以下内容来识别:

\makeatletter
\def\colorcolonother#1#2{%
  \expandafter\ifx\the\lst@token:%
    \color{#1}%
  \else
    \color{#2}%
  \fi
}
\makeatother

然后在CSS的定义样式中添加:

\lstdefinestyle{css}
{
  stringstyle=\colorcolonother{editorBlue}{editorPurple},

最小工作示例(MWE):

\documentclass{scrreprt}
\usepackage[dvipsnames,cmyk]{xcolor}
\usepackage{listings}
% --------------------------------------------------------------
% Color definitions
% --------------------------------------------------------------
\definecolor{editorGray}{cmyk}{0, 0, 0, 0.2}
\definecolor{editorBlue}{cmyk}{1, 0.35, 0, 0}
\definecolor{editorPink}{cmyk}{0, 1, 0, 0}
\definecolor{editorDarkOrange}{cmyk}{0, 0.8, 0.9, 0}
\definecolor{editorPurple}{cmyk}{0.75, 1, 0, 0}
% ----------------------------------------------------------------------
%  CSS
% ----------------------------------------------------------------------
% Targets the colon in inline CSS code
\makeatletter
\def\colorcolonother#1#2{%
  \expandafter\ifx\the\lst@token:%
    \color{#1}%
  \else
    \color{#2}%
  \fi
}
\makeatother

% Changes symbol colors for { and } in CSS
\newcommand{\CodeSymbol}[1]{\textcolor{editorPink}{#1}}

\lstdefinestyle{css}
{
  stringstyle=\colorcolonother{editorBlue}{editorPurple},
  commentstyle=\color{editorGray},
  keywordstyle=\color{editorBlue},
  identifierstyle=\color{editorDarkOrange},
  commentstyle=\color{editorGray},
  %
  sensitive=true,
  % Line numbers
  xleftmargin={14pt},
  numbers=left,
  stepnumber=1,
  firstnumber=1,
  numberfirstline=true,
  numberstyle=\color{black},
  frame=l,
  % Keywords
  keywords={%
% CSS properties
  accelerator,azimuth,background,background-attachment,
  background-color,background-image,background-position,
  background-position-x,background-position-y,background-repeat,
  behavior,border,border-bottom,border-bottom-color,
  border-bottom-style,border-bottom-width,border-collapse,
  border-color,border-left,border-left-color,border-left-style,
  border-left-width,border-right,border-right-color,
  border-right-style,border-right-width,border-spacing,
  border-style,border-top,border-top-color,border-top-style,
  border-top-width,border-width,bottom,caption-side,clear,
  clip,color,content,counter-increment,counter-reset,cue,
  cue-after,cue-before,cursor,direction,display,elevation,
  empty-cells,filter,float,font,font-family,font-size,
  font-size-adjust,font-stretch,font-style,font-variant,
  font-weight,height,ime-mode,include-source,
  layer-background-color,layer-background-image,layout-flow,
  layout-grid,layout-grid-char,layout-grid-char-spacing,
  layout-grid-line,layout-grid-mode,layout-grid-type,left,
  letter-spacing,line-break,line-height,list-style,
  list-style-image,list-style-position,list-style-type,margin,
  margin-bottom,margin-left,margin-right,margin-top,
  marker-offset,marks,max-height,max-width,min-height,
  min-width,transition-duration,transition-property,
  transition-timing-function,transform,
  -moz-transform,-moz-binding,-moz-border-radius,
  -moz-border-radius-topleft,-moz-border-radius-topright,
  -moz-border-radius-bottomright,-moz-border-radius-bottomleft,
  -moz-border-top-colors,-moz-border-right-colors,
  -moz-border-bottom-colors,-moz-border-left-colors,-moz-opacity,
  -moz-outline,-moz-outline-color,-moz-outline-style,
  -moz-outline-width,-moz-user-focus,-moz-user-input,
  -moz-user-modify,-moz-user-select,orphans,outline,
  outline-color,outline-style,outline-width,overflow,
  overflow-X,overflow-Y,padding,padding-bottom,padding-left,
  padding-right,padding-top,page,page-break-after,
  page-break-before,page-break-inside,pause,pause-after,
  pause-before,pitch,pitch-range,play-during,position,quotes,
  -replace,richness,right,ruby-align,ruby-overhang,
  ruby-position,-set-link-source,size,speak,speak-header,
  speak-numeral,speak-punctuation,speech-rate,stress,
  scrollbar-arrow-color,scrollbar-base-color,
  scrollbar-dark-shadow-color,scrollbar-face-color,
  scrollbar-highlight-color,scrollbar-shadow-color,
  scrollbar-3d-light-color,scrollbar-track-color,table-layout,
  text-align,text-align-last,text-decoration,text-indent,
  text-justify,text-overflow,text-shadow,text-transform,
  text-autospace,text-kashida-space,text-underline-position,top,
  unicode-bidi,-use-link-source,vertical-align,visibility,
  voice-family,volume,white-space,widows,width,word-break,
  word-spacing,word-wrap,writing-mode,z-index,zoom
  },%
  morecomment=[l][\color{darkgray}]{//},
  morecomment=[s][\color{darkgray}]{/*}{*/},
  alsoletter={.\#},
  morestring=[s]{:}{;},
  alsodigit={-;:},
  literate=*{\{}{{\CodeSymbol{\{}}}1
            {\}}{{\CodeSymbol{\}}}}1
}
% ----------------------------------------------------------------------
%  HTML
% ----------------------------------------------------------------------
\lstdefinestyle{html}
{
  language=html,
  sensitive=true,
  stringstyle=\color{editorPurple},
  commentstyle=\color{editorGray},
  keywordstyle=\color{editorPink},
  ndkeywordstyle=\color{editorBlue},
  identifierstyle=\color{editorDarkOrange},
  commentstyle=\color{editorGray},
  tagstyle=\color{editorBlue},
  %
  markfirstintag=true,
  morecomment=[s][\color{darkgray}]{<!-}{-->},
  alsoletter={!-'},
  alsodigit={.:},
  keywords={},
  % Line numbers
  xleftmargin={14pt},
  numbers=left,
  stepnumber=1,
  firstnumber=1,
  numberfirstline=true,
  numberstyle=\color{black},
  % Frame
  frame=l,
  % German umlauts
  literate=
  {á}{{\'a}}1 {é}{{\'e}}1 {í}{{\'i}}1 {ó}{{\'o}}1 {ú}{{\'u}}1
  {Á}{{\'A}}1 {É}{{\'E}}1 {Í}{{\'I}}1 {Ó}{{\'O}}1 {Ú}{{\'U}}1
  {à}{{\`a}}1 {è}{{\`e}}1 {ì}{{\`i}}1 {ò}{{\`o}}1 {ù}{{\`u}}1
  {À}{{\`A}}1 {È}{{\'E}}1 {Ì}{{\`I}}1 {Ò}{{\`O}}1 {Ù}{{\`U}}1
  {ä}{{\"a}}1 {ë}{{\"e}}1 {ï}{{\"i}}1 {ö}{{\"o}}1 {ü}{{\"u}}1
  {Ä}{{\"A}}1 {Ë}{{\"E}}1 {Ï}{{\"I}}1 {Ö}{{\"O}}1 {Ü}{{\"U}}1
  {â}{{\^a}}1 {ê}{{\^e}}1 {î}{{\^i}}1 {ô}{{\^o}}1 {û}{{\^u}}1
  {Â}{{\^A}}1 {Ê}{{\^E}}1 {Î}{{\^I}}1 {Ô}{{\^O}}1 {Û}{{\^U}}1
  {œ}{{\oe}}1 {Œ}{{\OE}}1 {æ}{{\ae}}1 {Æ}{{\AE}}1 {ß}{{\ss}}1
  {ç}{{\c c}}1 {Ç}{{\c C}}1 {ø}{{\o}}1 {å}{{\r a}}1 {Å}{{\r A}}1
  {€}{{\EUR}}1 {£}{{\pounds}}1 {\\-}{}{0\discretionary{-}{}{}}
}
% ----------------------------------------------------------------------
%  Language and inline definitions for listings
% ----------------------------------------------------------------------
\lstdefinelanguage{HTML5}{style=html}
\lstdefinelanguage{CSS3}{style=css}
\lstMakeShortInline[language=CSS3]^
\lstMakeShortInline[language=HTML5]|
% ----------------------------------------------------------------------
%  Code style
% ----------------------------------------------------------------------
\lstset{%
  % General design
  inputencoding=utf8,
  backgroundcolor=\color{white},
  basicstyle=\normalsize\ttfamily\upshape\lst@ifdisplaystyle\tiny\fi,
  frame=none,
  escapeinside=`',
  % Languages
  language=HTML5,
  alsolanguage=CSS3,
  % Code
  tabsize=2,
  showtabs=false,
  showspaces=false,
  showstringspaces=false,
  extendedchars=true,
  breaklines=false,
}

\begin{document}

\noindent Inline CSS

\noindent ^.p { color: #ffffff; }^

\noindent Inline HTML

\noindent |<div class="test">This works well. Another.</div>|

\noindent CSS

\begin{lstlisting}[language=CSS3]
// Classes
.p { color: #ffffff; }
header > .nav {
  color: #ffffff;
  text-decoration: none;
}
/* Ids */
#test {
  color: #ffffff;
  text-decoration: none;
}
\end{lstlisting}

\noindent HTML

\begin{lstlisting}[language=HTML5]
<!DOCTYPE html>
<html lang="sv">
  <head>
    <title>Comments in HTML</title>
    <meta charset="utf-8">
  </head>
  <body>
    <!-- This is a comment -->
    <div class="test">This works.</div>
  </body>
</html>
\end{lstlisting}

\end{document}

相关内容