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...
\ifx \getenv{tests if\getenvis a left brace – David Carlisle Jun 18 '17 at 10:22\firstand\secondto compare with? – A T Jun 18 '17 at 10:23pdflatex \\def\\introduction{$INTRODUCTION}\\input mainfile– David Carlisle Jun 18 '17 at 10:26\inputmacro isn't recognised. Trying to find what package to\usepackagefor… – A T Jun 18 '17 at 10:40filename.texfrom an environment variable, if a filename is not explicitly given in the document, right? What if the environment variable is not defined and no specific filename given explicitly? – jarnosc Jun 19 '17 at 19:28