# Possible to run a handler / callback function, when a macro (\inputlineno) changes value/definition?

I was thinking about the debugging procedure I went through in How come the tilde ~ sometimes does not behave like non-breaking space? - basically, to find out where did the macro ~ change, I had to manually insert logging commands on each line of code, like this:

% https://tex.stackexchange.com/questions/81789/get-current-source-line-number/81794#81794
\def\showLineMean{\typeout{line \the\inputlineno; MEANING: \meaning~}}
...
\usetikzlibrary{calc} \showLineMean                    % l.61
\usetikzlibrary{decorations.markings} \showLineMean    % l.62
\usepackage{txfonts} \showLineMean % Times font ...    % l.63
\renewcommand{\ttdefault}{pcr} \showLineMean           % l.64
...


Of course, this can be somewhat tedious, especially if you have a large document, with no apparent places in code to focus on.

So, I was thinking - since there already is a token called \inputlineno which changes its value of each line of code, I thought it would be much easier if I could say something like: "on each new line of source, typeout the \meaning of ~"; maybe through something like this pseudocode:

\ontokenchange{\inputlineno}{\typeout{line \the\inputlineno; MEANING: \meaning~}}


... so I could insert this at relevant places in the doc, and stop it with, say, \ontokenchangestop; or even better, with an a-priori limited range:

\ontokenchange[startval=100,stopval=130]{\inputlineno}{\typeout{line \the\inputlineno; MEANING: \meaning~}}


... which (assuming \inputlineno is monotonically increasing) would start the tracing at line 100, and stop it when the process hits line 130.

So, I'm interested in this in a Latex setting; however, \inputlineno seems to be tex core, as it is apparently defined in tex.web:

$grep -B1 -A1 -n 'input.*line.*no' tex.web 8405-And the |last_item| command is modified by either |int_val|, |dimen_val|, 8406:|glue_val|, |input_line_no_code|, or |badness_code|. 8407- 8408:@d input_line_no_code=glue_val+1 {code for \.{\\inputlineno}} 8409-@d badness_code=glue_val+2 {code for \.{\\badness}} -- 8431-@!@:last_skip_}{\.{\\lastskip} primitive@> 8432:primitive("inputlineno",last_item,input_line_no_code); 8433:@!@:input_line_no_}{\.{\\inputlineno} primitive@> 8434-primitive("badness",last_item,badness_code); -- 8448- glue_val: print_esc("lastskip"); 8449: input_line_no_code: print_esc("inputlineno"); 8450- othercases print_esc("badness") -- 8507- 8508:We also handle \.{\\inputlineno} and \.{\\badness} here, because they are 8509-legal in similar contexts. -- 8512-if cur_chr>glue_val then 8513: begin if cur_chr=input_line_no_code then cur_val:=line 8514- else cur_val:=last_badness; {|cur_chr=badness_code|}  Judging by this, I cannot really set up a handler that would execute when \inputlineno changes (hell, I'm so poor at reading this code, I can't even find the line where it actually does change :)); but I thought I'd ask the community just to make sure: would there otherwise be a way to set up such a handler - maybe by using some external package? (this is my excuse for using the tag "packages" on this post) Finally, I also tried a little hack to replace the \inputlineno from Latex; consider this tiny MWE (say, test.tex): \documentclass{article} \let\oldinputlineno\inputlineno \def\inputlineno{\oldinputlineno; MEANING: \meaning~} \begin{document} \title{Test title} \author{test} \maketitle \end{document}  If we run it, we get: $ pdflatex test.tex && grep -r MEANING test.log
This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011)
...
Transcript written on test.log.
LaTeX Font Info:    ... okay on input line 13; MEANING: macro:->\nobreakspace {
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 13; MEANING:
LaTeX Font Info:    ... okay on input line 13; MEANING: macro:->\nobreakspace {
(Font)              <12> on input line 18; MEANING: macro:->nobreakspace {}.
(Font)              <8> on input line 18; MEANING: macro:->nobreakspace {}.
(Font)              <6> on input line 18; MEANING: macro:->nobreakspace {}.


... which means, we can somewhat hook into \inputlineno - but it only makes a difference when it, as a macro, is called / executed - which typically (apparently) happens in a \typeout context, through the use of \the\inputlineno. What I'd like instead, is that the "callback" runs every time \inputlineno changes its value (that is, when a new source line is being processed).

Are there any options for doing this (primarily with pdflatex)?

• No, you can't hook in the code for detecting when \inputlineno changes. Maybe it can be done in LuaTeX. Jun 22, 2014 at 22:02
• Thanks for that, @egreg - good to have that explicitly confirmed. Cheers! Jun 22, 2014 at 22:10

With pdfTeX or XeTeX you can't hook into the code for detecting when \inputlineno changes. It's just a read only integer parameter that's updated in a part of the program the user hasn't access to.
Possibly the reader function in LuaTeX, described in section 4.1.2.1.1, might help with this engine.