42

All single quotes in code samples are displayed as backticks and the copy-pasted code can't be compiled without fixing the quotation. Is it possible to prevent this?

for example:

\documentclass{beamer}
\usepackage{listings}
\begin{document}

\lstset{language=Python}
\begin{frame}[fragile]{Code}
\begin{lstlisting}
print 'hi' 
\end{lstlisting}
\end{frame}
\end{document}

Compiled with latexmk -pdf file.tex, which creates a log starting like: This is pdfTeX, Version 3.1415926-2.5-1.40.14 (TeX Live 2013).

Output:

enter image description here

lockstep
  • 250,273
perreal
  • 689
  • 1
    Please provide a bit more information about your document and TeX system setup. E.g., do you use pdfLaTeX, XeLaTeX, or LuaLaTeX to compile your document? To help narrow down the list of things to consider, please provide a full MWE (minimum working example, starting with \documentclass{...} and ending with \end{document}) that produces the problem behavior you're looking to fix. – Mico Nov 17 '13 at 05:38
  • 1
    @Mico, thanks, added minimal code. I hope this is sufficient. – perreal Nov 17 '13 at 05:50

2 Answers2

57

You need to load the textcomp package and add upquote=true to your \lstset command. See §4.7 of the listings documentation. Alternatively, you can simply load the upquote package, which will make all verbatim quotes single quotes.

\documentclass{beamer}
\usepackage{listings}
\usepackage{textcomp}
\begin{document}

\lstset{language=Python,upquote=true}
\begin{frame}[fragile]{Code}
\begin{lstlisting}
print 'hi' 
\end{lstlisting}
\end{frame}
\end{document}

output of code

Alan Munn
  • 218,180
  • 7
    You could simply load the package upquote... – karlkoeller Nov 17 '13 at 07:54
  • inside the beamer document class, one must add the [fragile] option, i.e. \begin{frame}[fragile] (as far as I can tell, it fails without it) – PatrickT May 04 '17 at 09:53
  • 1
    @PatrickT Yes, that's what the example code shows. Since it was already in the code posted in the question I didn't mention it explicitly. But beamer requires any verbatim text to be in a [fragile] frame. – Alan Munn May 04 '17 at 12:40
  • @AlanMunn, True. In my code, [fragile] was the missing part that caused an error, so I thought I'd point it out explicitly. – PatrickT May 06 '17 at 06:37
  • The sourcecodepro font doesn't include ` so you might need to change font too. I'm currently using inconsolata to get around this problem. – Jeremy Smyth Jun 03 '19 at 09:38
  • 3
    Starting with the 2020/02/02 release of the LaTeX format, it's no longer necessary to load the textcomp package in order to use the option upquote=true. – Mico Jan 29 '22 at 11:39
-2

Try using ` (the symbol before the the numerical key of 1 above the alphabetical keys, it is on the key that shows: ~, when used with shift!). It works for me while creating documents.

So, essentially it will look like this:

\documentclass{beamer}
\usepackage{listings}
\begin{document}

\lstset{language=Python}
\begin{frame}[fragile]{Code}
\begin{lstlisting}
print `hi' 
\end{lstlisting}
\end{frame}
\end{document}

-Sameer

user33989
  • 1
  • 2
  • 7
    Hi and welcome to TeX.sx. You may want to check out our starter guide: Welcome to TeX.SX! This answer unfortunately doesn't actually answer the question. You've shown (correctly) how to get the correct curly quotes, but the question is asking how to get two identical straight quotes. – Alan Munn Nov 17 '13 at 06:16
  • 1
    @AlanMunn: Thanks. Will take care next time. This was my first attempt to answer! – user33989 Nov 17 '13 at 06:26
  • Apparently, it was user 33989 first and last attemps ;-) – Denis Cousineau Mar 14 '24 at 19:12
  • The issue I found with this (if I'm fine with curly quotes) was that the listings package would not properly color my code with this method. It would not recognize a backtick as a quote character but it would recognize the apostrophe/regular single quote, so then my code was colored to appear as if the ending quote actually began a string literal. So, just as a warning to others, it seems the above method is not entirely sufficient even for curly quotes. – Ac Hybl Mar 17 '24 at 20:29