How to write programming code containing the ' character in latex?
I am trying to write programming code in latex using the verbatim environment, but when I write
\begin{verbatim}
char ch = 'x';
\end{verbatim}
the开发者_开发百科n the ' -characters around x are displayed incorrectly (they look "curly"). How can I fix this problem?
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).
Adding \usepackage{upquote}
to my preamble was sufficient.
Perhaps older versions of LaTeX or upquote required more work.
I have
What is wrong?
New
If you want to get something like this
write
\makeatletter
\let \@sverbatim \@verbatim
\def \@verbatim {\@sverbatim \verbatimplus}
{\catcode`'=13 \gdef \verbatimplus{\catcode`'=13 \chardef '=13 }}
\makeatother
For displaying source code, you might consider using the listings
package; it is quite powerful and offers an option to display “straight” quotation marks.
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).
\textsf{``} and \textsf{''} come pretty close to straight quotes. No need for using any special packages.
This is what I got from another source, and this works.
Use `` to start the double quotes (this symbol is below ~ symbol on our keyboard)
Use '' to close the double quotes (this symbol is below the " symbol on our keyboard)
So, `` quote double, unquote double''
Same goes for single quotes, `quote single, unquote single'
精彩评论