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 there a way to access environment variables from within a LaTeX document? Suppose I define a variable in a batch file:

set MY_VAR=HelloWorld

How can I programmatically print the string "HelloWorld" inside my LaTeX document, without resorting to creating a new source document with the string in it?

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

You could use the write18 feature. For example:

\immediate\write18{echo $MY_VAR > var.tex}

Though, a file var.tex would be automatically created. You may have to enable the write18 feature, for instance by compiling with

pdflatex --enable-write18 filename.tex

or configuring your editor accordingly.

I just tested this example with the line

\immediate\write18{kpsewhich -var-value=TEXMFHOME > var.tex}

and got C:/Users/stefan/texmf as output in the pdf file.

share|improve this answer

A LuaTeX solution:

\edef \myvar{%

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.