5

I defined keywords for the JavaScript programming language and HTML.

I have read that it is possible to use the keywords of both languages in a \listings code snippet. But in my example, the JavaScript keywords are not shown in blue.

enter image description here

Here is my code example. Is my use of alsolanguage incorrect?

Code:

\documentclass{scrreprt}
\usepackage{color}
\usepackage{upquote}
\usepackage{listings}
\makeatletter
\lstdefinelanguage{JavaScript}{
  keywords={var, function}
}

\lstdefinelanguage{HTML5}{
    language=HTML,
    sensitive=true,
    tagstyle=\color{blue}\bfseries,
    usekeywordsintag=true
}

\lstset{%
    % Basic design
    basicstyle={\small\ttfamily},
    % Code design   
    keywordstyle=\color{blue}\bfseries,
    % Code
    language=HTML5,
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true
}
\makeatother
\begin{document}
    \begin{lstlisting}[language=HTML5,alsolanguage=JavaScript]
<!DOCTYPE html>
<html>
  <head>
    <title>Canvas-Rotation</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}
3

The JavaScript language is loaded, but because of how the html language is defined in listings the content inside <script> is formatted as text. It is controlled by the key tag. I don't know exactly what it does, but by making HTML5 inherit everything from HTML and overwriting the tag it seems to work for this simple example.

When using more than one language just must use the key morekeywords for JavaScript or else it will overwrite the list of keywords for HTML. Furthermore alsolanguages doesn't switch between the two languages but simply unifies the two languages. So when I define canvas as a keyword for HTML5, the variable canvas inside the JavaScript is also still formated as keyword.

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

\lstdefinelanguage{HTML5}[]{HTML}{
    sensitive=false,
    morekeywords={canvas},
    tag=[s]
}

\lstset{%
    % Basic design
    basicstyle={\small\ttfamily},
    % Code design   
    keywordstyle=\color{blue}\bfseries,
    stringstyle=\color{red},
    % Code
    language=HTML5,
    tabsize=2,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    extendedchars=true,
    breaklines=true
}
\begin{document}
    \begin{lstlisting}[language=HTML5,alsolanguage=JavaScript]
<!DOCTYPE html>
<html>
  <head>
    <title>Canvas-Rotation</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}

enter image description here

  • Thank you for your input! This sample was made to help with this one: tex.stackexchange.com/questions/99500/… - Your code gave me light into the darkness. Too bad that the JavaScript code is formatted too. I'm still looking for a solution to combine CSS, JavaScript and HTML code.... :/ – Benny Neugebauer Feb 23 '13 at 18:31
  • Is it maybe possible to specify <canvas for morekeywords? Because then the canvas in JavaScript won't be highlighted anymore. I tried it with morekeywords={<canvas} but it didn't work. :( – Benny Neugebauer Feb 23 '13 at 18:39

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.