0

I would like to parse a path in Lua in order to split it into its folder and file components. The goal is to create a macro that accepts an argument of the form myfolder/myfile and be able to use myfolderinside the lua block in order to put it into other macros.

The following does not work but is supposed to showcase what I seek:

\newcommand*{\splitme}[1]{\directlua{
    local parts=split(#1,'[\\/]+');
    tex.sprint("\graphicspath{",parts[1],"}");
}}

I checked out http://lua-users.org/wiki/SplitJoin but could not piece it together in LuaLaTeX. Is it possible though without too much hassle?

5
  • Can we assume we are simply splitting at the last / char? If so, string.match is likely the way to go
    – Joseph Wright
    Mar 7, 2017 at 9:28
  • 2
    you don't actually need lua for this of course, latex already has in the format \filename@parse which is used all over the graphics package to split a filename into directory, basename and extension parts. Mar 7, 2017 at 9:43
  • @Joseph: Yes, that was my intention. Henry Menke has actually used string.match in his answer...
    – FlorianL
    Mar 7, 2017 at 15:40
  • @David: Thanks for the hint. I just tried looking into \filename@parse, but find it very hard to understand its usage.
    – FlorianL
    Mar 7, 2017 at 15:42
  • @FlorianL I added an example Mar 7, 2017 at 15:52

3 Answers 3

5

In such cases StackOverflow can be helpful.

\documentclass{article}

% https://stackoverflow.com/questions/5243179
\newcommand*{\splitme}[1]{\directlua{\unexpanded{
    local parts = string.match([[#1]], "(.-)([^\\/]-)$")
    tex.sprint(parts)
    %tex.sprint("\graphicspath{",parts,"}")
}}}

\begin{document}

\splitme{/home/user/test.jpg}

\splitme{/home/user/}

\splitme{/home/user}

% Print the backslash
\def\\{\textbackslash}

\splitme{C:\\home\\user\\test.jpg}

\splitme{C:\\home\\user\\}

\splitme{C:\\home\\user}

\end{document}

enter image description here

0
1

Since I'm not very familiar with lua nor LuaLaTeX here is some lua code, which should return the substring:

local i = 0
local p = 0
while i <= in_string:len() do
    if in_string:sub(i,i) == "/" then
        p = i
    end
    i = i + 1
end
return in_string:sub(0,p)

There might be a much simpler solution making use of the capabilities of lua, but since I don't know these, this is what I would have done.

1

If you just need / (and you shouldn't ever need \ as a path separator in latex) then the format already has a filename parser for this:

enter image description here

\documentclass{article}

\makeatletter
\newcommand\splitme[1]{\filename@parse{#1}\filename@area}
\makeatother
\begin{document}

\splitme{/home/user/test.jpg}

\splitme{/home/user/}

\splitme{/home/user}


\end{document}
2
  • With the difference this is not expandable.
    – egreg
    Mar 7, 2017 at 15:56
  • @egreg well true but lots of things aren't expandable:-) (actually it could be expandable, or defined by directlua since it is definable in texsys.cfg at format time.:-) Mar 7, 2017 at 16:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .