# How to print mathematical object in text mode in lualatex?

In one of the callbacks of lualatex in lua (build_filter) i print the first argument of function.

\begin{luacode}
function f(a)
tex.print(a)
end
\end{luacode}


But during compiling i get an error:

! Missing $inserted. <inserted text>$


Ok, I know,that variable a probably has characters, which use in math mode, but how can I print it in text mode?

• printing text from the buildpage_filter isn't really expected, I tried to extend your document to a test file but could not get the error that you show as the text never appeared in the document. If you provide a test file someone can easily debug it. – David Carlisle Mar 6 at 22:23
• @sheß why the edit? In general it's better to use a code block than a quote for showing error messages – David Carlisle Mar 6 at 23:22
• Thank you very mutch. Catcode -2 help me solve my problem. 🙌 – Aleksandr Kozlovskiy Mar 7 at 0:42

Please always provide a usable test file not a fragment.

buildpage_filter gets an argument one of a fixed list of strings, some of them , such as after_output include an underscore so if you insert that string into the document via tex.print then you will need to use a non default catcode table.

One possible signature of tex.print is

tex.print(<number> n, <string> s, ...)


where n is the number of the catcode table used to print the string s. There are two special catcode tables which are identified by negative numbers:

• n = -1: Uses the currently active catcode regime
• n = -2: Uses the same catcodes as \the\toks, i.e. all characters have catcode 12, expect spaces which have catcode 10.
• But what category i must use in sprint function (because,as i understoode,print function not allow me set catcode category) Or i must use some another way to change this table,to fix my error. – Aleksandr Kozlovskiy Mar 6 at 23:06
• @AleksandrKozlovskiy -2 should work, but as I say I couldn't get any text printed here to appear, why adding text to the page from this filter? – David Carlisle Mar 6 at 23:12
• @HenriMenke actually I think the manual is wrong there, catcode12 except space is like \string not like \the\toks but -2 is what is needed anyway. – David Carlisle Mar 6 at 23:16