I am afraid that listing doesn't support javascript.
\begin{lstlisting}[language=javascript]
...
\end{lstlisting}
What am I doing wrong?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this communityI am afraid that listing doesn't support javascript.
\begin{lstlisting}[language=javascript]
...
\end{lstlisting}
What am I doing wrong?
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}
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?
)
Mar 30 at 10:09
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
}
ndkeywords
is deprecated: texdoc.net/texmf-dist/doc/latex/listings/listings.pdf (page 44)
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
}
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
}
pygmentize
command. Also, to be able to call that command, your LaTeX will require the--shell-escape
option.