29

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.

5
  • 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, 2012 at 23:19
  • Could you add a picture of the problematic output?
    – doncherry
    Jul 15, 2012 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, 2012 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, 2013 at 22:59
  • same for XeTeX: tex.stackexchange.com/questions/63345/… Aug 19, 2014 at 13:20

3 Answers 3

26

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

5
  • Thank you. The upquote package is exactly what I was looking for. Jul 15, 2012 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. Mar 13, 2015 at 2:08
  • @TravisBemrose No, apostrophes will be changed only in verbatim modes.
    – egreg
    Mar 13, 2015 at 7:19
  • What counts as verbatim mode? Is that "regular" text, when I'm not in math mode? Mar 31, 2015 at 16:41
  • 1
    @TravisBemrose What's in the argument of \verb, inside a verbatim environment (and also in alltt).
    – egreg
    Mar 31, 2015 at 16:42
1

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.

1
  • 1
    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, 2012 at 1:10
1

For starting quote use the slash that is before 1 and below tilde. You can use twice for double quote. For closing use the slash that is below ". You can use it twice to close it also. This produces proper quote and unquote symbols.

You must log in to answer this question.

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