[Tex/LaTex] access system/environment variables from LaTeX? For instance, $HOME

environment-variablesmacrosprogramming

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.

Best 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):

\usepackage{catchfile}
\newcommand{\getenv}[2][]{%
  \CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{\endlinechar=-1}%
  \if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}

\getenv[\HOME]{HOME}

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

\documentclass{article}

\usepackage{ifxetex,ifluatex}

\ifxetex
  \usepackage{catchfile}
  \newcommand\getenv[2][]{%
    \immediate\write18{kpsewhich --var-value #2 > \jobname.tmp}%
    \CatchFileDef{\temp}{\jobname.tmp}{\endlinechar=-1}%
    \if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}
\else
  \ifluatex
    \newcommand\getenv[2][]{%
      \edef\temp{\directlua{tex.sprint(
        kpse.var_value("\luatexluaescapestring{#2}") or "" ) }}%
      \if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}
  \else
    \usepackage{catchfile}
    \newcommand{\getenv}[2][]{%
      \CatchFileEdef{\temp}{"|kpsewhich --var-value #2"}{\endlinechar=-1}%
      \if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}
  \fi
\fi

\begin{document}
\getenv[\HOME]{HOME}\show\HOME
\end{document}

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.


UPDATE 2019

With recent and up-to-date TeX distributions, the following works with every engine (except, of course, Knuth TeX): pdflatex, xelatex, lualatex, platex and uplatex. Unrestricted shell escape is not necessary.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\NewDocumentCommand{\getenv}{om}
 {
  \sys_get_shell:nnN { kpsewhich ~ --var-value ~ #2 } { } \l_tmpa_tl
  \tl_trim_spaces:N \l_tmpa_tl
  \IfNoValueTF { #1 }
   {
    \tl_use:N \l_tmpa_tl
   }
   {
    \tl_set_eq:NN #1 \l_tmpa_tl
   }
 }

\ExplSyntaxOff

\begin{document}

\getenv[\HOME]{HOME}\show\HOME

\end{document}
Related Question