4

for the use of CSS code I've defined the following style:

\lstdefinelanguage{CSS}{
    sensitive=true,
    alsodigit={-},
    keywords={transform, transition-property, transition-duration, transition-timing-functio},              
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]',
    morestring=[b]"
}

In CSS3 transform can be a keyword and a property. Therefore, both variations are shown in blue in my document.

Screenshot:

s

Keywords always end with a colon. Can this rule be used to prevent the properties being marked as keyword?

MWE:

\documentclass{article}

\usepackage{listings}
\usepackage{color}

\definecolor{lightgray}{rgb}{0.95, 0.95, 0.95}
\definecolor{darkgray}{rgb}{0.4, 0.4, 0.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}

\lstdefinelanguage{CSS}{
    keywords={color,background-image,margin,padding,font,weight,display,position,top,left,right,bottom,list,style,border,size,white,space,min,width, transform, transition, transition-property, transition-duration, transition-timing-function},
    alsodigit={-},
    sensitive=true,
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]',
    morestring=[b]"
}

\lstset{%
    % General design
    backgroundcolor=\color{lightgray},
    basicstyle={\small\ttfamily},   
    frame=l,
    % Code design
    identifierstyle=\color{black},
    keywordstyle=\color{blue}\bfseries,
    ndkeywordstyle=\color{greenCode}\bfseries,
    stringstyle=\color{ocherCode}\ttfamily,
    commentstyle=\color{darkgray}\ttfamily,
    % Code
    language={CSS},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true,
    % line-numbers
    xleftmargin={0.75cm},
    numbers=left,
    stepnumber=1,
    firstnumber=1,
    numberfirstline=true,
}

\begin{document}
  \begin{lstlisting}
        #box {
            transition-property: transform;
            transition-duration: 2s;
            transition-timing-function: linear;
      }
  \end{lstlisting}
\end{document}
4

My current solution is to put a colon at the end of all the keywords and to use alsoletter={-} and alsodigit={:}.

Example:

\lstdefinelanguage{CSS}{
    keywords={transform:, transition-property:, transition-duration:, transition-timing-function:},
    sensitive=true,
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]',
    morestring=[b]",        
    alsoletter={-},
    alsodigit={:}
}

It is very annoying but it works. It would be better if one could define everything as a keyword that is between a space and a colon.

0

Update

As suggested by Qrrbrbirlbel, the answer to your question is yes. Using moredelim=[s][]{:}{;} should do the trick, and there is no longer any need to change anything in your code. This way, anything in between and including : and ; are typeset in black.

Original Post

You can use moredelim=[is][\bfseries]{/@}{@/} in \lstdefinelanguage or \lstset. Here, the delimeters /@ and @/ are used. (You can change them with a character or combination of characters not used in the language.) They will not appear in the output but typesets any character in between them, in this case, as boldface.

The Code

Here is the full code with the parts I inserted commented.

\documentclass{article}

\usepackage{listings}
\usepackage{color}

\definecolor{lightgray}{rgb}{0.95, 0.95, 0.95}
\definecolor{darkgray}{rgb}{0.4, 0.4, 0.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}

\lstdefinelanguage{CSS}{
    keywords={color,background-image,margin,padding,font,weight,display,position,top,left,right,bottom,list,style,border,size,white,space,min,width, transform, transition, transition-property, transition-duration, transition-timing-function},
    alsodigit={-},
    sensitive=true,
    morecomment=[l]{//},
    morecomment=[s]{/*}{*/},
    morestring=[b]',
    morestring=[b]",
    moredelim=[is][\bfseries]{/@}{@/}, % Typesets characters between /@ and @/ delimeters in boldface.
}

\lstset{%
    % General design
    backgroundcolor=\color{lightgray},
    basicstyle={\small\ttfamily},   
    frame=l,
    % Code design
    identifierstyle=\color{black},
    keywordstyle=\color{blue}\bfseries,
    ndkeywordstyle=\color{greenCode}\bfseries,
    stringstyle=\color{ocherCode}\ttfamily,
    commentstyle=\color{darkgray}\ttfamily,
    % Code
    language={CSS},
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true,
    % line-numbers
    xleftmargin={0.75cm},
    numbers=left,
    stepnumber=1,
    firstnumber=1,
    numberfirstline=true,
}

% See how the delimeters are used below.
\begin{document}
  \begin{lstlisting}
        #box {
            transition-property: /@transform@/;
            transition-duration: 2s;
            transition-timing-function: linear;
      }
  \end{lstlisting}
\end{document}

The Output

enter image description here

  • I’d go so far and use moredelim=[s][]{:}{;}: No change of input. Text between (and including) : and ; always caught and not in blue. – Qrrbrbirlbel Feb 13 '13 at 3:56
  • @Qrrbrbirlbel Yes, you are right. I will update my answer accordingly. Thank you. – hpesoj626 Feb 13 '13 at 3:58
  • Hello, thank you for this great help. Unfortunately mmoredelim=[s][]{:}{;} has some side effects. The word "transform" can be a property and a keyword. The keyword is "transform:" and the property is "transform". If I declare "transform" in the keyword list and if I use "transform: translate (100px , 100 px);" in my code, then transform is not blue anymore because of moredelim=[s][]{:}{;} :-( – Benny Neugebauer Feb 15 '13 at 10:51
  • 1
    I thought you wanted that part after the colon black. Well, it seems you already have solved your problem. – hpesoj626 Feb 16 '13 at 9:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.