[Tex/LaTex] How to make a real apostrophe or single-quote in LaTeX

pdftexpunctuationtypewriter

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.

Best Answer

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.

enter image description here