Peer pressure once again. :)
IIRC, pure Lua has no built-in way to find out the current working directory without resorting to trickery. Thankfully, LuaTeX is shipped with the amazing LuaFileSystem, which is a "library developed to complement the set of functions related to file systems offered by the standard Lua distribution."
The reference section of the LFS manual indicates a way of achieving what we want:
lfs.currentdir ()
Returns a string with the current working directory or nil
plus an error string.
There we go! Now let's write our .tex
file based on this idea:
\documentclass{article}
\edef\workingdir{\unexpanded\expandafter{\directlua{tex.write(lfs.currentdir())}}}
\begin{document}
Hello, I'm in \workingdir
\texttt{\meaning\workingdir}
\end{document}
Thanks to egreg for providing the \edef
version instead of my original \newcommand
approach; that way, it should avoid problems with special characters in the path, and if put in the preamble of the main document, the value won't change when \workingdir
is used in an \input
file.
Here's the sample output from my machine:
There we go! :)
Update: If we don't want to "freeze" the working directory value, I believe we can rely on my first attempt using \newcommand
instead of using \edef
:
\newcommand{\actualworkingdir}{%
\unexpanded\expandafter{\directlua{tex.write(lfs.currentdir())}}}
That way, lfs.currentdir()
is always issued on demand. Please bear in mind that the difference of both approaches is quite substantial:
Best Answer
This can be done with the help of the xstring package and its macro
\StrBehind
.In your case, if
\currfiledir
returnsdirA/dirB/
, thenwill return
dirB/
.In fact the result is "all the stuff behind
/
"If you also want to remove the trailing
/
you can useIn this way you first save the result of
\StrBehind
in the macro\currfilesubdir
and then you use it as the argument of\StrBefore
which does the opposite of\StrBehind
. The result will bedirB
.Obviously this only works if you have your file in a subdirectory of second level like
dirA/dirB/
.If your file is in a deeper subdirectory (let's say
dirA/dirB/dirC/dirD/
, you can opt for one of the following solutions to automate the whole process.Print the subdirectory name without the trailing
/
Add the following lines in your preamble:
and then in your subdocument use
instead of
and you will get
dirD
printed.Print the subdirectory name with the trailing
/
Add the following lines in your preamble:
and then in your subdocument use
instead of
and you will get
dirD/
printed.