Problem statement
I have several code listings containing HTML markup in my latex files. These listings contain multiple custom html tags (created in combination with corresponding AngularJS directives).
Such an HTML fragment could look for example like this:
<list-component data="angularDataObject"
list-type="dataActor">
</list-component>
<div id="test"></div>
Originating from a CamelCase name of a directive listComponent
in JavaScript, a dash is added in between resulting in list-component
to maintain code readability.
I tried using both listings and minted for syntax highlighting:
listings
Using listings I achieved the following result:
\documentclass{article}
\usepackage{listings}
\begin{document}
\begin{lstlisting}[language=HTML]
<list-component data="angularDataObject"
list-type="dataActor">
</list-component>
<div id="test"></div>
\end{lstlisting}
\end{document}
As you can see, the tag name is not identified, neither the custom attribute list-type
. I think the word type
is recognized as standard html attribute and therefore highlighted.
minted
Using minted I achieved the following result:
\documentclass{article}
\usepackage{minted}
\usemintedstyle{borland}
\begin{document}
\begin{minted}{html}
<list-component data="angularDataObject"
list-type="dataActor">
</list-component>
<div id="test"></div>
\end{minted}
\end{document}
The minted result is even worse, as the word list
is highlighted before the dash – component
by contrast isn't, even though list
is not an HTML standard tag. Attributes look fine this time, but the closing tag is not identified at all and gets an ugly background behind the opening bracket <
.
My goal
I search for a plugin that is able to automaticly detect and highlight custom tags, without needing to add them each by each – since I might be unsure which tags I'll use in the future. Further it would be great to identify custom attributes, but I could go without that, if attributes are not highlighted at all.
Finally it is essential that closing tags are at least highlighted the same way as the opening ones were highlighted before.
Thanks in advance for your generous help and advices!
By the way, while typing this question and adding language data for different code highlighting I discovered that Google Code prettifier is analyzing and displaying my code perfectly :-/
Other related TEX.SE questions
\listings code style for HTML5 (CSS, HTML, JavaScript)
In this solution any tags must be noted in the settings in order to be identified.
How to highlight own keywords in HTML lstlistings?
This solution allows individual highlighting of code parts, but changes need to be hard-coded into the listing. This ruins readability in the TEX markup.
How to use angle brackets for a keyword in \listings?
This solution reuires once again that all tags are defined a priori.
Best Answer
Ok, after some thinking about how to do this with
listings
while avoiding manually specifying keywords, I suppose a syntax based solution may work.I.e. I defined a language for
listings
usingmoredelim
andmorestring
to handle the most basic syntax ofXML
and get the following results:the corresponding code is:
For explanation of what the the code does, refer to sections 3.2, 3.3 and 4.18 onwards of the
listings
documentation.Note: for some reason thelist-type
attribute is still wrong, although I toldlistings
to treat-
as a letter withalsoletter={-}
- maybe you can figure out why that is, otherwise I'll have some time for more testing in a few hours.This was due to my usage of a tab for indentation, rather than a space. As that is fixed now, more debugging to do, since the closing>
of thelist-component
tag has now become green as well..Thanks to @Jubobs this is fixed as well - it was my bad to have an extra
*
in the attribute style line. In addition I fixed things to that indentation with tabs also works.