# Lualatex, deal with tcolorbox in Lua

I would like to try to use lualatex in order to use as little as possible Tex code (mostly because it's unreadable, and overcomplicated). I understand that, because some libraries are coded in Tex, I can't avoid to write a few 'tex.sprint', but I'd like to use the fewer I can.

So here is what I'd like to do: define a function in lua that creates a tcolorbox, and get on lua side the dimension of the box, in order to display, for example, a picture, or any code I'd have written in lua.

MWE:

test.tex

\documentclass{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}

\begin{document}%
\begin{tcolorbox}
\directlua{dofile("file.lua")
displayPicInTCbox("simpson.jpg")
}
\end{tcolorbox}
\end{document}


file.lua

function displayPicInTCbox(filename)
tex.sprint([[\begin{tcolorbox}]])
-- This works, but I'd like to use my own lua function, so can't use it
tex.sprint([[\includegraphics[width=\tcbtextwidth]{simpson.jpg}]])
img.write({ filename = "simpson.jpg";
-- This does not work
width=tex.dimen['tcbtextwidth']
-- Same problem
-- width="\\tcbtextwidth"
})
tex.sprint([[\end{tcolorbox}]])
end


Thank you! By the way, if you have any advice to replace TeX with Lua, please tell me!

• tcolorbox is a quite large package with tons of options and features, and it uses pgf which is also a large package with lots of options. You need a better argument than "I don't like tex code" if someone should rewrite all this in lua for you. If you want to see how output can be generated only with lua check the code of the speedata publisher: download.speedata.de/publisher – Ulrike Fischer Nov 12 '17 at 11:59
• @UlrikeFischer: ok thank you. Of course I don't ask to rewrite the great tcbox package, I just wanted to know if it was possible to recover in lua some "environment variable" on the fly. – tobiasBora Nov 12 '17 at 13:20

\tcbtextwidth is macro not a length. You can't access tex macros from inside lua, so you should either store it in a length (skip) or expand it before passing to lua:

\documentclass{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\usepackage{luacode}

\newlength\lualen{}

\begin{luacode}
function insertimageA ()
img.write({ filename = "example-image-A.pdf",width=tex.skip['lualen'].width})
end

function insertimageB (imgwidth)
img.write({ filename = "example-image-A.pdf",width=imgwidth})
end
\end{luacode}

\begin{document}%
\begin{tcolorbox}

\setlength\lualen{\tcbtextwidth}
\directlua{insertimageA()}

\directlua{insertimageB("\tcbtextwidth")}
\end{tcolorbox}
\end{document}


• Ok great, thank you for the "skip" tips. I'm not sure however to understand the difference between tex.dimen['lualen'] and tex.skip['lualen'].width... – tobiasBora Nov 12 '17 at 14:57
• – Ulrike Fischer Nov 12 '17 at 14:58