2

Is it possible to leave out some code and mark this, instead of including only parts of the code (which is almost the same)?

I would like to have an output almost like the second and third listing in combination. The vertical lines (between the two listings) might be dotted or dashed or be crossed out by two short lines. Also instead of ... there might be an other "replacement text/symbol"

Does the package contain something like this? Or can it be achieved (easily)?

\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}
user1
  • 2,196

3 Answers3

3

I did something similar in my thesis (appendix). I deliberately broke the listing where I omitted code, and put an explanation in normal text. Otherwise it would have looked like the ellipsis was part of the code. Note that the code was set very tight, vertically and horizontally (latin modern tt condensed).

    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}%
Chris H
  • 8,705
0

I guess, that there is no build in feature, because until now, no one came up with it.

Taking Chris H's answer, I got: (I think, that this can be done better, so everyone feel free to provide suggestions)

\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}
user1
  • 2,196
0

Based on TheConstructor's Answer, I came up with the following tiks-free all-in-one solution. Of course you can use tikz, too. ;-)

\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

The image shows a snippet (copyright and stuff) of the following:

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

Sample of spaces between line-ranges

Morty
  • 211