我想在列表环境中突出显示代码。我尝试了两种技术:使用 \usepackage{lstlinebgrd} 和使用 \usepackage{color}(基于这次谈话),但没有一个真正符合我的要求。如果一行代码太长,一行放不下,- colorbox 将不遵守换行符 - lstlinebgrd 将遵守换行符,但不会突出显示第二行(参见示例)
最重要的是,colorbox 会在列表框架中创建空白。
您是否知道即使在列表列表中有很长的行也能起作用的解决方案?
MWE(它有点长,因为我想显示我在主文档上使用的所有 lstlisting 选项):
\documentclass{article}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{listings}
\usepackage{lstlinebgrd}
\lstset{columns=fullflexible,
basicstyle=\small,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{ForestGreen},
frame=single,
extendedchars=true,
keywordstyle=\color{blue},
language=Python,
numbers=left,
numbersep=5pt,
numberstyle=\scriptsize\color{gray},
rulecolor=\color{black},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{purple},
tabsize=2,
title=\lstname,
belowskip=-1em,
aboveskip=1em,
belowcaptionskip=0em,
}
\begin{document}
First example
\begin{lstlisting}[escapechar=!]
dummycode
!\colorbox{blue!35}{dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-}!
\end{lstlisting}
second example
\begin{lstlisting}[
linebackgroundcolor={%
\ifnum\value{lstnumber}=2
\color{green!35}
\fi
}
]
dummycode
dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode
\end{lstlisting}
\end{document}
答案1
我将您的 MWE 修改为:
\documentclass{article}
\def\cccolorbox#1#2{\ifx#2\relax\let\next\allowbreak\else
\def\next{\colorbox{#1}{#2}\allowbreak\cccolorbox{#1}}\fi\next}
\def\ccolorbox#1#2{\fboxsep0pt\cccolorbox{#1}#2\relax}
\def\!#1{\ifx#1\ccolorbox\allowbreak\expandafter\ccolorbox\else
\ifx#1\end\expandafter\expandafter\expandafter\end\else
#1\allowbreak\expandafter\expandafter\expandafter\!\fi\fi}
\def\cccolorbox#1#2{\ifx#2\relax\let\next\allowbreak\else
\def\next{\colorbox{#1}{\strut #2}\allowbreak\cccolorbox{#1}}\fi\next}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{listings}
\usepackage{lstlinebgrd}
\lstset{columns=fullflexible,
basicstyle=\small,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{ForestGreen},
frame=single,
extendedchars=true,
keywordstyle=\color{blue},
language=Python,
numbers=left,
numbersep=5pt,
numberstyle=\scriptsize\color{gray},
rulecolor=\color{black},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{purple},
tabsize=2,
title=\lstname,
belowskip=-1em,
aboveskip=1em,
belowcaptionskip=0em,
}
\begin{document}
First example
\begin{lstlisting}[escapechar=!]
! \ccolorbox{blue!35}{dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-} !
\end{lstlisting}
second example
\begin{lstlisting}[escapechar=!]
dummycode
!\colorbox{blue!35}{\parbox{0.95\textwidth}{dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-}}!
\end{lstlisting}
\end{document}
第一个例子基于以下讨论:
列表的背景颜色没有正确换行
但结果对我来说看起来很丑陋。或者,在第二个例子中,我在 \colorbox 内使用了 \parbox。它看起来更好,但没有缩进,外部框架仍然支离破碎。这是我得到的结果:
编辑:
通过注释该行:
frame=single,
使用 framed 包中定义的 framed 环境,您可以修复碎片框问题。此外,使用 hangpars 环境,您可以在 parbox 内正确缩进。
以下是更新后的示例:
\documentclass{article}
\def\cccolorbox#1#2{\ifx#2\relax\let\next\allowbreak\else
\def\next{\colorbox{#1}{#2}\allowbreak\cccolorbox{#1}}\fi\next}
\def\ccolorbox#1#2{\fboxsep0pt\cccolorbox{#1}#2\relax}
\def\!#1{\ifx#1\ccolorbox\allowbreak\expandafter\ccolorbox\else
\ifx#1\end\expandafter\expandafter\expandafter\end\else
#1\allowbreak\expandafter\expandafter\expandafter\!\fi\fi}
\def\cccolorbox#1#2{\ifx#2\relax\let\next\allowbreak\else
\def\next{\colorbox{#1}{\strut #2}\allowbreak\cccolorbox{#1}}\fi\next}
\usepackage{framed}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{listings}
\usepackage{lstlinebgrd}
\usepackage{hanging}
\lstset{columns=fullflexible,
basicstyle=\small,
breakatwhitespace=false,
breaklines=true,
captionpos=b,
commentstyle=\color{ForestGreen},
% frame=single,
extendedchars=true,
keywordstyle=\color{blue},
language=Python,
numbers=left,
numbersep=5pt,
numberstyle=\scriptsize\color{gray},
rulecolor=\color{black},
showspaces=false,
showstringspaces=false,
showtabs=false,
stepnumber=1,
stringstyle=\color{purple},
tabsize=2,
title=\lstname,
belowskip=-1em,
aboveskip=1em,
belowcaptionskip=0em,
}
\begin{document}
First example
\begin{framed}
\begin{lstlisting}[escapechar=!]
! \ccolorbox{blue!35}{dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-} !
\end{lstlisting}
\end{framed}
second example
\begin{framed}
\begin{lstlisting}[escapechar=!]
dummycode
!\colorbox{blue!35}{\parbox{0.95\textwidth}{\begin{hangparas}{.25in}{1}dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-dummycode-\end{hangparas}}}!
\end{lstlisting}
\end{framed}
\end{document}