5

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*}

\newcommand\fullpath[2]{\luadirect{fullpath(\luastring{#1},\luastring{#2})}}    
\newcommand\format[1]{\luadirect{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\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*}

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

\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

Mathematica graphics

On Linux

Mathematica graphics

  • It's probably \luastring{#1}. – egreg 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. – Nasser 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? – David Carlisle 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. – Nasser Jul 1 '16 at 22:37
5

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. – Nasser 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 – David Carlisle Jul 1 '16 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.