Possible Duplicate:
How to make a real apostrophe or single-quote in LaTeX

I'm building a Python Regex cheat sheet using Latex, which has the following line:

\texttt{re.match('a\textbackslash sb', 'a b')

However, when the line is copied from the PDF document, the apostrophe (') is converted to Unicode Character 'RIGHT SINGLE QUOTATION MARK' (U+2019) ():

re.match(’a\sb’, ’a b’)

This text is not recognizable by the Python interpreter.

Is there a LaTex tag for code that preserves the original characters?

marked as duplicate by egreg, lockstep, Marco Daniel, Thorsten, Martin Schröder Oct 22 '12 at 11:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I wouldn't have expected that, what tex engine are you using. If it's pdftex try adding \usepackage[T1]{fontenc} – David Carlisle Oct 22 '12 at 9:29

With pdflatex use \textquotesingle or \verb together with the package upquote:

\texttt{re.match('a\textbackslash sb', 'a b')}

\texttt{re.match(\textquotesingle a\textbackslash sb\textquotesingle, \textquotesingle a b\textquotesingle)}

\verb+re.match('a\sb', 'a b')+

But you should be aware that copy code lines from a pdf can be problematic: e.g. spaces and identation can easily change.

  • I don't seem to have pdflatex in texShop (Mac OSX), and it does not work with LuaLatex nor XeLaTeX. – Adam Matan Oct 22 '12 at 10:40
  • You certainly have pdflatex. But I forgot a brace in the code (now corrected). With lualatex/xelatex + \usepackage{fontspec} I can't reproduce your problem. You should make a complete example. – Ulrike Fischer Oct 22 '12 at 11:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.