33

My question is related to the following two

but I'm specifically asking about double-quote characters (ASCII decimal code 34). They appear curly in my listings. How can I get straight double quotes instead?

MWE:

\documentclass{article}
\usepackage{textcomp,upquote,listings}
\lstset{upquote=true}
\begin{document}
\begin{lstlisting}
echo "Hello, world!"
\end{lstlisting}
\end{document}

produces

Output of the provided MWE; contains closing curly quotes

I understand that the upquote package only applies to single quotes. How can I make it apply to double quotes as well?

jub0bs
  • 58,916
wchargin
  • 3,139

4 Answers4

46

Simply add \usepackage[T1]{fontenc} to your preamble. Note that you don't need the upquote package if you load textcomp and set listings' upquote key to true.

enter image description here

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{listings}
\lstset{upquote=true}
\begin{document}
\begin{lstlisting}
echo "Hello, world!"
\end{lstlisting}
\end{document}
jub0bs
  • 58,916
5

For, me the above solutions did not work.

The main problem can be seen in listings.sty, lines 922-943, where a dispatch table of handling special characters in created, but the upquote is only done for single quotes.

One can fix this by adding the following to your preamble:

\usepackage{listings}
\lstset{upquote=true}
% ...
\makeatletter
\lst@CCPutMacro
    \lst@ProcessOther {"22}{\lst@ifupquote \textquotedbl
                                     \else \char34\relax \fi}
    \@empty\z@\@empty
\makeatother
% ...

which monkey-patches that table.

Caveat emptor: this is a terrible hack which relies on modifying internal macros of the listings package. This may arbitrarily and completely break and may even not work on your TeX distro.

  • 1
    It may be a terrible hack, but at least it works \o/. – Tom Carpenter Jan 01 '21 at 16:31
  • 3
    I only added \lstset{upquote=true} to the preamble, one line, and it worked, i.e. straight single or double quotes. – John Jan 18 '22 at 13:37
  • I prefer this because I was using the metropolis theme and using the T1 fontenc would change the entire font; thank you, @summentier! – Kim Nov 27 '23 at 06:23
1

If you are using XeLaTeX and you want to change the font (say, to use the default typewriter font), use the fontspec package to turn off TeX ligatures, as follows:

% !TEX TS-program = xelatex
% !TEX encoding = UTF-8 Unicode
\documentclass{article}

\usepackage{fontspec} \usepackage{listings}

\lstset{basicstyle=\ttfamily\addfontfeature{Mapping=no-mapping-ligtex}}

\begin{document}

\begin{lstlisting} echo "Hello, world!" \end{lstlisting}

\end{document}

0

One possible reason for double quotes showing up as curly is the usage of TeX ligatures — this is already mentioned in AthanasiusOfAlex's answer. As I discovered, one way these ligatures can be (often inadvertently) enabled is through the usage of a line such as

\defaultfontfeatures{Ligatures=TeX}

This line ensures that TeX ligatures are enabled for all fonts, including any monospaced fonts subsequently defined using \setmonofont. This then leads to curly quotes in listings which couldn't be solved using any of the other answers.

If you have such a line, get rid of it. The fontspec package already enables TeX ligatures by default for the \setmainfont and \setsansfont commands, which probably correspond to the fonts where you do want the ligatures. (This is mentioned in Part II, Section 1 of the package documentation.)

If you then want to type an em dash (for example) in the code listing, just use the actual Unicode symbol for it () instead of typing three hyphens ---.