TransWikia.com

Lualatex, deal with tcolorbox in Lua

TeX - LaTeX Asked on March 12, 2021

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!

One Answer

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}

newlengthlualen{}

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}

setlengthlualen{tcbtextwidth}
directlua{insertimageA()}

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

enter image description here

Correct answer by Ulrike Fischer on March 12, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP