更改代码清单中的行号

更改代码清单中的行号

下面是代码清单现在的样子: 在此处输入图片描述

我想将第 8 行更改为空白的,并将第 9 行改为10。原因是它们实际上是在...(第 8 行)的两行,我该怎么做?

以下是序言

\usepackage{upquote}
\usepackage{listings}
\usepackage{pxfonts}
\makeatletter
\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
}
\makeatother

以下是代码清单

\begin{lstlisting}
(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{lstlisting}

答案1

好吧,也许将第 8 行扩展为第 8 行和第 9 行并分别包含它们会更容易...

软件包listings提供了一些选项,您可以使用它们来更改行号。使用选项firstline和,lastline您可以定义打印列表中显示的行,使用和firstnumberlastnumber您可以定义显示的行号...

梅威瑟:

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

Complete listing:
\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}

结果:

mwe 第二页

答案2

我发现了同样的问题,并想出了这个解决方案:使用转义序列并修改名为的计数器lstnumber。例如,以下 XeLaTeX 文档:

\documentclass{article}
\usepackage{polyglossia}
\usepackage{listings}
\lstset{language=Python, 
numbers=left, 
escapechar=@, 
flexiblecolumns}

\begin{document}
My two cents. I hope it is not too late :-)
\begin{lstlisting}
    from __future__ import annotations
    
    
    class círculo: @\\\hspace*{4em}\large\vdots \setcounter{lstnumber}{32}@
        def __eq__(self, otro: object) -> bool:
            if isinstance(otro, círculo):
                return self.radio == otro.radio
            else:
                raise TypeError
    \end{lstlisting}
\end{document}

得出以下结果: 在此处输入图片描述

相关内容