11

I can query and use the states of counts in lua without problem. But now I'm wondering how to at best do it with booleans. I have been looking at the token library but it seems not to allow to get and compare the content of a command. Has someone a good idea?

\documentclass[parskip=half-]{scrartcl}
\usepackage{expl3}

\ExplSyntaxOn
\bool_new:N \g_uf_some_bool
\int_new:N  \g_uf_some_int
\ExplSyntaxOff

\usepackage{luacode}
\begin{luacode}
local catlatex       = luatexbase.registernumber("catcodetable@latex")

function printifzero ()
 if tex.count["g_uf_some_int"]==0  then
  tex.sprint(catlatex,"int is zero")
 else
  tex.sprint(catlatex,"int is " .. tex.count["g_uf_some_int"])
 end
end

function printifbool ()
 -- if g_uf_some_bool  then --???
 tex.sprint(catlatex,"bool is true")
 -- else
 tex.sprint(catlatex,"bool is false")  
 --end
end

\end{luacode}
\begin{document}
\minisec{Integers}
\ExplSyntaxOn
\directlua{printifzero()}

\par
\int_gset:Nn\g_uf_some_int{42}
\directlua{printifzero()}

\minisec{booleans}
\bool_set_true:N \g_uf_some_bool
\directlua{printifbool()}
\par

\bool_set_false:N \g_uf_some_bool
\directlua{printifbool()}
\par

\ExplSyntaxOff
\end{document}

enter image description here

8
  • 1
    So that's basically how to compare a macro's value with charcode 0 or 1…
    – TeXnician
    Commented Jul 1, 2018 at 12:05
  • 1
    The fact that a true boolean is \char"1 and a false one is \char"0 (the tokens is defined/redefined with \chardef) should not be relied upon: it's just the way booleans are internally implemented and it is in principle subject to change.
    – egreg
    Commented Jul 1, 2018 at 13:18
  • 2
    @egreg I'm sure Ulrike knows that: probably what is needed is some defined Lua-side interfaces provided by the team
    – Joseph Wright
    Commented Jul 1, 2018 at 13:39
  • @JosephWright I found that there is actually a token.get_macro() but it doesn't work for booleans, but returns nil. And while there is a token.set_char() there is no token.get_char() (or if there is something usable, it is well hidden ..). Commented Jul 1, 2018 at 13:46
  • 2
    @egreg the question is how you can do this in lua? Commented Jul 1, 2018 at 13:49

1 Answer 1

10

I seem to have found an answer. But the used field is not documentated in the luatex manual, I only found it by chance in the context code. So I neither know what it exactly contains, nor if it will be there in the next versions.

\documentclass[parskip=half-]{scrartcl}
\usepackage{expl3}
\ExplSyntaxOn
\bool_new:N \g_uf_some_bool
\int_new:N  \g_uf_some_int
\ExplSyntaxOff

\usepackage{luacode}
\begin{luacode}
local catlatex       = luatexbase.registernumber("catcodetable@latex")
local tbool =token.create("g_uf_some_bool")
local ttrue =token.create("c_true_bool")

function printifzero ()
 if tex.count["g_uf_some_int"]==0  then
  tex.sprint(catlatex,"int is zero")
 else
  tex.sprint(catlatex,"int is " .. tex.count["g_uf_some_int"])
 end
end

function printifbool ()
 if tbool.mode == ttrue.mode  then 
  tex.sprint(catlatex,"bool is true")
 else
  tex.sprint(catlatex,"bool is false")
 end
end


\end{luacode}
\begin{document}
\minisec{Integers}
\ExplSyntaxOn

\directlua{printifzero()}

\par
\int_gset:Nn\g_uf_some_int{42}
\directlua{printifzero()}

\minisec{booleans}
\bool_set_true:N \g_uf_some_bool
\directlua{printifbool()}
\par

\bool_set_false:N \g_uf_some_bool
\directlua{printifbool()}
\par

\bool_set_true:N \g_uf_some_bool
\directlua{printifbool()}
\par
\ExplSyntaxOff
\end{document}

enter image description here

4
  • Browsing the source, I found that there is also token.get_mode. Commented Jul 2, 2018 at 0:40
  • 1
    @HenriMenke did you also found info about what exactly is in property? Commented Jul 2, 2018 at 7:42
  • The mode is what is called the equivalent value in the TeX source. It's meaning depends on the command field, but here it is simple: expl3 bools are \chardefs with value 1 for true, 0 for false. For \chardefs, the mode is just the character code of the char, so it is 0 or 1. Commented Jul 6, 2018 at 9:46
  • Side note, token.get_mode(token.create(x)) can be simplified to token.create(x).mode; nevertheless it will probably result in "unpredictable" value when the token is not a chardef. // See note on newer LuaTeX versions.
    – user202729
    Commented Jun 12, 2022 at 12:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .