You have simply forgotten to declare +
as a letter for html
. Adding +
to alsoletter
as below
\lstdefinelanguage{HTML5}{
language=html,
sensitive=true,
alsoletter={<>=-+},
...
gives your desired colouring
\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,
</html, <html, <head, <title, </title, <style, </style, <link, </head, <meta, />,
% 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,
commentstyle=\color{darkgray}\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>
<head>
<title>Listings Style Test</title>
<meta charset="UTF-8">
<style>
/* CSS Test */
* {
padding: 0;
border: 0;
margin: 0;
}
</style>
<link rel="stylesheet" href="css/style.css" />
</head>
<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 +=
window.addEventListener('mousedown', function(event) {
// 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.
Listing is powerful tool , more documentation can be found here:The Listings Package.
Instead of setting global settings you should set styles for each individual listings
Flowing code should give you good picture of how to customise listings.
\documentclass{article}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{xcolor}
\lstdefinestyle{basic}{
basicstyle=\footnotesize\ttfamily,
numbers=left,
numberstyle=\tiny\color{gray}\ttfamily,
numbersep=5pt,
backgroundcolor=\color{white},
showspaces=false,
showstringspaces=false,
showtabs=false,
frame=single,
rulecolor=\color{black},
captionpos=b,
keywordstyle=\color{blue}\bf,
commentstyle=\color{gray},
stringstyle=\color{green},
keywordstyle={[2]\color{red}\bf},
}
\lstdefinelanguage{custom}
{
morekeywords={public, void},
sensitive=false,
morecomment=[l]{//},
morecomment=[s]{/*}{*/},
morestring=[b]",
}
\begin{document}
\lstinputlisting[ style=basic, language=Python]{main.java}
\lstinputlisting[ style=basic, language=Java]{main.java}
\lstinputlisting[ style=basic, language=custom]{main.java}
\end{document}
Three version of customised listings presented in the code above
they all use same colours for identity of: strings, comments, keywords.
also I showed how to create new language for your need. you can use existed languages for your foundation and add more keywords to it.
[UPDATE]
Here is set of different styles , I personally prefer this one.
\documentclass{article}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{color}
\usepackage{accsupp}
\definecolor{dkblue}{rgb}{0,0,0.5}
\definecolor{comment}{rgb}{1,0,0}
\definecolor{mauve}{rgb}{.627,.126,.941}
\definecolor{purple}{rgb}{0.5, 0, 0.545098}
\lstdefinestyle{java}{
belowcaptionskip=1\baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=\parindent,
language=Java,
tabsize=4,
tabsize=4,
numbers=left,
showstringspaces=false,
numberstyle=\tiny\noncopynumber,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\bfseries\color[rgb]{.133,.545,.133},
commentstyle=\itshape\color{blue},
stringstyle=\color{mauve},
directivestyle=\bfseries\color{purple},
frame=single],
resetmargins=true,
}
\lstdefinestyle{mathlab}{
belowcaptionskip=1\baselineskip,
breakatwhitespace=false, % sets if automatic breaks should only happen at whitespace
breaklines=true, % sets automatic line breaking
xleftmargin=\parindent,
language=C,
morekeywords={end, elseif},
comment=[l]\%, % comments
tabsize=4,
numberstyle=\tiny\noncopynumber,
showstringspaces=false,
basicstyle=\footnotesize\ttfamily,
keywordstyle=\color[rgb]{0,0,1},
commentstyle=\itshape\color[rgb]{.133,.545,.133},
stringstyle=\color{mauve},
directivestyle=\bfseries\color{purple},
framexleftmargin=3mm,
frame=single,
}
\newcommand{\noncopynumber}[1]{%
\BeginAccSupp{method=escape,ActualText={}}%
#1%
\EndAccSupp{}%
}
\begin{document}
\lstinputlisting[caption=Matlab style main code: "main.java", style=mathlab]{main.java}
\lstinputlisting[caption=Matlab style main code: "main.java", style=java]{main.java}
\end{document}
Best Answer
Well, most of your questions have already been answered either in the question itself or in the comments, so here's just a recap to satisfy the site's mores:
microtype.dtx
employs is to use the "non-breaking space" in lieu of a normal space to makelistings
believe that it's a letter, while it will be output as a space;latin1
).utf8
input encoding, which – though it meets the above requirement – is not compatible with thelistings
package. I don't see a way to fix this incompatibility other than inlistings
itself.AFAICT, you only have two options:
utf8
encoding – which in your case appears to be possible considering the fact that you say the only reason why you use it in the first place is that it is recommended everywhere;listingsutf8
package (which understandsutf8
input in included files) and store all your code listings in separate files – maybe a bit cumbersome depending on the number of listings you have.