(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 the lsset
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.
\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,
literate={\$}{{\textcolor{blue}{\$}}}1
}
\begin{document}
\begin{lstlisting}[name=Beispiel mit JavaScript Bibliothek (jQuery)]
$("p").elements.each(function() {
$(this).html = "Element erkannt";
});
\end{lstlisting}
\end{document}
The result is
use it this way:
\documentclass{beamer}
\usepackage{listings}
\lstset{
language=Java,
tabsize=8,
keepspaces,
extendedchars=true,
rulecolor=\color{black},
basicstyle=\footnotesize,
aboveskip=5pt,
upquote=true,
columns=fixed,
showstringspaces=false,
extendedchars=true,
breaklines=true,
frame=single,
showtabs=true,
showspaces=false,
showstringspaces=false,
}
\begin{document}
\defverbatim\lst{%
\begin{lstlisting}
public class Teddy {
public MethodName(String name, int var) {
this.name = name;
}
}
\end{lstlisting}
}
\begin{frame}
\lst
\end{frame}
\end{document}
Best Answer
As requested by Jubobs, here is a simple document elaborating on the comments, showing how to use math literals to use
\leftarrow
for assignment in alstlistings
environment.Preamble
Here, we use
\lstset
to change the settings for alllstlisting
environments in the document. You could put these options in the argument to an individual argument if you prefer. I've set the columns tofullflexible
for the sake of presentation, and added keywords because I haven't chosen any language. The settingmathescape=true
will allow us to use math-mode to typeset special character combinations.The list of 'literates' (note: no commas separating the items in the list!) tells
listings
how to typeset special character combinations. Here, assignment (=
) is being typeset with\leftarrow
, and equality testing (==
) is being typeset with the equality symbol. The{}
after the symbols is necessary for correct spacing in thefullflexible
column option: it has no effect withfixed
. Note the comma after the last literal, before themorekeywords
option.Sample document
Output