在列表框中使用颜色框而不丢失其他格式?

在列表框中使用颜色框而不丢失其他格式?

我有一个定义如下的 lstlisting

\begin{lstlisting}[language=bash,frame=single]
Offset(P)  Local Address             Remote Address            Pid
---------- ------------------------- ------------------------- ---
0x01e6a9f0 192.168.0.176:1176        212.150.164.203:80        888
0x01ec57c0 192.168.0.176:1189        192.168.0.1:9393          1244
0x01ed4270 192.168.0.176:2869        192.168.0.1:30379         1244
0x01eef808 192.168.0.176:2869        192.168.0.1:30380         4
0x01ffa7f8 0.0.0.0:0                 80.206.204.129:0          0
0x02041108 127.0.0.1:1168            127.0.0.1:1169            888
0x0225a448 192.168.0.176:1172        66.249.91.104:80          888
0x0226ac58 127.0.0.1:1169            127.0.0.1:1168            888
0x0227ac58 192.168.0.176:1171        66.249.90.104:80          888
0x02308890 192.168.0.176:1178        212.150.164.203:80        1752
0x02323008 192.168.0.176:1184        193.104.22.71:80          880
0x02410440 192.168.0.176:1185        193.104.22.71:80          880
\end{lstlisting}

我想突出显示某些行,因此我尝试这样做:

\begin{lstlisting}[language=bash,escapeinside={<@}{@>},frame=single]
Offset(P)  Local Address             Remote Address            Pid
---------- ------------------------- ------------------------- ---
<@\colorbox{yellow}{0x01e6a9f0 192.168.0.176:1176        212.150.164.203:80        888}@>
0x01ec57c0 192.168.0.176:1189        192.168.0.1:9393          1244
0x01ed4270 192.168.0.176:2869        192.168.0.1:30379         1244
0x01eef808 192.168.0.176:2869        192.168.0.1:30380         4
0x01ffa7f8 0.0.0.0:0                 80.206.204.129:0          0
0x02041108 127.0.0.1:1168            127.0.0.1:1169            888
0x0225a448 192.168.0.176:1172        66.249.91.104:80          888
0x0226ac58 127.0.0.1:1169            127.0.0.1:1168            888
0x0227ac58 192.168.0.176:1171        66.249.90.104:80          888
0x02308890 192.168.0.176:1178        212.150.164.203:80        1752
0x02323008 192.168.0.176:1184        193.104.22.71:80          880
0x02410440 192.168.0.176:1185        193.104.22.71:80          880
\end{lstlisting}

但突出显示的行丢失了之前的格式。我不确定如何实现我想要的效果?

答案1

你可以使用三个列表,并为中间的列表设置背景颜色,这样可以稍微省点时间。垂直边距需要使用aboveskip和进行一些调整belowskip。框架可以使用framed包单独绘制。

梅威瑟:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{framed}
\begin{document}
\begin{framed}
\scriptsize
\begin{lstlisting}[belowskip=0pt]
Offset(P)  Local Address             Remote Address            Pid
---------- ------------------------- ------------------------- ---
\end{lstlisting}
\begin{lstlisting}[backgroundcolor=\color{yellow},aboveskip=0pt,belowskip=0pt]
0x01e6a9f0 192.168.0.176:1176        212.150.164.203:80        888+
\end{lstlisting}
\begin{lstlisting}[aboveskip=0pt]
0x01ec57c0 192.168.0.176:1189        192.168.0.1:9393          1244
0x01ed4270 192.168.0.176:2869        192.168.0.1:30379         1244
0x01eef808 192.168.0.176:2869        192.168.0.1:30380         4
0x01ffa7f8 0.0.0.0:0                 80.206.204.129:0          0
0x02041108 127.0.0.1:1168            127.0.0.1:1169            888
0x0225a448 192.168.0.176:1172        66.249.91.104:80          888
0x0226ac58 127.0.0.1:1169            127.0.0.1:1168            888
0x0227ac58 192.168.0.176:1171        66.249.90.104:80          888
0x02308890 192.168.0.176:1178        212.150.164.203:80        1752
0x02323008 192.168.0.176:1184        193.104.22.71:80          880
0x02410440 192.168.0.176:1185        193.104.22.71:80          880
\end{lstlisting}
\end{framed}
\end{document}

结果:

在此处输入图片描述

相关内容