问题:
使用列表为 CSS 中的伪类关键字添加特定颜色。例如,伪类:link
、:visited
等应使用不同的颜色。
最小工作示例(MWE):
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
% --------------------------------------------------------------
% Colors
% --------------------------------------------------------------
\definecolor{editorPurple}{cmyk}{0.75, 1, 0, 0}
\definecolor{editorBlack}{cmyk}{0, 0, 0, 1}
\definecolor{editorOrange}{cmyk}{0, 0.5, 1, 0}
\definecolor{editorDarkOrange}{cmyk}{0, 0.8, 0.9, 0}
\definecolor{editorBlue}{cmyk}{1, 0.35, 0, 0}
\definecolor{editorPink}{cmyk}{0, 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
% ----------------------------------------------------------------------
% CSS
% ----------------------------------------------------------------------
\lstdefinelanguage{CSS3}{
stringstyle=\colorcolonother{editorBlue}{editorPurple},
commentstyle=\color{editorGray},
keywordstyle=[2]\color{editorBlue},
identifierstyle=\color{editorPink},
%
sensitive=true,
% Line numbers
xleftmargin={14pt},
numbers=left,
stepnumber=1,
firstnumber=1,
numberfirstline=true,
numberstyle=\color{editorBlack},
frame=l,
% Keywords
keywords=[2]{%
% CSS properties
color
},%
otherkeywords={!important},
morecomment=[l][\color{darkgray}]{//},
morecomment=[s][\color{darkgray}]{/*}{*/},
alsoletter={!.,\#\*},
morestring=[s]{:}{;},
alsodigit={!-:},
escapeinside={`}{'}
}
% ----------------------------------------------------------------------
% HTML
% ----------------------------------------------------------------------
\lstdefinelanguage{HTML5}{
language=html,
sensitive=true,
tagstyle=\color{editorBlue},
markfirstintag=true,
morecomment=[s][\color{darkgray}]{<!-}{-->},
alsoletter={!-'},
keywords={}
}
% ----------------------------------------------------------------------
% Code style
% ----------------------------------------------------------------------
\lstset{%
% General design
inputencoding=utf8,
backgroundcolor=\color{white},
basicstyle=\ttfamily\upshape\lst@ifdisplaystyle\tiny\fi,
frame=single,
float,
belowskip=0pt,
escapeinside=`',
% line-numbers
xleftmargin={14pt},
numbers=left,
stepnumber=1,
firstnumber=1,
numberfirstline=true,
numberstyle=\color{black},
% Code design
identifierstyle=\color{editorOrange},
keywordstyle=\color{editorPink},
commentstyle=\color{editorGray},
stringstyle=\color{editorPurple},
% Code
language=HTML5,
alsolanguage=CSS3,
alsodigit={.:},
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
keepspaces=true,
extendedchars=true,
breaklines=false
}
\begin{document}
\begin{lstlisting}[language=CSS3,numbers=left,firstnumber=1]
/* Correct */
a {
color: #555555 !important;
}
/* Incorrect
color: should be blue
:link should be purple
*/
a:link {
color: #555555;
}
\end{lstlisting}
\end{document}
期望输出:
- 身份标识 (
a
) 应为粉色 - 伪类(
:link
) 应为紫色 - 属性 (
color:
) 应为蓝色 - 值 (
#555555
) 应为紫色 - 括号(
{
和}
)应为黑色