
我想進行一個lstlisting
樣式定義,其中字串以背景顏色突出顯示,以使它們更加突出。我什至不確定這是否可能,但讓我解釋一下。
這是一次嘗試\colorbox
,可能會讓您了解我想要什麼:
\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\lstdefinestyle{mypython}{
keywordstyle=\bfseries,
captionpos=b,
language=Python,
keywordstyle=\bfseries,
identifierstyle=\color{black},
ndkeywordstyle=\color{darkgray}\bfseries,
commentstyle=\color{gray}\ttfamily,
stringstyle=\color{black}\ttfamily\colorbox{black!10},
showstringspaces=false
}
\begin{document}
\begin{lstlisting}[style=mypython]
{'timezone': 7200, 'cod': 200, 'dt': 1561470780, 'base': 'stations',
'weather': [{'id': 804, 'icon': '04d', 'main': 'Clouds',
'description': 'overcast clouds'}],
'sys': {'message': 0.0076, 'country': 'DK', 'sunrise': 1561429582,
\end{lstlisting}
\end{document}
可以看出,這種方法有幾個問題:
- 撇號與文字的其餘部分分開突出顯示
- 空白是在行開頭突出顯示的一些地方,其中字串尚未開始
- 色框的高度不相等
- 在單字和撇號之間引入了額外的間距
是否可以解決以上問題?也許使用tcolorbox
或其他什麼\colorbox
?