# How to make a real apostrophe or single-quote in LaTeX

I am trying to incorporate programming examples in a LaTeX document.

The document renders nicely with pdflatex; however, the single quotes get transformed into U+2019, a right-single-quotation mark. And, a backtick renders as U+2018, a left-single-quotation-mark.

What I need is a U+0027 real apostrophe so that the code can be successfully cut-and-pasted from the PDF into actual source code.

\documentclass{article}
\usepackage{alltt}
\usepackage{color}
\usepackage{fullpage}

\begin{document}
\center{\LARGE{hello.py}}
\begin{alltt}

{\color{red}print} 'hello world'

\end{alltt}
\end{document}


The same issue exists also with straight typewriter fonts: \tt{don't worry, be happy}. I'm unclear how to control the exact unicode character that is produced.

-

## migrated from stackoverflow.comJul 14 '12 at 23:07

This question came from our site for professional and enthusiast programmers.

I remember being bugged for this problem time ago, but forgot the result of my investigations. I kind of remember that it was an issue of font enconding, not TeX, and perhaps that changing tt font to courier solved the problem. But I can be misremembering. – JLDiaz Jul 14 '12 at 23:19
Could you add a picture of the problematic output? – doncherry Jul 15 '12 at 10:57
Not quite related to the q., but searching for the problem led me here: Outside of verbatim, you can use \char18 and \char13 to get  and ' characters. – Mohan Nov 20 '12 at 11:13
Reading "real apostrophe" confused me. Wikipedia had this to say about apostrophes: 'There are several types of apostrophe character in Unicode: ( ' ) Vertical typewriter apostrophe (Unicode name apostrophe or apostrophe-quote), U+0027, inherited from ASCII. ( ’ ) Punctuation apostrophe (or typographic apostrophe; right single quotation mark; single comma quotation mark), U+2019. Serves as both an apostrophe and closing single quotation mark. This is the preferred character to use for apostrophe according to the Unicode standard.' So, talk about programming only. B'marked the question though. – Christian Feb 24 '13 at 22:59

Use the upquote package; even if the package documentation doesn't mention alltt, it works also with it:

\documentclass{article}
\usepackage{alltt}
\usepackage{upquote}
\usepackage{color}
\usepackage{fullpage}

\begin{document}

\begin{center}
\LARGE hello.py
\end{center}

\begin{alltt}
{\color{red}print} 'hello world'
\end{alltt}

\end{document}


Notice some modifications to the input.

-
Thank you. The upquote package is exactly what I was looking for. – Raymond Hettinger Jul 15 '12 at 0:04
If I understand this right, it seems using the upquote package will change all apostrophes? Yikes. That's not what I want. In most places, the slanted apostrophe looks nice for indicating derivatives, but I'd like to be able to control the occasional vertical apostrophe. – Travis Bemrose Mar 13 '15 at 2:08
@TravisBemrose No, apostrophes will be changed only in verbatim modes. – egreg Mar 13 '15 at 7:19
What counts as verbatim mode? Is that "regular" text, when I'm not in math mode? – Travis Bemrose Mar 31 '15 at 16:41
@TravisBemrose What's in the argument of \verb, inside a verbatim environment (and also in alltt). – egreg Mar 31 '15 at 16:42

You want to do your own layout? Why not http://en.wikibooks.org/wiki/LaTeX/Packages/Listings?

Listings` even allows you to set up colors and the such.

-
I would like to know how to render apostrophes in the alltt environment for a number of reasons, not just code listings. – Raymond Hettinger Jul 14 '12 at 1:10