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?

0

4 Answers 4

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}
4
  • is it possible to show the quotation as example1 or example2
    – alhelal
    Commented Sep 26, 2017 at 10:33
  • 1
    @jubobs could you please elaborate on this example a bit? The listings.sty file actually only handles single quotes, but your example does compiles to straight double quotes. However, I ran into a failed example as well.
    – wlnirvana
    Commented Oct 25, 2018 at 1:25
  • @wlnirvana Your problem probably deserves a brand new question on this site.
    – jub0bs
    Commented Oct 25, 2018 at 8:13
  • \setmonofont{Consolas} voids the effects. How do I fix it?
    – Gqqnbig
    Commented Feb 29, 2020 at 2:08
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.

3
  • 1
    It may be a terrible hack, but at least it works \_o_/. Commented Jan 1, 2021 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
    Commented Jan 18, 2022 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
    Commented Nov 27, 2023 at 6:23
2

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}
1
  • 2
    Thank you! None of the other answers worked for me on XeLaTeX. Commented Oct 24, 2022 at 2:59
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 ---.

You must log in to answer this question.

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