LuaLaTeX Variable Expansion

I've run into an issue with variable expansion in the different distributions of LuaLaTeX (chiefly MikTeX and TeXLive). In TeXLive (both 2014 and 2016), I can use the following to reference my home directory:

\newcommand{\home}{${$HOME}}
\newcommand{\home}{\%HOME\%}

You can use catchfile and kpsewhich that, I believe, also works in MiKTeX.

\documentclass{article}
\usepackage{catchfile}

\CatchFileEdef{\home}{|"kpsewhich -var-value=HOME"}{\endlinechar=-1 }

\begin{document}

\texttt{\home}

\end{document}


Real data has been masked off:

If you're afraid of backslashes, do

\CatchFileEdef{\home}{|"kpsewhich -var-value=HOME"}{%
\endlinechar=-1
\catcode\\=12
}

• With miktex one has to use --enable-pipes. – Ulrike Fischer Jul 21 '16 at 17:00
• This seems to work across everything that I want it to. I needed to add --enable-pipes for miktex-pdflatex but not for miktex-lualatex. – aepksbuck Jul 21 '16 at 17:10

I can't really believe that \$HOME works, but on windows you can use something like this (to avoid problems with the backslash in pathes, one should print the path with another catcode table):

\documentclass{article}
%
\usepackage{graphicx}

%log all values of os.env:

\directlua{for k, v in pairs( os.env ) do
texio.write_nl ("key: "..k .. " value: ".. v)
end}

\begin{document}
\includegraphics{\directlua{tex.print(-2,os.getenv("USERPROFILE"))}/Pictures/babel.png}
\end{document}

• This solves the problem for miktex-lualatex and the texlive version, but leaves miktex-pdflatex in a lurch. It also breaks my Linux lualatex. – aepksbuck Jul 21 '16 at 16:30
• Your question didn't mention pdflatex, it also didn't mention linux. Perhaps you can use a variant of this solution tex.stackexchange.com/a/62032/2388 – Ulrike Fischer Jul 21 '16 at 16:37
• My question did initially mention Linux and I updated it to mention pdflatex, which I hadn't gotten to yet. Sorry for the confusion. – aepksbuck Jul 21 '16 at 16:58

There is os.getenv function provided by Lua, so I would use that:

\documentclass{article}

\newcommand\home{\directlua{tex.print(-2,os.getenv("HOME"))}}

\begin{document}

this is home: \home

\end{document}
`

• This works for Linux, but has issues on Windows with backslashes. – aepksbuck Jul 21 '16 at 16:31