15

This here is actually some kind of isolated follow-up question to Workaround for open lualatex bug in \scantokens? .

I tried to use tex.print("<some tex macro>{<something with a newline inside>}") and expected it to work as if I would have typed the content of tex.print in TeX directly.

More precisely, I tried to write

\pgfplotstabletypeset{A B
2 3
4 5
}

inside of tex.print. The entire motivation is in the linked question. No matter what I tried, I could not get the same treatment of the (mandatory) newline characters as if I typed them in TeX.

So, the question is: how can I use tex.print from Lua in order to get the same effect as if I wrote the stuff directly into TeX?

Here is a list of my attempts. Perhaps I overlooked something more or less obvious. You see that I started with something trivial (printing two empty lines to generate a paragraph) to see how this is handled:

\documentclass{article}

\usepackage{pgfplotstable}

\begin{document}
\parskip=20pt
\parindent=0pt

\expandafter\ifx\csname directlua\endcsname \relax \else
   \directlua {tex.enableprimitives('',tex.extraprimitives ())}
   % 
   % using tex.print(42, ...) does not make a difference:
   %\savecatcodetable 42
\fi

This is what I would like to generate -- from within LUA:

\pgfplotstabletypeset{A B
2 3
4 5
}

FAIL:
\def\n{\noexpand\n}
Here is a lua paragraph\directlua{tex.print("\n\n")}And here is the rest.

GOOD:
Here is a lua paragraph\directlua{tex.print("", "")}And here is the rest.

FAIL:
Here is a lua paragraph\directlua{tex.sprint("", "")}And here is the rest.

FAIL:
\directlua{
    % FAILS: the newline appears to have some strange catcode- and is
    % reported in a different way
    %tex.print("\noexpand\\pgfplotstabletypeset{A B\n 2 3\n 4 5\n}")
}

FAIL:
\directlua{
    % FAILS: the newline appears to have some strange catcode- and is
    % reported in a different way
    %tex.sprint("\noexpand\\pgfplotstabletypeset{A B\n 2 3\n 4 5\n}")
}

FAIL:
\directlua{%
    %
    % FAILS: the curly brackets do not match, it interpretes
    % \pgfplotstabletypeset{A B
    %
    % as first statement and does not see the rest
    %tex.print("\noexpand\\pgfplotstabletypeset{A B", "2 3", "4 5}")
}

FAIL:
\directlua{%
    %
    % FAILS: the curly brackets do not match, it interpretes
    % \pgfplotstabletypeset{A B
    %
    % as first statement and does not see the rest
    %tex.sprint("\noexpand\\pgfplotstabletypeset{A B", "2 3", "4 5}")
}

\end{document}

I uncommented the items which produce compile failures.

It should be noted that \pgfplotstabletypeset redefines the category code of newline to be 12 (other) and then it does its operation. I suppose this is what caused the problem (in conjunction with Lua's catcodetables).

enter image description here

Henri Menke
  • 109,596

1 Answers1

9

A single call to tex.print produces a single line of input, so you must call it once per line. For instance, to get two newline markers, do

Here is a Lua paragraph\directlua{tex.print("") tex.print("")}And here is another one.

Issuing \tracingall before these lines shows that indeed there is a single \par, and not two.

The same is true for the table:

\documentclass{article}
\usepackage{pgfplotstable}
\begin{document}
\directlua{tex.print("\string\\pgfplotstabletypeset{A B")
  tex.print("2 3")tex.print("4 5")tex.print("}")}
\end{document}