54

I am afraid that listing doesn't support javascript.

\begin{lstlisting}[language=javascript]
...
\end{lstlisting}

What am I doing wrong?

  • 2
    there is no language support for javaSCRIPT –  Jan 06 '13 at 09:35
  • 4
    thanks. but it's disappointing, that it doesn't, while it supports even php. – user1592714 Jan 06 '13 at 09:42
  • 1
    I can highly recommend the minted package, but it does require Pygments and its pygmentize command. Also, to be able to call that command, your LaTeX will require the --shell-escape option. – mknaf Feb 03 '17 at 15:20
  • This is an attempt to create a javaScript listing for ES6, Node.js, Express.js, Jasmine etc. into: https://github.com/xgirma/latex-javaScript – user5249270 May 21 '17 at 11:13

5 Answers5

69

Here is Fran's example, based on the cited (now defunct) source:

% Taken from Lena Herrmann at 
% http://lenaherrmann.net/2010/05/20/javascript-syntax-highlighting-in-the-latex-listings-package
\documentclass{article}
\usepackage{listings}
\usepackage{color}
\definecolor{lightgray}{rgb}{.9,.9,.9}
\definecolor{darkgray}{rgb}{.4,.4,.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}

\lstdefinelanguage{JavaScript}{
  keywords={typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break},
  keywordstyle=\color{blue}\bfseries,
  ndkeywords={class, export, boolean, throw, implements, import, this},
  ndkeywordstyle=\color{darkgray}\bfseries,
  identifierstyle=\color{black},
  sensitive=false,
  comment=[l]{//},
  morecomment=[s]{/*}{*/},
  commentstyle=\color{purple}\ttfamily,
  stringstyle=\color{red}\ttfamily,
  morestring=[b]',
  morestring=[b]"
}

\lstset{
   language=JavaScript,
   backgroundcolor=\color{lightgray},
   extendedchars=true,
   basicstyle=\footnotesize\ttfamily,
   showstringspaces=false,
   showspaces=false,
   numbers=left,
   numberstyle=\footnotesize,
   numbersep=9pt,
   tabsize=2,
   breaklines=true,
   showtabs=false,
   captionpos=b
}



\begin{document}

\medskip
\begin{lstlisting}[caption=My Javascript Example]
Name.prototype = {
  methodName: function(params){
    var doubleQuoteString = "some text";
    var singleQuoteString = 'some more text';
    // this is a comment
    if(this.confirmed != null && typeof(this.confirmed) == Boolean && this.confirmed == true){
      document.createElement('h3');
      $('#system').append("This looks great");
      return false;
    } else {
      throw new Error;
    }
  }
}
\end{lstlisting}

\end{document}

MWE

commonhare
  • 2,630
  • 1
    @commonhare, I was doing my answer based on this site but you have ahead, so just add an the example to your answers, as suggest Thorsen. – Fran Jan 06 '13 at 11:18
  • 5
    Now that the link is dead, I'm glad the entire example was included. Thank you. – Kristoffer la Cour Feb 22 '14 at 22:25
  • I wanted to highlight ?s, but adding it as a keyword didn't work. I was able to highlight it by adding moredelim=**[il][\mdseries{\color{blue}?}\mdseries]{?},. Seems to work with multiple questions marks (object?.maybeAttribute?.value), as well as not highlighting in comments (// this won't be blue?) – jeffrey.d.m Mar 30 '22 at 10:09
19

I merged the answers of @awx and @commonhare to get this gist:

\usepackage{color}
\definecolor{lightgray}{rgb}{.9,.9,.9}
\definecolor{darkgray}{rgb}{.4,.4,.4}
\definecolor{purple}{rgb}{0.65, 0.12, 0.82}
\lstdefinelanguage{JavaScript}{
  keywords={break, case, catch, continue, debugger, default, delete, do, else, false, finally, for, function, if, in, instanceof, new, null, return, switch, this, throw, true, try, typeof, var, void, while, with},
  morecomment=[l]{//},
  morecomment=[s]{/*}{*/},
  morestring=[b]',
  morestring=[b]",
  ndkeywords={class, export, boolean, throw, implements, import, this},
  keywordstyle=\color{blue}\bfseries,
  ndkeywordstyle=\color{darkgray}\bfseries,
  identifierstyle=\color{black},
  commentstyle=\color{purple}\ttfamily,
  stringstyle=\color{red}\ttfamily,
  sensitive=true
}

\lstset{
   language=JavaScript,
   backgroundcolor=\color{lightgray},
   extendedchars=true,
   basicstyle=\footnotesize\ttfamily,
   showstringspaces=false,
   showspaces=false,
   numbers=left,
   numberstyle=\footnotesize,
   numbersep=9pt,
   tabsize=2,
   breaklines=true,
   showtabs=false,
   captionpos=b
}
sternAndy
  • 361
  • Note: it seems that ndkeywords is deprecated: http://texdoc.net/texmf-dist/doc/latex/listings/listings.pdf (page 44) – lanoxx Sep 25 '16 at 14:08
9

The currently accepted answer misses a lot of keywords, so here's my language definition:

\lstdefinelanguage{JavaScript}{
  keywords={break, case, catch, continue, debugger, default, delete, do, else, finally, for, function, if, in, instanceof, new, return, switch, this, throw, try, typeof, var, void, while, with},
  morecomment=[l]{//},
  morecomment=[s]{/*}{*/},
  morestring=[b]',
  morestring=[b]",
  sensitive=true
}
awx
  • 91
2

A complete keyword list which also covers typescript would be:

keywords={abstract, any, as, boolean, break, case, catch, class, console, 
    const, continue, debugger, declare, default, delete, do, else, enum, export, 
    extends, false, finally, for, from, function, get, if, implements, import, in, 
    infer, instanceof, interface, keyof, let, module, namespace, never, new, null, 
    number, object, package, private, protected, public, readonly, require, return, 
    set, static, string, super, switch, symbol, this, throw, true, try, type, typeof, 
    undefined, unique, unknown, var, void, while, with, yield}
Ischen
  • 21
  • 1
2

I usually use the \lstset at the beginning of the each files(chapter1.tex - C code, chapter2.tex - Matlab code) with following setting:

\lstset{ %
  language=C,                            % choose the language of the code
  basicstyle=\footnotesize,              % the size of the fonts that are used for the code
  backgroundcolor=\color{White},         % choose the background color. You must add \usepackage{color}
  commentstyle=\color{help}\textit,
  keywordstyle=\color{keyword}\textbf,
  breaklines=true,                       % sets automatic line breaking
  breakatwhitespace=true,                % sets if automatic breaks should only happen at whitespace
  showspaces=false,                      % show spaces adding particular underscores
  showstringspaces=true,                 % underline spaces within strings
  showtabs=true,                         % show tabs within strings adding particular underscores
  frame=none,                              % adds a frame around the code - none, single
  tabsize=8,                             % sets default tabsize to 8 spaces
  captionpos=b,                          % sets the caption-position to bottom
  numbers=left,                          % where to put the line-numbers -none, left, right
  numberstyle=\footnotesize,             % the size of the fonts that are used for the line-numbers
  stepnumber=1,                          % the step between two line-numbers. If it's 1 each line
                                         % will be numbered
}
JardaFait
  • 3,922