[Tex/LaTex] XML listing shows incorrect quotation marks

listings

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)?

Best Answer

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}

Comparison OT1 vs. T1 encoding for quotes

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}

\ttfamily

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}

Result

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}

Result upquote