Fonts & Character SetsListing package displays curly 'quotes' and tiny tildes ~

Information and discussion about fonts and character sets (e.g. how to use language specific characters)
stinkinrich88
Posts: 52
Joined: Sat Jun 07, 2008 11:56 am

Listing package displays curly 'quotes' and tiny tildes ~

Post by stinkinrich88 »

It seems that when using language=R, it does not like using % as escape characters (maybe % is a key symbol in language R). To solve this, use a different escape character. In the following code, I used £ as the escape character, and it works fine:

Code: Select all

    \documentclass[pdftex,a4paper]{scrreprt}
    \usepackage[ngerman]{babel}
    \usepackage[utf8]{inputenc}
    \usepackage{color}
    \usepackage[formats]{listings}
    \definecolor{darkred}{rgb}{.5,0,0}
    \definecolor{darkblue}{rgb}{0,0,.5}
    \lstset{
       basicstyle=\ttfamily,
       frame=lines,
       tabsize=4,
       showstringspaces=false,
       keywordstyle=\color{darkblue},
       stringstyle=\color{darkred},
       identifierstyle=\bfseries,
       escapechar={\£}
       }
    \begin{document}
    lorem ipsum
    \begin{lstlisting}[language=SAS]
       £$\sim$£
       set &dat1;
       title1 'test';
    \end{lstlisting}
    \begin{lstlisting}[language=R]
       #comment
       £$\sim$£
            importXPORT <- function(filename) {
    }
    \end{lstlisting}
    \end{document}
Here is the same solution using straight quote symbols instead of curly quote symbols:

Code: Select all

    \documentclass[pdftex,a4paper]{scrreprt}
    \usepackage[ngerman]{babel}
    \usepackage[utf8]{inputenc}
    \usepackage{textcomp}
    \usepackage{color}
    \usepackage[formats]{listings}
    \definecolor{darkred}{rgb}{.5,0,0}
    \definecolor{darkblue}{rgb}{0,0,.5}
    \lstset{
       upquote=true,
       basicstyle=\ttfamily,
       frame=lines,
       tabsize=4,
       showstringspaces=false,
       keywordstyle=\color{darkblue},
       stringstyle=\color{darkred},
       identifierstyle=\bfseries,
       escapechar={\£}
       }
    \begin{document}
    lorem ipsum
    \begin{lstlisting}[language=SAS]
       £$\sim$£
       set &dat1;
       title1 'test';
    \end{lstlisting}
    \begin{lstlisting}[language=R]
       #comment
       £$\sim$£
            importXPORT <- function(filename) {
    }
    \end{lstlisting}
    \end{document}
Hope this helps!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
dontpanic
Posts: 5
Joined: Mon Jul 26, 2010 1:13 pm

Re: Listing package displays curly 'quotes' and tiny tildes

Post by dontpanic »

great, now it works... thank you!
Post Reply