7

How do I make comments highlighted in the form:

<!-- ======= Processing Root tag =======   -->

. This is not working.

\documentclass[11pt]{scrartcl}
\usepackage{listings, color}

\definecolor{gray}{rgb}{0.4,0.4,0.4}
\definecolor{darkblue}{rgb}{0.0,0.0,0.6}
\definecolor{cyan}{rgb}{0.0,0.6,0.6}

\lstloadlanguages{XML}

\lstdefinelanguage{XML}
{
  morestring=[b]",
  morestring=[s]{>}{<},
  morecomment=[s]{<?}{?>},
  morecomment=[s][\color{orange}]{<!--}{-->},
  stringstyle=\color{black},
  identifierstyle=\color{darkblue},
  keywordstyle=\color{cyan},
  morekeywords={xmlns,version,type}
}

\lstdefinestyle{listXML}{language=XML, extendedchars=true,  belowcaptionskip=5pt, xleftmargin=1.8em, xrightmargin=0.5em, numbers=left, numberstyle=\small\ttfamily\bf, frame=single, breaklines=true, breakatwhitespace=true, breakindent=0pt, emph={}, emphstyle=\color{red}, basicstyle=\small\ttfamily, columns=fullflexible, showstringspaces=false, commentstyle=\color{gray}\upshape}

\begin{document}
 XSLT listing:
 \begin{lstlisting}[style=listXML]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     version="1.0">
<!-- ======= Processing Root tag =======   -->
<xsl:template match="Root">
    <TeXML>
      <TeXML escape="0">
                \documentclass[14pt]{extarticle}
                \usepackage[T2A]{fontenc}
                \usepackage{pscyr}
                \usepackage[cp1251]{inputenc}
                \usepackage[english,russian]{babel}
                \usepackage{longtable}
                \usepackage[a4paper, left=30mm, right=15mm, top=20mm, bottom=20mm]{geometry}
                \usepackage{indentfirst}
                \usepackage{fixltx2e}
         </TeXML>
      <env name="document">
        <cmd name="clearpage"/>
        <xsl:apply-templates select="BasicInfo | StatusInfo"/>
      </env>
    </TeXML>
  </xsl:template>
  <!-- ======= Types of opening ====== -->
  <xsl:template match="ConsDescr">
    <cmd name="par"/>
    <cmd name="textbf">
....
\end{lstlisting}
 \end{document}
15

The problem is that the following two lines in your \lstdefinelanguage{XML} are in conflict:

morestring=[s]{>}{<},
morecomment=[s][\color{orange}]{<!--}{-->},

In addition, doesn't \lstdefinelanguage{XML} redefine the XML language settings, thereby losing a lot of configs? One of the most noticeable drawbacks is that you can no longer access tagstyle, while the identifierstyle setting would highlight both tag names and contents (the embedded LaTeX code).

Would it be feasible for you to move all the settings into \lstdefinestyle instead, dropping morestring=[s]{>}{<}, and using tagstyle in place of identifierstyle?

\lstdefinestyle{listXML}{language=XML, extendedchars=true,  belowcaptionskip=5pt, xleftmargin=1.8em, xrightmargin=0.5em, numbers=left, numberstyle=\small\ttfamily\bf, frame=single, breaklines=true, breakatwhitespace=true, breakindent=0pt, emph={}, emphstyle=\color{red}, basicstyle=\small\ttfamily, columns=fullflexible, showstringspaces=false, commentstyle=\color{gray}\upshape,
morestring=[b]",
morecomment=[s]{<?}{?>},
morecomment=[s][\color{orange}]{<!--}{-->},
keywordstyle=\color{cyan},
stringstyle=\color{black},
tagstyle=\color{darkblue},
morekeywords={xmlns,version,type}
}

This is the output I got:

enter image description here

  • 1
    @Youra_P You're welcome. :-) If this answer has solved your problem, it would be much appreciated if you could "accept" it by clicking the tick mark next to it (you may have to mouse-over the upvote count area to see it). Only when there is an accepted answer (even if it's the only answer given), will a question be deemed "answered" on stackexchange. – LianTze Lim Jul 3 '12 at 2:47
  • This is the first Latex XML Highlighting code that doesn't break with my XML files. Thanks – user7581 Oct 21 '15 at 8:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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