I work on a listings code style for HTML5. The style looks quite good but I have a little problem with the equals sign. I defined the equals sign as a keyword:

 ndkeywords={=, ... }


This will cause the equals sign to appear in green (which is what I want). However, in += the = is also green (but not the +).

Screenshot:

Can I define that the equals sign is only highlighted when there is a space before?

Here is the code example: https://www.writelatex.com/74567mmxwkw

• Could you add a minimal working example (MWE) that illustrates your problem. The linked code is quite much and contains a lot that isn’t even related to the question. – Qrrbrbirlbel Mar 3 '13 at 1:48

You have simply forgotten to declare + as a letter for html. Adding + to alsoletter as below

\lstdefinelanguage{HTML5}{
language=html,
sensitive=true,
alsoletter={<>=-+},
...


\documentclass{scrreprt}
\makeatletter
\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}
\definecolor{editorGray}{rgb}{0.95, 0.95, 0.95}
\definecolor{editorOcher}{rgb}{1, 0.5, 0} % #FF7F00 -> rgb(239, 169, 0)
\definecolor{editorGreen}{rgb}{0, 0.5, 0} % #007C00 -> rgb(0, 124, 0)
\usepackage{upquote}
\usepackage{listings}
% CSS
\lstdefinelanguage{CSS}{
keywords={color,background-image:,margin,padding,font,weight,display,position,top,left,right,bottom,list,style,border,size,white,space,min,width, transition:, transform:, transition-property, transition-duration, transition-timing-function},
sensitive=true,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]',
morestring=[b]",
alsoletter={:},
alsodigit={-}
}

% JavaScript
\lstdefinelanguage{JavaScript}{
morekeywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
morecomment=[s]{/*}{*/},
morecomment=[l]//,
morestring=[b]",
morestring=[b]'
}

\lstdefinelanguage{HTML5}{
language=html,
sensitive=true,
alsoletter={<>=-+},
morecomment=[s]{<!-}{-->},
tag=[s],
otherkeywords={
% General
>,
% Standard tags
<!DOCTYPE,
% body
</body, <body,
% Divs
</div, <div, </div>,
% Paragraphs
</p, <p, </p>,
% scripts
</script, <script,
% More tags...
<canvas, /canvas>, <svg, <rect, <animateTransform, </rect>, </svg>, <video, <source, <iframe, </iframe>, </video>, <image, </image>
},
ndkeywords={=,
% General
+=,
% HTML attributes
charset=, src=, id=, width=, height=, style=, type=, rel=, href=,
% SVG attributes
fill=, attributeName=, begin=, dur=, from=, to=, poster=, controls=, x=, y=, repeatCount=, xlink:href=,
% CSS properties
margin:, padding:, background-image:, border:, top:, left:, position:, width:, height:,
% CSS3 properties
transform:, -moz-transform:, -webkit-transform:,
animation:, -webkit-animation:,
transition:,  transition-duration:, transition-property:, transition-timing-function:,
},
}

\lstset{%
% General design
backgroundcolor=\color{editorGray},
basicstyle={\small\ttfamily},
frame=l,
% line-numbers
xleftmargin={0.75cm},
numbers=left,
stepnumber=1,
firstnumber=1,
numberfirstline=true,
% Code design
identifierstyle=\color{black},
keywordstyle=\color{blue}\bfseries,
ndkeywordstyle=\color{editorGreen}\bfseries,
stringstyle=\color{editorOcher}\ttfamily,
% Code
language=HTML5,
alsolanguage=JavaScript,
alsodigit={.:;},
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
extendedchars=true,
breaklines=true,
% German umlauts
literate=%
{Ö}{{\"O}}1
{Ä}{{\"A}}1
{Ü}{{\"U}}1
{ß}{{\ss}}1
{ü}{{\"u}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
}

\makeatother
\begin{document}
\begin{lstlisting}
<!DOCTYPE html>
<html>
<title>Listings Style Test</title>
<meta charset="UTF-8">
<style>
/* CSS Test */
* {
border: 0;
margin: 0;
}
</style>
<body>
<!-- Paragraphs are fine -->
<div id="box">
<p>
Hello World
</p>
<p>Hello World</p>
<p id="test">Hello World</p>
<p></p>
</div>
<div>Test</div>
<!-- HTML script is not consistent -->
<script src="js/benchmark.js"></script>
<script>
function createSquare(x, y) {
// This is a comment.
var square = document.createElement('div');
square.style.width = square.style.height = '50px';
square.style.backgroundColor = 'blue';

/*
* This is another comment.
*/
square.style.position = 'absolute';
square.style.left = x + 'px';
square.style.top = y + 'px';

var body = document.getElementsByTagName('body')[0];
body.appendChild(square);
};

// Please take a look at +=
// German umlaut test: Berührungspunkt ermitteln
var x = event.touches[0].pageX;
var y = event.touches[0].pageY;
var lookAtThis += 1;
});
</script>
</body>
</html>
\end{lstlisting}
\end{document}


Update Having > as general keyword means the final > in a comment gets the wrong colour, as pointed out by @kexxcream. A partial work around is to include > in each keyword as appropriate. The listings code for html contains something rather more sophisticated to deal with this.

• The second time that you saved me. Thank you! – Benny Neugebauer Mar 6 '13 at 22:55
• There is one issue with your code. It gives a blue color for the ">" sign at the end of a comment marked with <!-- --> while the rest of the comment is grey. – kexxcream Jun 26 '15 at 5:35
• @kexxcream Thanks for spotting that. It is a different question to the one asked here, and is due to other parts of the code from the link solution. However, I have now fixed it. – Andrew Swann Jun 26 '15 at 7:15
• @AndrewSwann Unfortunately, now the end ">" of each tag has also changed color to black. Take for instance the tag <html> - it starts with a blue < and ends with a black >. – kexxcream Jun 26 '15 at 7:30
• @kexxcream OK - I have rolled back my edit. I think you should ask a separate question; this was not the issue I was answering in my solution above. – Andrew Swann Jun 26 '15 at 7:33