Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
2  
we need more informations about your font setting –  Herbert May 17 '11 at 14:20
1  
Please provide a minimal working example illustrating your problem. Upon suitable completion your code snippet doesn't show the bahaviour mentioned. –  Gonzalo Medina May 17 '11 at 14:23
2  
The MWE works fine for me. I've had problems with symbols not showing up in listings before and I solved them by adding \usepackage[T1]{fontenc} to the beginning of my preamble... –  Seamus May 17 '11 at 16:23
1  
Compile your document with pdflatex and put the log file and the resulting pdf somewhere on the net for inspection. –  Ulrike Fischer May 18 '11 at 8:00
1  
As stated before, this works for other people and me. Please update your TeX installation to the latest version and try again. –  Martin Scharrer Jul 18 '11 at 8:44

2 Answers 2

up vote 3 down vote accepted

(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

enter image description here

share|improve this answer

(Sorry, it looks like my answer do not address the actual problem here)

You can add mathescape=false to your \lstset{...} and use non-escaped $ signs in your listing.

If you need both the mathescape functionality and to display $ characters you can change the escapechar.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.