1

Bash example

# echo ${FOO:-bar}
bar
# FOO=haz; echo ${FOO:-bar}
haz

LaTeX attempt:

Repo: https://github.com/AlecTaylor/latex-env-testcase

Command (defined in preamble)

% Edited from: https://tex.stackexchange.com/a/342447
\def\UNDEFINEDvar{UNDEFINED}
\ifxetex
  \usepackage{catchfile}
  \newcommand\getenv[2][]{%
    \immediate\write18{kpsewhich --var-value #2 > \jobname.tmp}%
    \CatchFileDef{\temp}{\jobname.tmp}{\endlinechar=-1}%
    \ifx\temp\empty\def\temp{\\UNDEFINEDvar}\fi
    \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 "" ) }}%
      \ifx\temp\empty\def\temp{UNDEFINED}\fi
      \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}%
      \ifx\temp\empty\def\temp{\UNDEFINEDvar}\fi
      \if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}
  \fi
\fi

Conditional (later in preamble)

\ifx \getenv{Introduction}\UNDEFINEDvar
    \def \Introduction{Introduction.draft0}
\else
    \def \Introduction{\getenv{Introduction}}
\fi

Usage (within \begin{document})

\input{\Introduction}

Unfortunately I get this error:

(/usr/share/texlive/texmf-dist/tex/latex/tools/.tex)
! TeX capacity exceeded, sorry [input stack size=5000].
\getenv ->\@protected@testopt \getenv 
                                      \\getenv {}
l.300 \input{\Introduction}

EDIT:

Changing the if to:

Conditional (later in preamble)

\getenv[\Introduction]{Introduction}
\ifx \Introduction\UNDEFINEDvar
    \def \Introduction{Introduction.draft0.tex}
\fi

and now I get this error:

! LaTeX Error: File `UNDEFINED.tex' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: tex)

Enter file name: 
! Emergency stop.
<read *> 

l.300 \input{\Introduction}
                           ^^M
!  ==> Fatal error occurred, no output PDF file produced!

Latexmk: Missing input file: 'UNDEFINED.tex' from line
  '! LaTeX Error: File `UNDEFINED.tex' not found.'
Latexmk: 'pdflatex': source file 'UNDEFINED.tex' doesn't exist. I'll try making it...
12
  • 3
    please edit the question to state the intention of the code and provide a test file, disconnected fragments are impossible to debug. Jun 18 '17 at 10:21
  • 2
    \ifx \getenv{ tests if \getenv is a left brace Jun 18 '17 at 10:22
  • So do I need to create a one-use variable for each side, so that it has \first and \second to compare with?
    – A T
    Jun 18 '17 at 10:23
  • 1
    there are much easier ways of passing an enviornment variable to tex, eg pdflatex \\def\\introduction{$INTRODUCTION}\\input mainfile Jun 18 '17 at 10:26
  • 3
    No sorry the question should not involve external links. The question is archived forever and should be understandable as posted. Jun 18 '17 at 10:42
1

You can't use \getenv like that:

\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}%
    \ifx\temp\empty\def\temp{UNDEFINED}\fi
    \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 "" ) }}%
      \ifx\temp\empty\def\temp{UNDEFINED}\fi
      \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}%
      \ifx\temp\empty\def\temp{UNDEFINED}\fi
      \if\relax\detokenize{#1}\relax\temp\else\let#1\temp\fi}
  \fi
\fi


\getenv[\myvar]{FOO}
\def\UNDEFINEDvar{UNDEFINED}

\ifx \myvar\UNDEFINEDvar
  \def \Introduction{Introduction.draft0}
\else
  \def \Introduction{\myvar}
\fi

\begin{document}

\texttt{\meaning\Introduction}

\end{document}
7
  • Your answer didn't work, but I modified it a little and got it to almost work. Here's my WiP: tex.stackexchange.com/a/375538 - If you can edit it to make it work, I'll delete that answer and accept your edited one @egreg
    – A T
    Jun 18 '17 at 10:55
  • 5
    @AT You shouldn't add a non-answer, but rather open a new question specifying in detail what you are after, with a full minimal example from \documentclass to \end{document}. I showed you what your mistake was, but I'm not clairvoyant and cannot guess your final aim.
    – egreg
    Jun 18 '17 at 10:58
  • Here is pseudo bash for what I want: \begin{document} `cat ${Introduction:-introduction.draft0.tex}`\end{document}. As you can see, it has a default filename if none is provided in an environment variable.
    – A T
    Jun 18 '17 at 11:07
  • 1
    @AT Sorry, I gave you the help I could. Guessing is not my favorite activity.
    – egreg
    Jun 18 '17 at 11:09
  • I am trying to be as unambiguous as possible. Include filename in \input macro, where filename is found in a specific environment variable else use specified default filename.
    – A T
    Jun 18 '17 at 11:09

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