如何获取当前 .tex 源代码行号?我想使用它在我正在编写的包中生成有用的错误消息。
有些错误只能在文档末尾检测到,所以我想创建一个命令来检查这些错误并告诉错误发生在哪一行。为此,我需要能够保存错误应用的命令中的源代码行号。
\sourcelineno
最好是可以像本例中一样使用的命令:
\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}
输出结果为
You are now at line 6.
This paragraph starts at line 8, and some more text, and runs until line 10.
答案1
您可以使用内核的变体\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}
答案2
Gonzalo 答案的 LuaLaTeX 变体,作为用法的介绍性示例\directlua
。
\documentclass{article}
\def\showlineno{
line \directlua{tex.print(tex.inputlineno)}%
}
\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}
奇怪的行号不受管理。