Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I've got a code of a HTML website that I want to show in my LaTeX document. The problem is, that the HTML code has a tag called <canvas> and the JavaScript code inside the HTML document has a variable called canvas too.

If I set canvas as a keyword for the language that is used to style the code, then both words (the canvas tag and the canvas variable name) get highlighted. But I only want the tag to be blue.

So I thought, I can specify a keyword called <canvas to prevent that the word without angle brackets is highlighted. But morekeywords={<canvas} does not work.

MWE:

\documentclass{scrreprt}
\usepackage{color}
\usepackage{upquote}
\usepackage{listings}
\lstdefinelanguage{JavaScript}{
  morekeywords={var, function},
  morecomment=[s]{/*}{*/},%
  morecomment=[l]//,%
  morestring=[b]",%
  morestring=[b]'%
}

\lstdefinelanguage{HTML5}{
    language=html,
    sensitive=false,        
    morekeywords={<canvas},
    otherkeywords={<, >, !},
    tag=[s]
}

\lstset{%
    % Basic design
    basicstyle={\small\ttfamily},
    % Code design   
    keywordstyle=\color{blue}\bfseries,
    stringstyle=\color{red},
    % Code
    language=HTML5,
    alsolanguage=JavaScript,
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true,
    alsodigit={.:;}
    % alsoletter={},
}
\begin{document}
    \begin{lstlisting}
<!DOCTYPE html>
<html>
  <head>
    <title>Canvas</title>
    <meta charset="UTF-8" />
  </head>
  <body>
    <canvas id="square" width="200" height="200"></canvas>
    <script>
            var canvas = document.createElement('canvas');
            canvas.width = 200;
            canvas.height = 200;

            var image = new Image();
            image.src = 'images/card.png';
            image.width = 114;
            image.height = 158;
            image.onload = window.setInterval(function() {
                rotation();
            }, 1000/60);
   </script>
  </body>
</html>
    \end{lstlisting}
\end{document}

Screenshot:

enter image description here

Can someone help me?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

It seems that "other keywords" are processed first. So < is recognized as a keyword and not <canvas. This would help:

\lstdefinelanguage{HTML5}{
    language=html,
    sensitive=false,
    alsoletter={<},
    morekeywords={<canvas},
    %otherkeywords={<, >, !},
    tag=[s]
}

... but then you'd have to manually define all the possible tag names (or rather, <tagname) as keywords ;-(

share|improve this answer
    
No problem! I will get a list of all defined HTML tags and then I will insert them. With your help I am pretty close to solve: tex.stackexchange.com/questions/99500/… - Thank you! –  Benny Neugebauer Feb 23 '13 at 20:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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