I'm using \lstinputlisting from listings package to insert an xml file as a listing in my latex file. In the XML file the quotation marks are double quotes as normal when I view my PDF they appear as double closing inverted commas for both the opening and closing. I know that latex requires to use `` and '' for quotation marks but since I'm importing from an XML file, how can I fix this to make them appear normal(so I get proper opening quotation marks)?
2 Answers
In OT1 encoding only a limited set of quotes are available. Try T1 encoding:
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=XML,basicstyle=\fontencoding{OT1}\selectfont]
<root attr1="OT1" attr2='foobar'/>
\end{lstlisting}
\begin{lstlisting}[language=XML]
<root attr1="T1" attr2='foobar'/>
\end{lstlisting}
\end{document}
Or use a typewriter font:
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=XML,basicstyle=\ttfamily]
<root attr1="foo" attr2='bar'/>
\end{lstlisting}
\end{document}
And I prefer something like
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[variablett]{lmodern}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
columns=flexible,
}
\begin{document}
\begin{lstlisting}[language=XML]
<root attr1="foo" attr2='bar'/>
\end{lstlisting}
\end{document}
A straight single quote is available in encoding TS1
(package textcomp
).
In package listings
this is enabled by option upquote
(Thanks alexis and Paul Gaborit):
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage[variablett]{lmodern}
\usepackage{listings}
\lstset{
basicstyle=\ttfamily,
columns=flexible,
upquote,
}
\begin{document}
\begin{lstlisting}[language=XML]
<root attr1="foo" attr2='bar'/>
\end{lstlisting}
\end{document}
-
Your document is visually correct but if you copy the xml fragment from the generated PDF, you get
’bar’
instead of'bar'
. You need theupquote
option. Mar 11, 2014 at 6:53 -
@PaulGaborit: Thanks, I have updated the answer. However it remains viewer dependent. AR9/Linux works fine, but xpdf v3.00 now replaces the single quotes with spaces. Perhaps it can't handle the glyph name
/quotesingle.ts1
, because it does not ignore the suffix. Mar 11, 2014 at 9:56 -
Here (xubuntu 13.10), with xpdf 3.03, your last example is perfect (visually and via copy/paste). Mar 11, 2014 at 12:22
Use the listings
package option upquote=true
to get upright single quotes (requires the textcomp
package), together with a tt
font style or the T1
encoding to get your double quotes to display as vertical wedges. With the default font and input encoding, your double quotes would all look like double closing quotes.
\documentclass{article}
\usepackage{textcomp}
\lstset{language=XML,upquote=true,basicstyle=\ttfamily}
\begin{document}
\begin{lstlisting}
<root double="attribute" single='attribute'/>
\end{lstlisting}
\end{document}
Credit: I learned about the T1
option for getting undirected quotes from @HeikoOberdiek's answer, and I'm including it for completeness. It works like this: basicstyle=\fontencoding{T1}\selectfont
(as an alternative to using basicstyle=\ttfamily
).
\usepackage{textcomp} \lstset{upquote=true}
". If it is reopened I can answer it.