[Tex/LaTex] Directory name as a variable

environment-variablesfile-lookupmacros

Is it possible to use the directory where the current tex-file is in as variable?

For example, suppose I am in the directory

/path/to/foo/

and compile there the document
bar.tex (absolute path is /path/to/foo/bar.tex).

Is it possible to use /path/to/foo or even foo as a variable in TeX/LaTeX like it is possible to use bar with variable \jobname? Is there an (array-like) TeX internal macro that is populated with the segments of a path to the input file being compiled?

Best Answer

TeX has no notion of the current directory tree, since it's written to work also with operating systems where this has no sense. If you are on a Unix system, you can run pdflatex from the command line with

pdflatex "\def\mypath{$(pwd)}\input{file}"

where file.tex is your main document, thus exploiting shell command substitution. The $(pwd) syntax is for bash, other shells may require `pwd`, instead.