Markus
Markus

Reputation: 71

using initial line numbering with listings package in latex

Is there the posibility to use the initial line numbering from the code I want to list in my latex file? So the code should be numbered like in the picture:

enter image description here

But using the following code, the numbering starts from 1

\lstinputlisting[language=C, 
    numbers=left,
    numberfirstline=false,
    keywordstyle=\color{blue}, 
    commentstyle=\color{green}, 
    stringstyle=\color{red},
    firstline=222, lastline=228]{Test_Fehlerbehebung_LIN.can}   

enter image description here

And why is the first number (1) obove the first row? Shouldn't numberfirstline=false fix this problem?

This ist the minimal example:

\documentclass[]{scrreprt}

\usepackage{colortbl}
\usepackage{listings}
\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}{
    %backgroundcolor=\color{backcolour},   
    commentstyle=\color{codegreen},
    keywordstyle=\color{magenta},
    numberstyle=\tiny\color{black},
    stringstyle=\color{codepurple},
    basicstyle=\ttfamily\footnotesize,
    breakatwhitespace=false,         
    breaklines=true,                 
    captionpos=b,                    
    keepspaces=true,                 
    numbers=left,                    
    numbersep=5pt,                  
    showspaces=false,                
    showstringspaces=false,
    showtabs=false,                  
    tabsize=2,
    firstnumber=auto
}

\begin{document}
\lstset{basicstyle=\footnotesize, style=mystyle}
\lstinputlisting[language=C]{Code/Test_Fehlerbehebung_LIN.c}
\end{document}   

Upvotes: 1

Views: 1778

Answers (1)

You can use the same fix as in https://tex.stackexchange.com/a/27240/36296

\documentclass[]{scrreprt}

\usepackage{colortbl}
\usepackage{listings}
\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}{
    numbers=left,                    
}

\usepackage{etoolbox}
\makeatletter
\patchcmd{\lst@GLI@}% <command>
  {\def\lst@firstline{#1\relax}}% <search>
  {\def\lst@firstline{#1\relax}\def\lst@firstnumber{#1\relax}}% <replace>
  {\typeout{listings firstnumber=firstline}}% <success>
  {\typeout{listings firstnumber not set}}% <failure>
\makeatother


\begin{document}
\lstset{basicstyle=\footnotesize, style=mystyle}
\lstinputlisting[language=C,firstline=10, lastline=20,firstnumber=firstline]{document.tex}
\end{document}   

Upvotes: 1

Related Questions