5

Problem:

Coloring different components of CSS using listings. I have managed to get HTML to work well, but CSS is not supported by listings and therefore I am wondering if there is a way to get colouring done.

Minimal Working Example (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{editorGray}{gray}{0}
\definecolor{editorPurple}{cmyk}{0.75, 1, 0, 0}
% ----------------------------------------------------------------------
%  CSS
% ----------------------------------------------------------------------
\lstdefinelanguage{CSS3}{
  sensitive=true,
  tagstyle=\color{editorBlue},
  markfirstintag=true,
  % Line numbers
  xleftmargin={14pt},
  numbers=left,
  stepnumber=1,
  firstnumber=1,
  numberfirstline=true,
  numberstyle=\color{black},
  frame=l,
  % Keywords
  keywords={color},  
  sensitive=true,
  morecomment=[l][\color{darkgray}]{//},
  morecomment=[s][\color{darkgray}]{/*}{*/},
  morestring=[b]',
  morestring=[b]",
  alsoletter={.:\#{};},
  alsodigit={-}
}
% ----------------------------------------------------------------------
%  HTML
% ----------------------------------------------------------------------
\lstdefinelanguage{HTML5}{
  language=html,
  sensitive=true,
  tagstyle=\color{editorBlue},
  markfirstintag=true,
  morecomment=[s][\color{darkgray}]{<!-}{-->},
  alsoletter={!-'},
  keywords={},
  % Line numbers
  xleftmargin={14pt},
  numbers=left,
  stepnumber=1,
  firstnumber=1,
  numberfirstline=true,
  numberstyle=\color{black},
  % Frame
  frame=l
}
% ----------------------------------------------------------------------
%  Code style
% ----------------------------------------------------------------------
\lstset{%
  % General design
  inputencoding=utf8,
  backgroundcolor=\color{white},
  basicstyle=\normalsize\ttfamily\upshape\lst@ifdisplaystyle\tiny\fi,
  frame=none,
  escapeinside=`',
  % Code design
  identifierstyle=\color{editorDarkOrange},
  keywordstyle=\color{editorPink},
  ndkeywordstyle=\color{editorPink},
  commentstyle=\color{editorGray},
  stringstyle=\color{editorPurple},
  % Languages
  language=HTML5,
  alsolanguage=CSS3,
  % Code
  alsodigit={.:},
  tabsize=2,
  showtabs=false,
  showspaces=false,
  showstringspaces=false,
  extendedchars=true,
  breaklines=false,
}

\begin{document}

\noindent CSS

\begin{lstlisting}[language=CSS3]
.p { color: #ffffff; }
\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, it works! -->
    <div class="test">Hello</div>
  </body>
</html>
\end{lstlisting}

\end{document}

Desired output:

To color different parts of CSS the same way as HTML. For instance, the .p or things before { should be in purple. { and } should be in blue, keywords like color should be in pink and things after : should be in dark orange.

Update 1:

It seems to be working well now with the exception that the : should also be coloured in blue. Is there a way to target specifically this?

Minimal Working Example (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{editorGray}{gray}{0}
\definecolor{editorPurple}{cmyk}{0.75, 1, 0, 0}
% ----------------------------------------------------------------------
%  CSS
% ----------------------------------------------------------------------
\newcommand{\CodeSymbol}[1]{\textcolor{editorPink}{#1}}

\lstdefinelanguage{CSS3}{
  sensitive=true,
  tagstyle=\color{editorBlue},
  markfirstintag=true,
  % Line numbers
  xleftmargin={14pt},
  numbers=left,
  stepnumber=1,
  firstnumber=1,
  numberfirstline=true,
  numberstyle=\color{black},
  frame=l,
  % Keywords
  ndkeywords={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,-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}]{/*}{*/},
  morestring=[s]{:}{;}, 
  alsoletter={.\#{}},
  alsodigit={-},
  literate={\{}{{\CodeSymbol{\{}}}1
           {\}}{{\CodeSymbol{\}}}}1
           {>}{{\CodeSymbol{>}}}1
}
% ----------------------------------------------------------------------
%  HTML
% ----------------------------------------------------------------------
\lstdefinelanguage{HTML5}{
  language=html,
  sensitive=true,
  tagstyle=\color{editorBlue},
  markfirstintag=true,
  morecomment=[s][\color{darkgray}]{<!-}{-->},
  alsoletter={!-'},
  keywords={},
  % Line numbers
  xleftmargin={14pt},
  numbers=left,
  stepnumber=1,
  firstnumber=1,
  numberfirstline=true,
  numberstyle=\color{black},
  % Frame
  frame=l,
}
% ----------------------------------------------------------------------
%  Code style
% ----------------------------------------------------------------------
\lstset{%
  % General design
  inputencoding=utf8,
  backgroundcolor=\color{white},
  basicstyle=\normalsize\ttfamily\upshape\lst@ifdisplaystyle\tiny\fi,
  frame=none,
  escapeinside=`',
  % Code design
  stringstyle=\color{editorPurple},
  commentstyle=\color{editorGray},
  keywordstyle=\color{editorPink},
  ndkeywordstyle=\color{editorBlue},
  identifierstyle=\color{editorDarkOrange},
  commentstyle=\color{editorGray},
  % Languages
  language=HTML5,
  alsolanguage=CSS3,
  % Code
  alsodigit={.:},
  tabsize=2,
  showtabs=false,
  showspaces=false,
  showstringspaces=false,
  extendedchars=true,
  breaklines=false,
}

\begin{document}

\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">Hello</div>
  </body>
</html>
\end{lstlisting}

\end{document}
  • You could use the keyval morekeywords={} to add a comma separated list of stuff which should be colored like your .p – Skillmon Mar 30 '17 at 6:57
  • @Skillmon Thanks, it works for keywords. But but I cannot seem to get the : to have the same color as the keywords. I am using morestring=[s]{:}{;},. – kexxcream Mar 30 '17 at 7:28
  • 1
    Solution to the above issue with coloring the : is available here: tex.stackexchange.com/questions/361465/… – kexxcream Apr 4 '17 at 3:13

Browse other questions tagged or ask your own question.