请问一下使用 listings 宏包排版代码的时候,有什么办法在行号和代码块之间加上一条竖线以区分代码和行号,如下图的绿线所示。
MWE.tex 如下。
\documentclass[12pt,UTF8]{ctexart}
\usepackage{listings}
\usepackage{color}
\begin{document}
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}
\lstdefinestyle{mystyle}{
basicstyle=\footnotesize\ttfamily,
backgroundcolor=\color{backcolour},
language=c++,
firstnumber=10000,
numbers=left,
xleftmargin=40pt,
framexleftmargin=40pt,
numbersep=10pt,
}
\lstset{style=mystyle}
\lstinputlisting{./src/code_sample.cpp}
\end{document}
看看 listings 文档,用 frame = L
就是双线了。颜色自己查查手册了。示例如下:
\RequirePackage{filecontents}
\begin{filecontents*}{\jobname.c}
(function (__global) {
var tmp0, tmp1,....;
tmp13 = function (a) {
var tmp14;
tmp14 = a;
return tmp14;
};
...
tmp16 = function (flag) {
...
tmp20 = tmp21 == tmp22;
if (tmp20) {
tmp26 = 'x';
tmp24 = __global[tmp26];
tmp25 = 1;
tmp23 = tmp24 * tmp25;
return tmp23;
}
...
tmp7 = tmp16(tmp10, tmp11);
...
}(typeof global === 'undefined' ? this : global));
\end{filecontents*}
\documentclass{scrartcl}
\usepackage{listings}
\usepackage{xcolor}
\lstdefinelanguage{HTML5}{
sensitive=true,
keywords={%
% JavaScript
typeof, new, true, false, catch, function, return, null, catch, switch, var, if, in, while, do, else, case, break,
% HTML
html, title, meta, style, head, body, script, canvas,
% CSS
border:, transform:, -moz-transform:, transition-duration:, transition-property:,
transition-timing-function:
},
% http://texblog.org/tag/otherkeywords/
otherkeywords={<, >, \/},
%ndkeywords={class, export, boolean, throw, implements, import, this},
comment=[l]{//},
% morecomment=[s][keywordstyle]{<}{>},
morecomment=[s]{/*}{*/},
morecomment=[s]{<!}{>},
morestring=[b]',
morestring=[b]",
alsoletter={-},
alsodigit={:}
}
\lstset{%
% Basic design
%backgroundcolor=\color{white},
basicstyle={\ttfamily},
frame=L,
% Line numbers
xleftmargin={0.75cm},
numbers=left,
stepnumber=1,
firstnumber=1,
numberfirstline=true,
% Code design
identifierstyle=\color{black},
keywordstyle=\bfseries,
ndkeywordstyle=\color{blue}\bfseries,
stringstyle=\color{black}\ttfamily,
commentstyle=\color{darkgray}\ttfamily,
% Code
language={HTML5},
tabsize=2,
showtabs=false,
showspaces=false,
showstringspaces=false,
extendedchars=true,
breaklines=true
}
\begin{document}
\lstinputlisting{\jobname.c}
\clearpage
The following lines lines shows how to~\dots
\lstinputlisting[firstline=1,lastline=7]{\jobname.c}
The following lines shows that \dots
\lstinputlisting[firstline=9,firstnumber=10]{\jobname.c}
\end{document}
效果:
为什么要用双线呀?可能我表述不到位。我重新表述一下图片所表达的内容:
(1)在行号和代码块之间加上一条【单竖线】(粗细不用管)以区分代码和行号;
(2)行号和置入的代码作为一个整体,衬以浅色背景(即行号也要在背景色范围内)。