![列表中的锯齿状下划线](https://linux22.com/image/444563/%E5%88%97%E8%A1%A8%E4%B8%AD%E7%9A%84%E9%94%AF%E9%BD%BF%E7%8A%B6%E4%B8%8B%E5%88%92%E7%BA%BF.png)
我试图listing
用典型的锯齿状红色下划线将某些代码标记为错误。
理想情况下,这将是以下代码的输出。
\begin{lstlisting}
12 + ~[1, 2]~
\end{lstlisting}
我迄今为止尝试过
- 我找到了创建锯齿状下划线的代码这里。对于上面的图片,我使用了命令
\MarkText[red][1pt][2pt]{$[1, 2]$}
。 - 我发现该
listing
选项moredelim=**[is][...]{~}{~}
可用于将所需的功能添加到lstlisting
环境中,其中...
应该有一个适当的标记命令。
不幸的是,填写\MarkText[red][1pt][2pt]
点会导致出现模糊的错误信息。我不知道该如何继续。
MWE(几乎所有代码都用于定义\MarkText
命令,您可以跳到该部分%%% START %%%
以快速概览):
\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}