# Directory name as a variable

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?

-

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.

-
Probably the best solution. Though this of course doesn't work if you use \input{somedir/file}. (Btw, if you are using LuaTeX, then the current dir (which might or might not coincide with the directory of the TeX file) is available as os.env.PWD.) –  Caramdir Aug 9 '11 at 21:00

There is the currfile package which gives you the file name and relative path of the current input file. Since v0.6 from 2012/05/06 it is also possible to get absolute paths if the document is compiled with the -recorder argument. Note that this argument is already used by latexmk, so if you are using it there is no extra effort.

The simplest way is to load currfile with the abspath option:

% Compile with: (pdf|xe|lua|)latex -recorder filename
% Needs two runs with MiKTeX, one with TeX Live
\documentclass{article}

\usepackage[abspath]{currfile}[2012/05/06]

\begin{document}

% These macros will show the data of the current
% input file, i.e. inside an \input file they will
% show different values
Absolute directory: \currfileabsdir

Absolute path: \currfileabspath

\end{document}


See the manual for all other provided macros (file base name, extension, etc.) if you also have need for them.

If you don't need the full functionality of currfile (which will update several macros for every input file), then you can use the sub-package currfile-abspath directly:

% Compile with: (pdf|xe|lua|)latex -recorder filename
% Needs two runs with MiKTeX, one with TeX Live
\documentclass{article}

\usepackage{currfile-abspath}

\getmainfile % get real main file (can be different than jobname in some cases)
\getabspath{\themainfile} % or use \jobname.tex instead (not as safe)
\let\mainabsdir\theabsdir % save result away (macro will be overwritten by the next \getabspath
\let\mainabspath\theabspath % save result away (macro will be overwritten by the next \getabspath

\begin{document}

Absolute directory: \mainabsdir

Absolute path: \mainabspath

\end{document}

-