因此,我正在使用 listing 包来风格化源代码(我目前正在使用 solarized 配色方案)。
我已设置一个框架,使行号具有彩色背景。我不希望此框架显示在代码底部,但我希望显示边框(左边框和下边框,但仅显示左框架)。
样式代码如下(以及来自清单文档的一些虚拟代码)。这用于多种语言,因此我省略了选择language
器。
\documentclass[a4paper,11pt]{article}
\usepackage[top=19mm, left=12.925mm, right=12.925mm, bottom=19mm]{geometry}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{xcolor-solarized}
\lstdefinestyle{mystyle}{
basicstyle = \ttfamily\color{solarized-base00}, % -- Making the font monospaced and small
backgroundcolor=\color{solarized-base3}, % -- Setting the background colour to solarized-base3
breaklines=true, % -- Allows automatic line breaking
captionpos=b, % -- Caption position at bottom of listing
commentstyle = \color{solarized-base1}, % -- Sets comments to solarized-green
escapeinside = {\%*}{*}, % -- Useful for adding LaTeX within the code
emphstyle =\bfseries\color{solarized-red}, % -- Emphasized text style to red
frame=l, % -- Adds a line to the left of the listings, which is used for line numbers.
framesep=2em, % -- Frame seperator size
framerule=1pt, % -- Frame line size
fillcolor=\color{solarized-base2}, % -- Frame fill colour
identifierstyle=\color{solarized-base00}, % -- Style for Identifiers
keepspaces=true, % -- Keeps Spaces in text, useful for keeping identation of code.
keywordstyle = \color{solarized-cyan}\bfseries, % -- Keyword style
numbers=left, % -- Line numbers on the left
numbersep=0.5em, % -- How far away should line numbers be from code
numberstyle=\normalfont\footnotesize\color{solarized-violet}, % -- Line Number font style, size and colour
rulecolor=\color{solarized-blue}, % -- if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (solarized-blue here))
showspaces=false, % -- Do not show spaces everywhere by adding particular underscores
showtabs=fase, % -- Do not show tabs within strings adding particular underscores
stringstyle=\color{solarized-blue}, % -- String style
tabsize=2, % -- Setting the tab size to 2 spaces.
xleftmargin=2em % -- Margin from left of listing
}
\begin{document}
\begin{lstlisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{lstlisting}
\end{document}
我尝试过搜索,也尝试过对我的代码进行许多不同的调整。基本上,这些解决方案都不能解决我的问题。如果您需要更多信息,请告诉我。
答案1
原来我睡不着觉。如约而至,这里有一个使用tcolorbox
和 的版本listings
。
\documentclass[a4paper,11pt]{article}
\usepackage[top=19mm, left=12.925mm, right=12.925mm, bottom=19mm]{geometry}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{xcolor-solarized}
\lstdefinestyle{mystyle}{
basicstyle = \ttfamily\color{solarized-base00}, % -- Making the font monospaced and small
%backgroundcolor=\color{solarized-base3}, % -- Setting the background colour to solarized-base3
breaklines=true, % -- Allows automatic line breaking
captionpos=b, % -- Caption position at bottom of listing
commentstyle = \color{solarized-base1}, % -- Sets comments to solarized-green
escapeinside = {\%*}{*}, % -- Useful for adding LaTeX within the code
emphstyle =\bfseries\color{solarized-red}, % -- Emphasized text style to red
identifierstyle=\color{solarized-base00}, % -- Style for Identifiers
keepspaces=true, % -- Keeps Spaces in text, useful for keeping identation of code.
keywordstyle = \color{solarized-cyan}\bfseries, % -- Keyword style
numbers=left, % -- Line numbers on the left
numbersep=0.5em, % -- How far away should line numbers be from code
numberstyle=\normalfont\footnotesize\color{solarized-violet}, % -- Line Number font style, size and colour
showspaces=false, % -- Do not show spaces everywhere by adding particular underscores
showtabs=fase, % -- Do not show tabs within strings adding particular underscores
stringstyle=\color{solarized-blue}, % -- String style
tabsize=2, % -- Setting the tab size to 2 spaces.
}
\newcommand\sideline{
\draw[line width=2em,solarized-base2]
([xshift=1em+1pt]frame.north west) --
([shift={(1em+1pt,1pt)}]frame.south west);
}
\usepackage[most]{tcolorbox}
\newtcblisting{mylisting}{%
nobeforeafter,
colback=solarized-base3,
colframe=solarized-blue,boxrule=0pt,leftrule=1pt,bottomrule=1pt,
boxsep=0pt,left=2em,top=-2mm,right=0pt,bottom=-2mm,arc=0pt,enhanced jigsaw,breakable,
listing options={style=mystyle},listing only,
overlay unbroken and first={\sideline},
overlay middle and last={\sideline}
}
\newtcbinputlisting{\myinputlisting}[2][]{%
listing file={#2},
nobeforeafter,
colback=solarized-base3,
colframe=solarized-blue,boxrule=0pt,leftrule=1pt,bottomrule=1pt,
boxsep=0pt,left=2em,top=-2mm,right=0pt,bottom=-2mm,arc=0pt,enhanced jigsaw,breakable,
listing options={style=mystyle},listing only,
overlay unbroken and first={\sideline},
overlay middle and last={\sideline},
#1}
\usepackage{filecontents}
\begin{filecontents*}{myfile.txt}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{filecontents*}
\begin{document}
\begin{mylisting}
for i:=maxint to 0 do
begin
{ do nothing }
end;
Write(’Case insensitive ’);
WritE(’Pascal keywords.’);
\end{mylisting}
\myinputlisting{myfile.txt}
\end{document}