1

I would like to print in a .tex file the number of lines of some other file (not the content, just how many lines).

For example in the following code I would like to have 3 print in the text

\documentclass{article}

\begin{filecontents}{test.txt}
a
b
c
\end{filecontents}

\begin{document}

Here should be the number of lines in test.txt

\end{document}
3

This prints 7 not 3 as the file has 7 lines due to comments, but:

\documentclass{article}

\begin{filecontents}{test.txt}
a
b
c
\end{filecontents}

\newread\zzz
\newcount\zzzlines
\begin{document}

Here should be the number of lines in test.txt

\zzzlines=-1
\openin\zzz=test.txt
\loop
\ifeof\zzz
\else
\read\zzz to \zzztmp
\advance\zzzlines 1
\repeat
\the\zzzlines

\end{document}

test.txt is

%% LaTeX2e file `test.txt'
%% generated by the `filecontents' environment
%% from source `bb988' on 2019/07/09.
%%
a
b
c
  • Thanks for your answer! – wasa Jul 9 at 19:54
1

With the help of knitr

\documentclass{article}
\begin{document}
\verb|test.txt| have \Sexpr{length(readLines("test.txt"))} lines
\end{document}

Will produce:

test.txt have 3 lines

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.