9

I need to refer to to the count value of the number of lines in a listing. Is there a way to do that ?

    \lstinputlisting[caption={SPARQL query used...},label={lst:sqry}]{codes/sparql_query.sparql}

I can refer to this listing in a document by using \ref{lst:sqry}, but I need to be able to automatically count the number of lines in that listing and refer to the value.

I would deeply appreciate any help.

9

The following should do the trick using counter lst@lineno. The example creates a reference after the listings that contains the number of lines:

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{listings}

\makeatletter
\newcommand*{\lstcountlabel}[1]{%
  \@bsphack
  \begingroup
    \edef\@currentlabel{\the\numexpr\the\lst@lineno-1}%
    \label{#1}%
  \endgroup
  \@esphack
}
\makeatother

\begin{document}
\lstinputlisting[
  language={[LaTeX]TeX},
  columns=flexible,
  numbers=left,
  basicstyle=\ttfamily,
  caption={The listings has \ref{lst:job} lines.},
]
{\jobname.tex}
\lstcountlabel{lst:job}
\end{document}

Result

  • Brilliant ! Thank you very much for the prompt reply. Will surely mention this help in my acknowledgement :) – Joshua Apr 18 '13 at 16:47
5

This solution uses a different approach and was written as answer for question "Listings: total line number count" that was closed too fast.

This solution uses a different approach than my other answer:

  • Also a label is used to remember the number of code lines. This variant uses package zref for this purpose.

  • The number of code lines is made available by macro \mylstlines in the caption for the listing. That saves the user from the need to invent unique label names. Counter mylstlisting identifies a listing and the value is used in the name of the label.

  • The code lines are counted via counter mylstlines by hooking into the listings hook EveryPar. This way, the value of mylstlines should not be affected by options that affect the selected line range (firstline) or that change the printed line numbers (firstnumber).

Example file:

\documentclass[a4paper]{article}
\usepackage{listings}
\usepackage{zref-base}

\makeatletter
\newcounter{mylstlisting}
\newcounter{mylstlines}
\lst@AddToHook{PreSet}{%
  \stepcounter{mylstlisting}%
  \setcounter{mylstlines}{0}%
}
\lst@AddToHook{EveryPar}{%
  \stepcounter{mylstlines}%
}
\lst@AddToHook{ExitVars}{%
  \begingroup
    \zref@wrapper@immediate{%
      \zref@setcurrent{default}{\the\value{mylstlines}}%
      \zref@labelbyprops{mylstlines\the\value{mylstlisting}}{default}%
    }%
  \endgroup
}

% \mylstlines print number of lines inside listing caption
\newcommand*{\mylstlines}{%
  \protect\zref@extract{mylstlines\the\value{mylstlisting}}{default}%
}
\makeatother

% cosmetics
\usepackage[T1]{fontenc}
\usepackage[variablett]{lmodern}
\lstset{basicstyle=\ttfamily, columns=fullflexible,
    numbers=left, stepnumber=1}

\begin{document}
  \lstlistoflistings
  \lstinputlisting[%
    language={[LaTeX]TeX},
    caption={Example with \mylstlines\ lines.},
  ]{\jobname}
\end{document}

Result

BTW [off topic]: The number of code lines in this example is the answer to another question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.