[Tex/LaTex] How to get matching curly double quotes using listings

listingspunctuation

I'm having a strange problem with listings. I'm trying to use matching double quotes around strings, as in this minimal example:

\documentclass{article}

\usepackage{listings}
\lstset{
    language=Java,
    tabsize=2,
    numbers=left,
    basicstyle=\footnotesize
}

\begin{document}

\begin{lstlisting}
void my_method() {
    int test = 0;
    some_method(``string");
    boolean b = false;
    some_method(``string");

}
\end{lstlisting}

\end{document}

First of all, the backticks render individually instead of as a nice curly left double quote like I want. And, well, the bigger problem is that after inserting the backticks it starts showing all the whitespace in my code in between the two strings, even when I add showspaces=false to my settings, as in the following screenshot:

result of the above markup

I've found a lot about how to use straight double quotes, but I actually do want the curly quotes. Does anyone know how I can insert the left quotation marks properly?

Best Answer

You can get the curly left quotes with literate, but imho you have to disable the string delimiter to avoid to get the spaces shown.

\documentclass{article}

\usepackage{listings}
\lstset{
    language=Java,
    tabsize=2,
    numbers=left,
    basicstyle=\footnotesize,
    literate={``}{\textquotedblleft}1,
    deletestring=[b]",
    ,
}

\begin{document}

\begin{lstlisting}
void my_method() {
    int test = 0;
    some_method(``string");
    boolean b = false;
    some_method(``string");

}
\end{lstlisting}

\end{document}

enter image description here