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...
jarnosc
  • 4,266
A T
  • 4,093
  • 3
    please edit the question to state the intention of the code and provide a test file, disconnected fragments are impossible to debug. – David Carlisle Jun 18 '17 at 10:21
  • 2
    \ifx \getenv{ tests if \getenv is a left brace – David Carlisle 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
  • impossible to say what you need as you haven't said what you are trying to do nor provided any code that can be run to generate the example. – David Carlisle Jun 18 '17 at 10:24
  • 1
    there are much easier ways of passing an enviornment variable to tex, eg pdflatex \\def\\introduction{$INTRODUCTION}\\input mainfile – David Carlisle Jun 18 '17 at 10:26
  • Added a test-case repo. Weird that the \input macro isn't recognised. Trying to find what package to \usepackage for… – A T Jun 18 '17 at 10:40
  • 3
    No sorry the question should not involve external links. The question is archived forever and should be understandable as posted. – David Carlisle Jun 18 '17 at 10:42
  • Correct me if I'm wrong, but you want to load a default filename.tex from 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
  • I want a default value if the environment variable is undefined. That's the purpose of this question. – A T Jun 19 '17 at 20:07
  • Ok; but then you want to load the file with the name of the value of that variable in a document, right? – jarnosc Jun 19 '17 at 21:11
  • Well then it should fail to build, like it currently fails to build when it doesn't exist. – A T Jun 19 '17 at 23:51
  • so what do you want TeX to do if the file doesn't exist? – jarnosc Jun 20 '17 at 01:47

1 Answers1

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}
egreg
  • 1,121,712
  • Your answer didn't work, but I modified it a little and got it to almost work. Here's my WiP: https://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
  • 6
    @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
  • @AT You probably are better served by \InputIfFileExists. – egreg Jun 18 '17 at 11:10
  • Yeah, I saw that one a little earlier, but then I have a bug if someone creates a file called "UNDEFINED.tex" which would be hard to debug. – A T Jun 18 '17 at 11:11