我有很多代码在lstlisting环境中。如何在该环境中突出显示(着色背景)特定的代码块?
\usepackage{color}
\definecolor{light-gray}{gray}{0.80}
然后在你的文档中像这样使用它: \begin{lstlisting}[escapechar=!]
def mult(m: Matrix[Int], n: Matrix[Int]) {
val p = !\colorbox{light-gray}{new MatrixInt}!(m.rows, n.cols)
}
\end{lstlisting}
colorbox
中失去了语法高亮。 - Julian\begin{frame}[fragile]
将帧标记为脆弱以使其正常工作。 - MKroehnert\definecolor{light-green}{green}{0.80}
时,出现了“latex undefined color model”错误。这就是为什么我改用\definecolor{lightgreen}{rgb}{0.8,1.0,0.8}
的原因。希望能对大家有所帮助。 - Michael虽然有点繁琐,但是你可以将代码分成几个lstlisting
环境。
\begin{lstlisting}
line
\end{lstlisting}
\vspace{-\baselineskip}
\begin{lstlisting}[backgroundcolor=\color{pink}]
very
interesting
\end{lstlisting}
\vspace{-\baselineskip}
\begin{lstlisting}
line
line
\end{lstlisting}
firstnumber
关键字来修复这个问题。在这个例子中,你可以在第二个和第三个lstlisting
环境中分别使用firstnumber=2
和firstnumber=4
。使用firstnumber=last
应该从前一个lstlisting
环境继续编号,但是当我尝试时,结果偏差了一个。 - Anton Geraschenko\begin{lstlisting}[name=asdf, ...
)。名称不会显示出来,具有相同名称的“lstlisting”环境默认共享一个行计数器。 - Anton Geraschenkonewcommand
或newenvironment
中吗? - Julian\vspace{-\baselineskip}
对我来说无法抑制每个块之间的间隔。根据需要设置选项 aboveskip=0
和 belowskip=0
可以解决问题。 - Gohutikz
来突出显示(部分)单行的解决方案:\documentclass[pdftex,11pt,a4paper]{article}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{tikzmark}
% Command to place a TikZ anchor at the current position
\newcommand{\mytikzmark}[1]{%
\tikz[overlay,remember picture,baseline] \coordinate (#1) at (0,0) {};}
\newcommand{\highlight}[2]{%
\draw[yellow,line width=14pt,opacity=0.3]%
([yshift=4pt]#1) -- ([yshift=4pt]#2);%
}
\begin{document}
\begin{lstlisting}[escapechar=@, language=C]
@\mytikzmark{hl1Start}@struct@\mytikzmark{hl1End}@ S {
double @\mytikzmark{hl2Start}@salary_@\mytikzmark{hl2End}@;
};
\end{lstlisting}
\begin{tikzpicture}[remember picture, overlay]
\highlight{hl1Start}{hl1End}
\highlight{hl2Start}{hl2End}
\end{tikzpicture}
\end{document}
lstlisting
的语法高亮显示。有一个包叫做lstlinebgrd
可以完成这个功能。
% !TEX encoding = UTF-8 Unicode
% !TEX TS-program = xelatex
\documentclass{article}
\usepackage[a4paper]{geometry}
\usepackage{lstlinebgrd}
\usepackage{listings, xcolor}
\lstset{tabsize = 4, showstringspaces = false, breaklines = true, numbers = left, numberstyle = \tiny, basicstyle = \small \ttfamily, keywordstyle = \color{blue}, stringstyle = \color{red}, commentstyle = \color{green}, rulecolor = \color{black}}
\begin{document}
\begin{lstlisting}[language = python, frame = tRBl, basicstyle = \ttfamily \scriptsize, linebackgroundcolor = {\ifnum \value{lstnumber} = 8 \color{yellow} \fi, \ifnum \value{lstnumber} = 10 \color{yellow} \fi, \ifnum \value{lstnumber} = 12 \color{yellow} \fi}, linebackgroundsep = 2.2 em, linebackgroundwidth = 15 em]
import numpy
from tensorflow.keras.layers import Dense, Activation, Dropout, Input
from tensorflow.keras.models import Sequential, Model, load_model
from tensorflow.keras.optimizers import Adam
model_input = Input(shape = x_train[0].shape)
x = Dense(120, activation = 'relu')(model_input)
x = Dropout(0.01)(x)
x = Dense(120, activation = 'relu')(x)
x = Dropout(0.01)(x)
x = Dense(120, activation = 'relu')(x)
x = Dropout(0.01)(x)
model_output = Dense(numpy.shape(y_train)[1])(x)
model = Model(model_input, model_output)
\end{lstlisting}
\end{document}
而你获得
然而,它仍未经过优化,您需要手动调整高亮条的左右边缘,并且设置多行高亮是繁琐的。lstlinebgrd
目前存在问题 https://tex.stackexchange.com/questions/451532/recent-issues-with-lstlinebgrd-package-with-listings-after-the-latters-updates - Supernormal列表包提供了backgroundcolor = \ color {}选项,但我确定这会使整个背景色而不是一部分。
您可以尝试将其放在带有颜色的parbox中,或者使用colorbox包。
lstlinebgrd
的包可以实现这个功能,唯一的注意点是它不能自动读取文本长度。 - zyy