清单:替换遗漏的代码

清单:替换遗漏的代码

是否可以省略一些代码并标记这一点,而不是仅包含部分代码(这是几乎相同)?

我希望输出结果与第二个和第三个列表的组合非常相似。垂直线(两个列表之间)可能是虚线或虚线,或者被两条短线划掉。此外,...可能还有另一个“替换文本/符号”

软件包中是否包含类似的东西?或者可以(轻松)实现吗?

\documentclass{article}

\usepackage{listings}

\begin{document}

whole code in \texttt{filepath}:
\begin{lstlisting}[numbers=left,frame=single,language=pascal]
for i:=maxint to 0 do
begin
    very
    much
    code
end;

Write(Case insensitive);
WritE(Pascal keywords.);
\end{lstlisting}

%real usage:
%\lstinputlisting[firstnumber=1,linerange={1-2,6-9}]{filepath}

\vspace{5cm}

%almost desired output
\begin{lstlisting}[numbers=left,frame=tlr,language=pascal]
for i:=maxint to 0 do
begin
\end{lstlisting}...
\begin{lstlisting}[firstnumber=6,numbers=left,frame=blr,language=pascal]
end;

Write(Case insensitive);
WritE(Pascal keywords.);
\end{lstlisting}

\end{document}

答案1

我在论文(附录)中做了类似的事情。我故意打破了省略代码的列表,并在普通文本中加入了解释。否则,省略号看起来就像是代码的一部分。请注意,代码设置得非常紧凑,垂直和水平(拉丁现代 tt 压缩)。

    The Python code set out in this section was written for finding the peaks from the \mbox{GaN} and \AlGaN{x} epilayers of the devices studied in \rep{Chapter}{chapter}~\ref{chap_IMEC}.
    It was also used to find the positions of both Raman and \acl{PL} peaks in some of the other results in this thesis. 
    \setstretch{.9}%
    \lstset{firstnumber=auto}%
    \lstinputlisting[caption={},linerange={1-73}]{Appendices/PeakPick.py}%
    \vspace{-1.3\baselineskip}{\footnotesize $\scriptstyle\vdots\quad$ The setup routine writes a default configuration file.}\vspace{-.3\baselineskip}
    \lstinputlisting[caption={},firstnumber=118,firstline=118,lastline=377]{Appendices/PeakPick.py}%
    \vspace{-1.3\baselineskip}{\footnotesize $\scriptstyle\vdots\quad$ User options are read from the command line and configuration file.}\vspace{-.3\baselineskip}
    \lstinputlisting[caption={},firstnumber=431,linerange={431-514}]{Appendices/PeakPick.py}%
    \vspace{-0.5\baselineskip}%

答案2

我猜测,没有内置功能,因为到现在为止,还没有人想到这个。

服用克里斯·H的回答,我得到了:(我认为,这可以做得更好,所以大家可以随意提供建议)

\documentclass{article}

\usepackage{listings}
\usepackage{tikz}

\lstset{numbers=left}

\begin{document}

text
\lstinputlisting[belowskip=0pt,frame=tlr,linerange={1-3}]{filepath}%
\hspace{-3.4pt}\begin{tikzpicture}
\draw[dotted] (0,0)-- (0,1.1em);
\end{tikzpicture} \hspace{\linewidth}\hspace{1.7pt}
\begin{tikzpicture}
\draw[dotted] (0,0) -- (0,1.1em);
\end{tikzpicture}
\lstinputlisting[aboveskip=0pt,belowskip=0pt,frame=lr,firstnumber=7,firstline=7,lastline=17]{filepath}%
\hspace{-3.4pt}\begin{tikzpicture}
\draw[dotted] (0,0)-- (0,1.1em);
\end{tikzpicture} \hspace{\linewidth}\hspace{1.7pt}
\begin{tikzpicture}
\draw[dotted] (0,0) -- (0,1.1em);
\end{tikzpicture}
\lstinputlisting[aboveskip=0pt,frame=blr,firstnumber=20,linerange={20-22}]{filepath}%
text

\end{document}

答案3

基于建造者的答案,我想出了以下无 tiks 的一体化解决方案。当然,您也可以使用 tikz。;-)

\makeatletter
\def\lst@MSkipToFirst{%
    \global\advance\lst@lineno\@ne
    \ifnum \lst@lineno=\lst@firstline
    \def\lst@next{\lst@LeaveMode \global\lst@newlines\z@
        \lst@OnceAtEOL \global\let\lst@OnceAtEOL\@empty
        \ifnum \c@lstnumber>0 %
            \vspace{1 pt}\newline\hspace*{-3em}\hbox to 2cm{\leaders\hbox to 10pt{\hss - \hss}\hfil}\vspace{1 pt} %
        \fi
        \lst@InitLstNumber % Added to work with modified \lsthk@PreInit.
        \lsthk@InitVarsBOL
        \c@lstnumber=\numexpr-1+\lst@lineno % this enforces the displayed line numbers to always be the input line numbers
        \lst@BOLGobble}%
    \expandafter\lst@next
    \fi}
\makeatother

该图显示了以下片段(版权和其他内容):

\lstinputlisting[linerange={68-72,76-76,80-80,84-84,88-88,92-93}]{my-cfile.c}

行间距示例

相关内容