TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

For example, suppose I am in the directory


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?

share|improve this question
up vote 9 down vote accepted

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.

share|improve this answer
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



% 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


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


\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


Absolute directory: \mainabsdir

Absolute path: \mainabspath

share|improve this answer
Hi @MartinScharrer , unfortunately this doesn't work on my computer. Maybe because I'm using Windows 10? – K.Mulier Jun 27 at 17:56
@K.Mulier: Are you sure you use the -recorder argument of the LaTeX executable? – Martin Scharrer Jun 30 at 18:27
how do I "use the '-recorder' argument on the LaTeX executable"? I use the TeXstudio IDE to develop my LaTeX documents. The compilation is a simple click on a button. I'm not really familiar with the things happening during compilation behind the scenes of my IDE. Thank you so much for your help. – K.Mulier Jun 30 at 18:38
@K.Mulier: Sorry, don't know TeXstudio. Look after "Settings", "Compiler", or similar, in the menu. – Martin Scharrer Jul 4 at 18:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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