我尝试用彩色背景突出显示源代码,但遇到了一些错误。
第一张图片仅使用列表创建,这正是我需要的。您可以在最后一张图片上看到basicstyle
未应用(仍为黑色文本颜色)。
这是 tcolorbox + 列表代码(用于生成最后一幅图像)。
\documentclass{article}
\usepackage{xcolor}
\usepackage{tcolorbox}
\tcbuselibrary
{
breakable,
listings
}
\lstset{%
language=[LaTeX]{TeX}
}
\begin{document}
\begin{tcblisting}
{
listing only,
left = 3em,
boxrule = 0.1mm,
breakable,
parbox = true,
colback = gray,
colframe = black,
listing options =
{
basicstyle = \normalsize\ttfamily\color{white},
keywordstyle = \color{white},
stringstyle = \color{green},
identifierstyle = \color{yellow},
commentstyle = \color{black},
numbers = left,
numbersep = 1em,
columns = fullflexible,
breaklines = true,
breakindent = 0pt,
tabsize = 3,
showstringspaces = false,
numberstyle = \small\color{white},
},
}
\documentclass{article}
\usepackage{xcolor}
\lstset{%
language=[LaTeX]{TeX},
basicstyle = \normalsize\ttfamily\color{white},
keywordstyle = \color{white},
stringstyle = \color{green},
identifierstyle = \color{yellow},
commentstyle = \color{black},
numbers = left,
numbersep = 1em,
columns = fullflexible,
breaklines = true,
breakindent = 0pt,
tabsize = 3,
showstringspaces = false,
numberstyle = \small\color{gray},
backgroundcolor = \color{gray},
}
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi. Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies et,
tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet magna,
vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis. Suspendisse
ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus. Aliquam tincidunt urna.
Nulla ullamcorper vestibulum turpis. Pellentesque cursus luctus mauris.
\end{document}
\end{tcblisting}
\end{document}
答案1
列表的颜色basicstyle
应该与框的文本颜色(coltext
或colupper
)相匹配,因为在休息期间会恢复框的文本颜色(以应对其他可能出现的问题)。
因此,请使用以下命令:
...
\begin{tcblisting}
{
listing only,
left = 3em,
boxrule = 0.1mm,
breakable,
parbox = true,
colback = gray,
colframe = black,
coltext = white,% <--- !!
listing options =
{
basicstyle = \normalsize\ttfamily\color{white},% <--- match
...