It's almost a year later, but as the OP has not yet accepted an answer, I still see my chance :-)
In the following, I use the same basic principle as in my other answer, that is, employ listings
moredelim=**
option to define delimiters, which styles then apply on top of all other formattings, so that the syntax formatting is kept.
However, instead of being limited to a standard font command for the delimiter style, I now use an lrbox
to grab the content of the current group. The resulting box holds the (syntax-formatted) output of listings
, which can be used to implement more fancy highlighting styles.
In the example code, the implementation of the \btHL
command employs TikZ to typeset the lrbox
into a TikZ node. Additional TikZ/PGF options to both, the tikzpicture and the node can be given in the optional argument (such as \btHL[fill=red!20,draw=red]
), which provides for pretty flexible highlighters.
(Note: when the optional argument is used inside some listings
argument, the whole \btHL[<key>=<value>,...]
command has to be put into curly braces in order to not confuse the listings
key=value parser.)
The \btHL
command can as well be used inside normal text. It does, however, not work across line breaks, which is a possible disadvantage compared to the approach that culminated from Bens answer. Insides of listings, however, this usually is not an issue.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings,xcolor,beramono}
\usepackage{tikz}
\makeatletter
\newenvironment{btHighlight}[1][]
{\begingroup\tikzset{bt@Highlight@par/.style={#1}}\begin{lrbox}{\@tempboxa}}
{\end{lrbox}\bt@HL@box[bt@Highlight@par]{\@tempboxa}\endgroup}
\newcommand\btHL[1][]{%
\begin{btHighlight}[#1]\bgroup\aftergroup\bt@HL@endenv%
}
\def\bt@HL@endenv{%
\end{btHighlight}%
\egroup
}
\newcommand{\bt@HL@box}[2][]{%
\tikz[#1]{%
\pgfpathrectangle{\pgfpoint{1pt}{0pt}}{\pgfpoint{\wd #2}{\ht #2}}%
\pgfusepath{use as bounding box}%
\node[anchor=base west, fill=orange!30,outer sep=0pt,inner xsep=1pt, inner ysep=0pt, rounded corners=3pt, minimum height=\ht\strutbox+1pt,#1]{\raisebox{1pt}{\strut}\strut\usebox{#2}};
}%
}
\makeatother
\lstdefinestyle{SQL}{
language={SQL},basicstyle=\ttfamily,
moredelim=**[is][\btHL]{`}{`},
moredelim=**[is][{\btHL[fill=green!30,draw=red,dashed,thin]}]{@}{@},
}
\begin{document}
A listing with {\btHL highlighting of all \textbf{important} elements} looks as follows:
\begin{lstlisting}[style=SQL]
SELECT name, password `FROM` users @WHERE@ name=@UNION SELECT@
\end{lstlisting}
The documentation for listings describes the \lstdefinelanguage
macro, which can be used to define new languages as extensions of others.
\documentclass{article}
\usepackage{listings}
\lstdefinelanguage
[x64]{Assembler} % add a "x64" dialect of Assembler
[x86masm]{Assembler} % based on the "x86masm" dialect
% with these extra keywords:
{morekeywords={CDQE,CQO,CMPSQ,CMPXCHG16B,JRCXZ,LODSQ,MOVSXD, %
POPFQ,PUSHFQ,SCASQ,STOSQ,IRETQ,RDTSCP,SWAPGS, %
rax,rdx,rcx,rbx,rsi,rdi,rsp,rbp, %
r8,r8d,r8w,r8b,r9,r9d,r9w,r9b, %
r10,r10d,r10w,r10b,r11,r11d,r11w,r11b, %
r12,r12d,r12w,r12b,r13,r13d,r13w,r13b, %
r14,r14d,r14w,r14b,r15,r15d,r15w,r15b}} % etc.
\lstset{language=[x64]Assembler}
\begin{document}
\begin{lstlisting}
cdqe 1, r8
push 1
add rsp, 4
push 1
\end{lstlisting}
\end{document}
Which looks a little like:
(I've just added the instructions listed here and only some of the registers, you can easily add any more that you use.)
Best Answer
You don't want to define a dialect but a new language based on xml, so your syntax is wrong. It should be