3

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?

2 Answers 2

4

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

3
  • Hmm, it seems it has two single left quotes, rather than a double left quote.
    – egreg
    Jul 21, 2016 at 20:20
  • @egreg, you are right, one has to use \textquotedblleft Jul 21, 2016 at 20:26
  • I wish I could accept both answers, since it was really just the literate thing I was looking for. I went with something like this, used '' instead of ", and added {''}{\textquotedblright}1 to the arguments for literate.
    – Anonymous
    Jul 21, 2016 at 20:40
1

listings scans character by character, so you have to teach it not do do this in special cases:

\documentclass{article}

\usepackage{listings}
\lstset{
  language=Java,
  tabsize=2,
  numbers=left,
  basicstyle=\footnotesize,
  literate=
    {``}{\odblq}{1}
    {''}{\cdblq}{1},
}
\newcommand{\odblq}{``}
\newcommand{\cdblq}{''}

\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

Using '' for the closing quotes avoids the problem with " being considered a delimiter.

However, this might confuse your readers, who could think that reverse quotes are needed in their code.

An alternative could be using UTF-8

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{listings}
\lstset{
  language=Java,
  tabsize=2,
  numbers=left,
  basicstyle=\footnotesize,
  literate=
    {“}{\odblq}{1}
    {”}{\cdblq}{1},
}
\newcommand{\odblq}{``}
\newcommand{\cdblq}{''}

\begin{document}

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

}
\end{lstlisting}

\end{document}
3
  • Your two solutions solved the problem with quotation marks (I prefer the last one because I need \usepackage[utf8]{inputenc} in my full document), but if I have defined a stringstyle=\bfseries\color{blue}, this doesn't work properly (I mean the quotation marks works fine but inside “example” the word example doesn't painted blue). Do you need an MWE in a new question?
    – manooooh
    Jul 21, 2018 at 8:46
  • Please see this example.
    – manooooh
    Jul 21, 2018 at 8:50
  • @egreg; nice suggestion...
    – MadyYuvi
    Mar 3, 2020 at 11:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .