tags:

views:

676

answers:

5

Hi! I am trying to write programming code in latex using the verbatim environment, but when I write

  \begin{verbatim}
     char ch = 'x';
  \end{verbatim}

then the ' -characters around x are displayed incorrectly (they look "curly"). How can I fix this problem?

+2  A: 

If you're seeing curly single right quotes in a verbatim environment, then the single right quote in your typewriter font is curly, and that's the correct one to use for what you're doing (which I assume is displaying some C code).

uckelman
Well, it is not the correct one to use because if someone copy and paste the code from my pdf-document then he or she will get a syntax error, because the compiler is expecting the straight single quote...
ragnarius
I think that uckelman meant that it is a straight single quote, it just looks curly in the font used... Did you try copying and pasting to see if the character itself was modified?
Jimbo
Yes, I pasted it into emacs and it does not look as a straight quote.
ragnarius
Quotes in general are curly, but the quote keys on keyboards (i.e., in ascii) *are* straight. So if you're trying to represent "what to type" then it's wrong to use curly quotes.
Will Robertson
+2  A: 
Alexey Malistov
The quote is "curly", if I copy a curly quote and paste it into the source code I get a syntax error.
ragnarius
And now? I added new information.
Alexey Malistov
Thank for the magic, it works!
ragnarius
This only works for the default CMTT font; it won't work if you switch fonts. E.g., with `\usepackage[T1]{fontenc}`.
Will Robertson
@Will: It is right. I know. But I wanted to help. And I hope I succeeded.
Alexey Malistov
+3  A: 

Load the upquote package to fix this issue in verbatim mode.

If you want straight quotes in monospaced text mode (e.g., \texttt{...}), or indeed in any other font, then you can use the \textquotesingle command defined in the textcomp package:

\documentclass{article}
\usepackage{upquote,textcomp}
\begin{document}
\newcommand\upquote[1]{\textquotesingle#1\textquotesingle}
\verb|'c'| \texttt{\upquote{h}}
\textsf{\upquote{h}} \upquote{h}
\end{document}

This will work well for fonts in any encoding rather than depending on a specific glyph slot (such as \char13 in the default OT1 encoding).

Will Robertson
+2  A: 

For displaying source code, you might consider using the listings package; it is quite powerful and offers an option to display “straight” quotation marks.

Philipp
A: 

\textsf{``} and \textsf{''} come pretty close to straight quotes. No need for using any special packages.

Dave