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
using moredelim
and morestring
to handle the most basic syntax of XML
and get the following results:
the corresponding code is:
\documentclass{report}
\usepackage{listings}
\usepackage{color}
\lstdefinelanguage{XML_SYNTAX}{%
alsoletter=-,
morestring=[b]",stringstyle=\color[rgb]{0,0,1},
moredelim=*[s][{\color[rgb]{0.75,0,0}}]{<}{>},
moredelim=[s][{\color[rgb]{0,0,0}}]{<!--}{-->},
moredelim=[s][{\color[rgb]{0,0.75,0}}]{\ }{=},
moredelim=[s][{\color[rgb]{0,0.75,0}}]{\ }{=} % here there is \tab
}
\lstset{
% Basic design
backgroundcolor=\color[rgb]{0.9,0.9,0.9},
basicstyle={\small\ttfamily},
breaklines=true,
frame=l,
tabsize=2,
% Line numbers
xleftmargin={0.75cm},
numbers=left,
stepnumber=1,
firstnumber=1,
numberfirstline=true,
% HTML formatting
language=XML_SYNTAX,
}
\begin{document}
\begin{lstlisting}
<!-- with tab -->
<list-component data="angularDataObject"
list-type="dataActor">
</list-component>
<!-- with space -->
<list-component data="angularDataObject"
list-type="dataActor">
</list-component>
<!-- some other tag -->
<div id="test"></div>
\end{lstlisting}
\end{document}
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 the list-type
attribute is still wrong, although I told listings
to treat -
as a letter with alsoletter={-}
- 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 the list-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.
You load the mcode
package with the framed
option, which adds the thin (undesired) frame around the code. Your mylisting
environment is a color box from the tcolorbox
package, which adds another frame around it. This is why you have the two frames.
To remove the inner frames, simply remove the framed
option when loading mcode
:
\usepackage[numbered,autolinebreaks,useliterate]{mcode}
Remark: you are using two different packages for the same purpose. If you don't watch out, you will get inconsistent output. As both these packages use listings
in the background, they have undesired interactions: for example by removing framed
from the mcode
package, suddenly the line numbers in the matlab-prettifier
listing are outside of the frame - even though you didn't change anything! Please, try to decide for one package1 and stick to that.
You can setup matlab-prettifier
to look like your mcode
listings now, or the other way around.
1 matlab-prettifier
is newer and has more features than mcode
, and it comes with most standard LaTeX installations
Best Answer
Unfortunately
lstlisting
environments can not be embedded inside other environments.Here I define a new environment
lstuselanguage
which takes an argument (the language):to be used in this way:
Output: