1

Problem:

Adding specific color to pseudo-class keywords in CSS using listings. For example, the pseudo-classes :link, :visited, etc should be colored differently.

Minimal Working Example (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}

Desired output:

  • Identifiers (a) should be in pink
  • Pseudo-classes (:link) should be in purple
  • Properties (color:) should be in blue
  • Values (#555555) should be in purple
  • Brackets ({ and }) should be in black

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.