# question on latex->lua->latex->lua call chain. How to pass things as strings back to latex?

question on basic lualatex programming which I am learning.

I have a lua function which returns the full path of a file name. On its own it works ok. I print the value from lua, and it is correct. I wanted to call this lua function from latex, return the full path, then again call a second lua function with this value in order to do some other processing with the file.

(later I wanted to open the file in lua to read its content). But I wanted to see if I can do this in 2 steps.

The problem is when I get the result from the first lua call, I do not know how to send it back from lua as string, in order to save it inside latex and pass it to the next lua call.

Since the full file name contains _ I need everything to be strings ofcourse, and I do not know how to return the result as string in order to make Latex happy and let me pass this value later to lua. Do I need to use verbatim for this? But will this mess up the second call to lua? Here is the call flow

       (current folder,file name)
latex ---------------------------> lua first call
<-------- return full file name "/home/me/etc..../file_name.m"

(full file name)
latex -------------------> second lua call (does processing on file)
<---- return final result


here is a MWE of the above

\documentclass{article}
\IfFileExists{luatex85.sty}{\usepackage{luatex85}}{}

\ifdefined\HCode% detect tex4ht
\usepackage[utf8]{luainputenc}
\usepackage[T1]{fontenc}
\else
\usepackage{fontspec}
\fi
\usepackage{luacode}

\begin{luacode*}
require 'lfs'
-- first Lua function
function fullpath(curDir,fileName)
local pathseparator = package.config:sub(1,1)

if os.type == "windows" then
fileName=string.gsub(fileName,"/",pathseparator)
end

local name=table.concat({lfs.currentdir(),fileName}, pathseparator);
print(name)
tex.print(name)
end

-- second Lua function
function format(fileName)
--for now, just print it.
tex.print("\\verb|"..fileName.."|")
end
\end{luacode*}

\begin{document}
%ask Lua to find full path name of some file. This should work
%on both windows and Linux

%how to save this result in Latex?
%\fullpath{\jobname.tex}{sub_folder/foo7_1.tex}  %causes an error.

\edef\name{\fullpath{\jobname.tex}{sub_folder/foo7_1.tex}}

%send the name back to lua. causes error if uncomment
%\format{\name}%causes error. I need to pass VALUE of \name to lua. How?
\end{document}


How can one make the above work? I can do everything inside lua in one function, but wanted to learn how to do the above.

Thanks to David help. Here is the final version, which works on windows and Linux with the output generated. I thought to to show the output on both windows and Linux.

\documentclass{article}
\IfFileExists{luatex85.sty}{\usepackage{luatex85}}{}

\ifdefined\HCode% detect tex4ht
\usepackage[utf8]{luainputenc}
\usepackage[T1]{fontenc}
\else
\usepackage{fontspec}
\fi
\usepackage{luacode}

\begin{luacode*}
require 'lfs'
-- first Lua function
function fullpath(curDir,fileName)
local pathseparator = package.config:sub(1,1)

if os.type == "windows" then
fileName=string.gsub(fileName,"/",pathseparator)
end

local name=table.concat({lfs.currentdir(),fileName}, pathseparator);
tex.print(-2,name)
end
-- second Lua function
function format(fileName)
tex.print(-2,fileName)
end

\end{luacode*}

\begin{document}
\def\name{\fullpath{\jobname.tex}{sub_folder/foo7_1.tex}}
%send the name back to lua to test the call
\format{\name}%
\end{document}


On widnows

On Linux

• It's probably \luastring{#1}. Jul 1 '16 at 22:30
• @egreg that was copy paste error on end., I had that there, last minute error. WIll update. This is not the problem ofcouse. Thanks. Jul 1 '16 at 22:34
• as egreg says but why have you defined \fullpath so the first 1rgument (if not discarded as now) is " quoted but teh second argument has to have "? wouldn't it be more natural to require quotes in neither, or both? Jul 1 '16 at 22:34
• @DavidCarlisle Ok, I changed it. This is style issue, but will change it so it is the same for both. Thanks. Jul 1 '16 at 22:37

You want to print using catcode table -2: all catcode 12 or 10:

\documentclass{article}
\IfFileExists{luatex85.sty}{\usepackage{luatex85}}{}

\ifdefined\HCode% detect tex4ht
\usepackage[utf8]{luainputenc}
\usepackage[T1]{fontenc}
\else
\usepackage{fontspec}
\fi
\usepackage{luacode}

\begin{luacode*}
require 'lfs'
-- first Lua function
function fullpath(curDir,fileName)
local pathseparator = package.config:sub(1,1)

if os.type == "windows" then
fileName=string.gsub(fileName,"/",pathseparator)
end

local name=table.concat({lfs.currentdir(),fileName}, pathseparator);
print(name)
tex.print(-2,name)
end

-- second Lua function
function format(fileName)
--for now, just print it.
tex.print(-2,fileName)
end
\end{luacode*}

\newcommand\fullpath[2]{\directlua{fullpath(\luastring{#1},#2)}}
\newcommand\format[1]{\directlua{format(#1)}}

\begin{document}
%ask Lua to find full path name of some file. This should work
%on both windows and Linux

%how to save this result in Latex?
\fullpath{\jobname.tex}{"sub_folder/foo7_1.tex"}  %causes an error.

%edef causes error
\def\name{\fullpath{\jobname.tex}{"sub_folder/foo7_1.tex"}}

%send the name back to lua. causes error if uncomment
\format{"\name"}
\end{document}


personally I wouldn't use the luacode package here and just use \directlua and \luaescapestring as then it is easier to understand the relationship between lua and tex as there are not so many layers involved.

• Thanks. but the error is still there when I uncoment \format{\name} . I need to pass the full path name back to lua. Jul 1 '16 at 22:46
• @Nasser you had forgotten the quotes, \format needs \format{"hello"] the way you have defined it but \name just expands to hello, I uncommented that line in my answer Jul 1 '16 at 22:49