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

For instance, I want to include a figure: ~/figures/figure.pdf. On my Linux box, this is /home/me/figures/figure.pdf, but on OS X it is /Users/me/figures/figure.pdf. I'd like it if there is an environment variable like $HOME so that the latex file can compile on both machines \includegraphics{$HOME/figures/figure.pdf}. (Of course it wouldn't use the $ sign).

I just found this post, but Is write18 the most native way? And, in the given example, the assignment wouldn't actually define the variable in LaTex. I tried

\immediate\write18{echo "\newcommand{\HOME}{$HOME}" > var.tex}

But gives me this error:

! Argument of \@gobble has an extra }.

So now I am at a loss.

share|improve this question
Related: tex.stackexchange.com/questions/1728/… – Dror Jan 19 '14 at 15:08
up vote 12 down vote accepted

@egreg has already answered your question so I thought I'd not answer it. That is suggest not relying on environment variables for this. If the latex file just goes \includegraphics{figures/figure.pdf}

then if $HOME/figures// is in TEXINPUTS environment variable or texmf.cnf configuration setting then the file will be found without LaTeX explicitly needing to access the machine-specific information. This keeps machine specific information and directory structure in the kpathsearch system which is optimised to deal with that rather than the macro layer which tries as far as possible to avoid such issues.

Also for the specific case of home directory you can (on web2c systems on at least linux and windows/cygwin, but I assume the others too) use \string~/figures/figure.pdf as kpathsea understands ~ as the home directory (but you have to pass it a literal ~ not the normal active definition for a non-breakable space, hence the \string.

share|improve this answer

If you have a recent TeX Live (2010 or later) or MiKTeX (v. 2.9), then the following works (and does not need the -shell-escape command line option):

  \CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{}%


If you only say \getenv{VAR} then the value of the variable is printed instead of being stored in a control sequence.

Not only HOME can be used, but any environment variable and also the "pseudovariables" defined in the TeX kpathsea system such as TEXMF or TEXINPUTS.

Note that this works only with pdflatex. With other engines or older distributions, shell escape is needed. Of course LuaTeX has its methods for interacting with the system.

A version that works with all recent engines is



    \immediate\write18{kpsewhich --var-value #2 > \jobname.tmp}%
        kpse.var_value("\luatexluaescapestring{#2}") or "" ) }}%
      \CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{\endlinechar=-1}%


In the case of xetex an auxiliary file \jobname.tmp is written and -shell-escape is necessary.

Note: the LuaTeX method has been suggested by Patrick Gundlach. If the variable is unset or not known to kpathsea, the empty string will result.

share|improve this answer

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.