[Tex/LaTex] Get current source line number

debuggingerrorssource

How can I get the current .tex source line number? I want to use it to generate useful error messages in a package I am writing.

There are some errors that can only be detected at the end of the document, so I want to create a command that checks for them and tells at what line the error was made. To do that, I need to be able to save the source line number in the command that was misapplied.

Preferably a command that can be used like \sourcelineno in this example:

\documentclass{minimal}
\newcommand \showlineno {line \sourcelineno}

\begin{document}

You are now at \showlineno.

This paragraph starts at \showlineno, and 
some more text,
and runs until \showlineno. 

\end{document}

where the output would be

You are now at line 6.

This paragraph starts at line 8, and some more text, and runs until line 10. 

Best Answer

You can use a variation of the kernel's \on@line:

\documentclass{article}

\makeatletter
\ifnum\inputlineno=\m@ne
\let\showlineno\@empty
\else
\def\showlineno{ line \the\inputlineno}
\fi
\makeatother

\begin{document}

You are now at \showlineno.

This paragraph starts at \showlineno, and 
some more text,
and runs until \showlineno.

on input line \the\inputlineno

\end{document}
Related Question