# How to access etoolbox toggle inside Lua

I'm creating a package that uses etoolbox toggles but a good amount of the package is written in Lua. Is there a way to access the etoolbox toggles direct in Lua?

I know that etoolbox uses a separate namespace for it's bools, but I can't find a way to access bools created by \newif in Lua either.

Minimal (non-)working example

\documentclass{article}

\usepackage{etoolbox}
\usepackage{luacode}

\newtoggle{mytoggle}

\begin{document}
\directlua{
% Not sure what etoolbox_toggle_true function should be
if tex.?etoolbox_toggle_true?(\luastring{mytoggle}) then
tex.print('mytoggle is true!')
else
tex.print('mytoggle is false.')
end
}
\end{document}

• Distantly related, but probably not the answer: tex.stackexchange.com/q/438765/35864 – moewe Jul 9 '18 at 13:41
• The 'values' of etoolbox toggles are either \etb@toggletrue or \etb@togglefalse. And the toggle foo is actually just \etb@tgl@foo. – moewe Jul 9 '18 at 13:43

Stealing Ulrike Fischer's solution to Querying the state of a boolean from lua seems to work. Of course the same caveats apply: It is not clear to me why mode (described in the June 2018/1.0.9 version of the manual in §10.6.3, p. 209 as "a number either representing a character or another entity") is the correct thing to compare.

\documentclass[parskip=half-]{scrartcl}
\usepackage{etoolbox}
\newtoggle{foo}

\usepackage{luacode}
\begin{luacode}
local catlatex = luatexbase.registernumber("catcodetable@latex")
local mytoggle = token.create("etb@tgl@foo")

function is_etb_toggletrue_tok(toggle)
local etb_toggle_true = token.create("etb@toggletrue")
end

function is_etb_toggletrue_name(togglename)
local toggle = token.create("etb@tgl@" .. togglename)
return is_etb_toggletrue_tok(toggle)
end

function printifbool ()
if is_etb_toggletrue_name("foo") then
tex.sprint(catlatex,"bool is true")
else
tex.sprint(catlatex,"bool is false")
end
end
\end{luacode}
\begin{document}
\minisec{booleans}

\toggletrue{foo}
\directlua{printifbool()}

\togglefalse{foo}
\directlua{printifbool()}
\end{document}


We just have to know that etoolbox toggles have only two values: \etb@toggletrue or \etb@togglefals and that the toggle foo is \etb@tgl@foo.

The code now has two functions is_etb_toggletrue_tok that takes a token as produced by token_create or is_etb_toggletrue_name that takes a string with the etoolbox name of a toggle.

etb_toggle_true is assigned on each run of is_etb_toggletrue_tok, I'm not sure if that is good practice or not. But I wanted it in the smallest possible scope.