2

I am wondering if package listings has a feature that can skip a few lines and substitute them with some dots, to produce something like the following

1 | error_temp = error - ((numpy.linalg.norm(b, 'fro')) ** 2.0)

... | ...

5 | error -= (numpy.linalg.norm(b[j, :])) ** 2.0

I would like to be able to specify a few lines and ask listings to substitute them with dots and also print a few dots as its line number to indicate that it is skipping a few lines here. Also, when the skipping is over, resume to the actual line number as if the skipped lines were not skipped.

Here is my minimal working example, which might not help though

\documentclass{article}
\usepackage{listings}
\lstset{breaklines = true, frame = trBL, tabsize = 4, basicstyle = \small \ttfamily, keywordstyle = \color{blue}, stringstyle = \color{red}, rulecolor = \color{black}, numbers = left, numberstyle = \tiny}

\begin{document}

\begin{lstlisting}[language = Python, escapeinside = {(@}{@)}] error_temp = error - ((numpy.linalg.norm(b, 'fro')) ** 2.0) (@ \cdots @) error -= (numpy.linalg.norm(b[j, :])) ** 2.0 \end{lstlisting}

\end{document}

The problem with the code is, it would not skip line numbers and it can not print dots as line number for skipped lines.

Please let me know if it is not clear of what I am trying to do, thanks in advance!

zyy
  • 2,146

1 Answers1

5

With some modification of this answer, following code maybe used.

\documentclass{article}
\usepackage{listings,xcolor}
\lstset{breaklines = true, frame = trBL, tabsize = 4, basicstyle = \small \ttfamily, keywordstyle = \color{blue}, stringstyle = \color{red}, rulecolor = \color{black}, numbers = left, numberstyle = \tiny}

\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
  \lst@AddToHook{OnNewLine}{%
    \let\thelstnumber\relax%
  }%
}

\newcommand*\Reactivatenumber{%
  \lst@AddToHook{OnNewLine}{%
   \let\thelstnumber\origthelstnumber%
  }%
}
\makeatother

\begin{document}

\begin{lstlisting}[language = Python,escapeinside=||]
error_temp = error - ((numpy.linalg.norm(b, 'fro')) ** 2.0)|\Suppressnumber|
 ... 
 ...
 ...|\Reactivatenumber|
error -= (numpy.linalg.norm(b[j, :])) ** 2.0
\end{lstlisting}

\end{document}

enter image description here

ADDENDUM: Line numbers to indicate it is skipping lines.

\documentclass{article}
\usepackage{listings,xcolor}
\lstset{breaklines = true, frame = trBL, tabsize = 4, basicstyle = \small \ttfamily, keywordstyle = \color{blue}, stringstyle = \color{red}, rulecolor = \color{black}, numbers = left, numberstyle = \tiny}

\let\origthelstnumber\thelstnumber
\makeatletter
\newcommand*\Suppressnumber{%
  \lst@AddToHook{OnNewLine}{%
    \let\thelstnumber\relax%
  }%
}

\newcommand*\Reactivatenumber{%
  \lst@AddToHook{OnNewLine}{%
   \let\thelstnumber\origthelstnumber%
  }%
}
\makeatother

\begin{document}

\begin{lstlisting}[language = Python,escapeinside=||]
error_temp = error - ((numpy.linalg.norm(b, 'fro')) ** 2.0)|\Suppressnumber|
 |\thelstnumber|
 |\thelstnumber|
 |\thelstnumber||\Reactivatenumber|
error -= (numpy.linalg.norm(b[j, :])) ** 2.0
error -= (numpy.linalg.norm(b[j, :])) ** 2.0
\end{lstlisting}

\end{document}

enter image description here

  • Thanks for your answer! Is it possible to write dots as line numbers to indicate it is skipping lines? – zyy Feb 25 '19 at 22:12
  • @zyy, your welcome. Question in your comment requires another effort. Some of them have already started to vote for closing because of duplicate. Your question will probably close. If the question does not close and I have time, I can work on it. –  Feb 25 '19 at 22:40