5 使用 listings 宏包排版代码的时候,如何在行号和代码块之间加上一条竖线以区分代码和行号?

发布于 2023-02-05 23:56:30

请问一下使用 listings 宏包排版代码的时候,有什么办法在行号和代码块之间加上一条竖线以区分代码和行号,如下图的绿线所示。
2023-02-05_234839.png
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}

查看更多

关注者
0
被浏览
852
1 个回答
LaTeXStudio
LaTeXStudio 2023-02-06
这家伙很懒,什么也没写!

看看 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} 

效果:
image.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览