I'm trying to mark some code in a listing as erroneous with a typical jagged red underline.
Ideally, this would be the output of the following code.
\begin{lstlisting}
12 + ~[1, 2]~
\end{lstlisting}
What I tried so far
- I found code to create a jagged underline here. For the image above, I used the command
\MarkText[red][1pt][2pt]{$[1, 2]$}. - I found out that the
listingoptionmoredelim=**[is][...]{~}{~}can be used to add the wanted functionality to thelstlistingenvironment, where...should be an appropriate mark-up command.
Unfortunately, filling in \MarkText[red][1pt][2pt] for the dots results in obscure error messages. I'm not sure how to continue.
MWE (almost all code is there to define the \MarkText command, you can skip to the part %%% START %%% to get a quick overview):
\documentclass{article}
%%%%%% CODE FOR \MarkText MACRO %%%%%%%%
\usepackage{xparse}
\usepackage{soul}
\usepackage{tikz}
\usetikzlibrary{calc}
\usetikzlibrary{decorations.pathmorphing}
\newlength\LineWidth
\newlength\Amplitude
\newlength\SegLength
\setlength\LineWidth{0.4pt}
\setlength\Amplitude{1pt}
\setlength\SegLength{5pt}
\definecolor{HLcolor}{RGB}{240,0,0}
% The following code contains a variation of the great code by Antal S-Z
% in his answer to http://tex.stackexchange.com/a/6029/3954
%in TeX.SX
\newcommand\tikzmark[1]{%
\tikz[overlay,remember picture] \node (#1) {};}
\makeatletter
\newcommand{\highlight@DoHighlight}{
\draw[HLcolor,line width=\LineWidth,decorate,decoration={zigzag,amplitude=\Amplitude,segment length=\SegLength}] ($(begin highlight)+(0,-2pt)$) -- ($(end highlight)+(0,-2pt)$) ;
}
\newcommand{\highlight@BeginHighlight}{
\coordinate (begin highlight) at (0,0) ;
}
\newcommand{\highlight@EndHighlight}{
\coordinate (end highlight) at (0,0) ;
}
\newdimen\highlight@previous
\newdimen\highlight@current
\DeclareRobustCommand*\highlight[1][]{%
\SOUL@setup
%
\def\SOUL@preamble{%
\begin{tikzpicture}[overlay, remember picture]
\highlight@BeginHighlight
\highlight@EndHighlight
\end{tikzpicture}%
}%
%
\def\SOUL@postamble{%
\begin{tikzpicture}[overlay, remember picture]
\highlight@EndHighlight
\highlight@DoHighlight
\end{tikzpicture}%
}%
%
\def\SOUL@everyhyphen{%
\discretionary{%
\SOUL@setkern\SOUL@hyphkern
\SOUL@sethyphenchar
\tikz[overlay, remember picture] \highlight@EndHighlight ;%
}{%
}{%
\SOUL@setkern\SOUL@charkern
}%
}%
%
\def\SOUL@everyexhyphen##1{%
\SOUL@setkern\SOUL@hyphkern
\hbox{##1}%
\discretionary{%
\tikz[overlay, remember picture] \highlight@EndHighlight ;%
}{%
}{%
\SOUL@setkern\SOUL@charkern
}%
}%
%
\def\SOUL@everysyllable{%
\begin{tikzpicture}[overlay, remember picture]
\path let \p0 = (begin highlight), \p1 = (0,0) in \pgfextra
\global\highlight@previous=\y0
\global\highlight@current =\y1
\endpgfextra (0,0) ;
\ifdim\highlight@current < \highlight@previous
\highlight@DoHighlight
\highlight@BeginHighlight
\fi
\end{tikzpicture}%
\the\SOUL@syllable
\tikz[overlay, remember picture] \highlight@EndHighlight ;%
}%
\SOUL@
}
\makeatother
\DeclareDocumentCommand\MarkText{O{red}O{1pt}O{5pt}m}{%
\colorlet{HLcolor}{#1}
\setlength\Amplitude{#2}%
\setlength\SegLength{#3}%
\tikzmark{endquote}\tikzmark{beginquote}\highlight{#4}%
}
%%%%%% START %%%%%%%%
\usepackage{listings}
\lstset{
moredelim=**[is][\MarkText[red][1pt][2pt]]{~}{~}
}
\begin{document}
\begin{lstlisting}
12 + ~[1, 2]~
\end{lstlisting}
\end{document}
