I need to create some jQuery-Listings but when I try to do so
\documentclass[12pt,oneside,a4paper]{scrartcl}
\usepackage{textcomp}
\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\ttfamily,
numbersep=9pt,
tabsize=2,
breaklines=true,
showtabs=false,
frame=leftline,
caption=\lstname}
\begin{document}
\begin{lstlisting}[name=Beispiel mit JavaScript Bibliothek (jQuery)]
\$("p").elements.each(function() {
\$(this).html = "Element erkannt";
});
\end{lstlisting}
\end{document}
The $
doesn't get displayed. If I try to escape it normally using \$("#test")
, it ends up looking like \ ("test")
. The $
is still omitted and the backslash is displayed.
How can I get the $
to work in a lstlisting?
Best Answer
(This answer is based on a previous answer of mine from another problem : my previous answer)
The solution is to use the
literate
option within thelsset
command. I modified your code (note that you need only enter$
and not\$
in the actual code listing. For visual purposes, I included a coloring for the $ but you may change it to suit your needs.The result is