3

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

8
  • 1
    Best to add an example that shows the problem. What font are you using? Most (or many or some) monospace fonts use straight quotes so it isn't a problem, but you are getting closing quotes.... Sep 4, 2012 at 19:57
  • Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers.
    – jub0bs
    Jan 16, 2014 at 3:05
  • 1
    The (documented) answer to this question is: "\usepackage{textcomp} \lstset{upquote=true}". If it is reopened I can answer it.
    – alexis
    Jan 17, 2014 at 7:59
  • @alexis Even though the question is vague, I'm voting to reopen it so you can answer it.
    – jub0bs
    Mar 10, 2014 at 12:18
  • Thanks, I just did. I don't see anything vague about the question, to be honest. I'd think it would be clear (and very specific) to anyone familiar with TeX's handling of quotes in text and thinking about displaying code. Maybe the phrasing can be improved by someone who finds it vague?
    – alexis
    Mar 10, 2014 at 16:48

2 Answers 2

5

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

3
  • Your document is visually correct but if you copy the xml fragment from the generated PDF, you get ’bar’ instead of 'bar'. You need the upquote 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
2

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

1
  • +1 Very good advice to get correct XML (visually and actually). Mar 11, 2014 at 7:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .