Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I'm currently using the listings package to render my XML snippets. The default XML language settings recognize elements, attribute and text properly and my style is set to render tag names bold, however, I'd like to see the tag surrounding brackets in a bold font face.

So by default a tag is rendered like this: <html> but I'd like it like <.html> (I had to add the . to fool the syntax highlighting). Do you have any ideas how to do that?

I know I could use minted, but I have long snippets than run over more than one page and I also need line wrapping which is AFAIK not supported by minted.

share|improve this question
    
It is always best to compose a MWE that illustrates the problem including the \documentclass and the appropriate packages so that those trying to help don't have to recreate it. Also include some XML code -- best if you can use filecontents. See highlighting strings between and in lstlistings for an example. –  Peter Grill Nov 18 '11 at 18:42
add comment

1 Answer

up vote 5 down vote accepted

As per Mico's answer to using bold italic text inside listings, there is no boldfaced mono spaced font in the Computer Modern font family, so you need to use a font that has a bold mono spaced font.

Borrowing from this earlier question on XML syntax highlighting, here is an example:

enter image description here

Known Issues:

  • I am not sure why the very first non-comment < was not being highlighted.

    Matthias's suggested that this behavior can be overcome by using an escapechar and replacing the first < with !\color{red}{<}! where ! is the escapechar. This has been incorporated into the solution below.

Code:

\documentclass[border=2pt]{standalone}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{pxfonts}

\lstset{
  basicstyle=\ttfamily,
  columns=fullflexible,
  showstringspaces=false,
  commentstyle=\color{gray}\upshape,
  breaklines=true,
}

\lstdefinelanguage{XML}{
  morestring=[b][\color{brown}]",
  morestring=[s][\color{red}\bfseries]{>}{<},
  morecomment=[s]{<?}{?>},
  stringstyle=\color{black},
  identifierstyle=\color{blue},
  keywordstyle=\color{cyan},
  escapechar=!,
  morekeywords={name,use,xmlns,version,type}% list your attributes here,
}


\begin{document}
\begin{lstlisting}[language=XML]
<?xml version="1.0" encoding="utf-8"?>
!\color{red}\bfseries<!xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="points">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" name="point">
          <xs:complexType>
            <xs:attribute name="x" type="xs:unsignedShort" use="required" />
            <xs:attribute name="y" type="xs:unsignedShort" use="required" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>
\end{lstlisting}
\end{document}

Alternate Solution:

Using the solution form ttfamily with bfseries or how to enable bold in fixed width font you can use the courier font with

\renewcommand{\ttdefault}{pcr}

but I personally find these results no so satisfying:

enter image description here

Code:

Same as above but delete \usepackage{pxfonts} and add:

\renewcommand{\ttdefault}{pcr}
share|improve this answer
    
Thanks, works like charm (besides the known issue with the first bracket. I'm curious why the colon (which separates the prefix and the localname) is printed black and not blue? –  vanto Nov 30 '11 at 13:34
    
Hmmm.. I assume it because it is not a normal alphanumeric character so have a different catcode. I tried to adjust that but have not been able to get that character to change color. Perhaps a follow up question? –  Peter Grill Nov 30 '11 at 18:47
    
@Matthias: Thanks for the fix to the first < issue. I have incorporated that into the answer. –  Peter Grill Dec 7 '12 at 0:10
    
@PeterGrill: And thanks for your answer to the question :) –  Matthias Dec 7 '12 at 22:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.