24

I'm using LaTeX's "listings" package to format source code. Unfortunately I get curly quotes instead of straight quotes. Since the curly quotes don't always point in the right direction, it looks bad. How can I get straight quotes instead?

I'd prefer not to change or filter the source code itself. Filtering the code to properly change " to `` or '' would work, but this is easier done than said with multiple quotes on a line, or quotes spanning multiple lines. Or you could use symbol or a host of other things. But I'd really like to keep the source unchanged.

Example LaTeX:

\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}
Fahrenheit=input("What is the Fahrenheit temperature?")
Celsius=(5.0/9.0)*(Fahrenheit-32)
print"The temperature is",Celsius,"degrees Celsius"
\end{lstlisting}
\end{document}

Example output (using Miktex on windows): Image of source code

(Direct link to image of incorrect output)

Community
  • 1
  • 1
Paul Vincent Craven
  • 2,027
  • 3
  • 19
  • 24
  • It changes the source code but you can fix the direction of the quotes using the ` (key just to the left of your one key) on the left hand side of the quote and ' (on the regular quote key) on the right. – Alex Jan 12 '09 at 01:21

6 Answers6

26

I see in the documentation (which should have been distributed with the packge, but is available at http://www.ctan.org/tex-archive/macros/latex/contrib/listings/listings.pdf) for listings that there is a settable property called upquote to take care of this.

From the documentation:

upquote=⟨true|false⟩                                                false
  determines whether the left and right quote are printed ‘’ or `'. This 
  key requires the textcomp package if true. 

Do something like

\lstset{upquote=true}

before begining the list environment, or use

\begin{lstlisting}[upquote=true]
...
\end{lstlisting}

It is also possible that tis property is already set for you in the appropriate language definition (see the docs again, big list of predefined languages on page 12).

Use:

\lstloadlanguages{<dialects you need>}

in the header. And then set the language using either of the above conventions for choosing options.

dmckee --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
  • This looked promising, but unfortunately it didn't work when I tried it. The curly quotes still appear for me on Linux and Windows with Miktex of I set upquote to true. Setting it to false didn't work either. – Paul Vincent Craven Jan 11 '09 at 19:02
  • That's what I get for just skimming the docs. ChrisN seems to have the right property, but I would encourage you to use the predefined languages if one is appropriate. And, do read the docs, it is all in there. Cheers. – dmckee --- ex-moderator kitten Jan 11 '09 at 21:32
  • I do use the predefined language in my actual work. I stripped out a lot of what I am doing to make the example minimal. That PDF is a good resource, but searching through it I still didn't find the answer. Thanks though. – Paul Vincent Craven Jan 11 '09 at 22:01
  • 1
    This worked for me once I paid attention to the documentation and compilation warnings that told me to `\usepackage{textcomp}`. – Xiong Chiamiov Feb 23 '16 at 05:48
  • This is the correct answer for the question instead of using a different style – Shriraj Hegde Jan 17 '23 at 15:03
10

dmckee's answer above probably works. If you drop your last condition, i.e. you permit changes to the code, then there is a more generic solution, which I tend to use whenever (La)TeX renders a character somehow differently than I expect it to do is to use the \symbol command. I list it here because it can be useful in other situations as well:

\newcommand{\qq}{\symbol{34}} % 34 is the decimal ascii code for "

And then your example:

\begin{lstlisting}
...
print{\qq}The temperature is{\qq},Celsius,{\qq}degrees Celsius{\qq}
...
\end{lstlisting}

Note the curly braces which supposedly take listings back to LaTeX mode (see escapechars option of the package.)

Community
  • 1
  • 1
David Hanak
  • 10,754
  • 3
  • 31
  • 39
10

Have you considered using a monospaced (typewriter) font for the listing? The following example works:

\documentclass{article}
\usepackage{listings}
\lstset{basicstyle=\ttfamily} % <<< This line added
\begin{document}
\begin{lstlisting}
Fahrenheit=input("What is the Fahrenheit temperature?")
Celsius=(5.0/9.0)*(Fahrenheit-32)
print"The temperature is",Celsius,"degrees Celsius"
\end{lstlisting}
\end{document}
ChrisN
  • 16,635
  • 9
  • 57
  • 81
  • 2
    Using \ttfamily in the basic style works great for me too, although it causes me to loose the styling provided by the listing package for my language (such as bolding keywords). Any thoughts besides re-defining the language style? – jasonb Jan 22 '10 at 07:31
9

Here is a solution

\usepackage[T1]{fontenc}  
\usepackage{textcomp}  
\usepackage{lmodern}  

% in the listings package configuration, try:  
literate={"}{\textquotedbl}1,  
Buğra Gedik
  • 714
  • 8
  • 16
6

I had the same problem, using fontspec, and the solution was to not set \defaultfontfeatures{Mapping=tex-text}, but instead setting Mapping=tex-text specifically on only the main and sans font, and leaving the tt font to it's own devices. :)

Lennart Regebro
  • 167,292
  • 41
  • 224
  • 251
1

Maybe it's because I installed listings early as a LaTeX user, but I'm surprised to learn that without the listings package the behaviour is any different.

My solution was similar to David Hanak's, but I used the symbols for double-quote as described in the LaTeX Cheat Sheet (http://stdout.org/~winston/latex)

\newcommand{\QQ}[1]{``#1''}
vo1stv
  • 55
  • 2
  • 17