Bash 示例
# echo ${FOO:-bar}
bar
# FOO=haz; echo ${FOO:-bar}
haz
LaTeX 尝试:
回购:https://github.com/AlecTaylor/latex-env-testcase
命令(在序言中定义)
% 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
有条件的(在序言后面)
\ifx \getenv{Introduction}\UNDEFINEDvar
\def \Introduction{Introduction.draft0}
\else
\def \Introduction{\getenv{Introduction}}
\fi
使用量(以内\begin{document}
)
\input{\Introduction}
不幸的是我得到了这个错误:
(/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}
编辑:
更改if
为:
有条件的(在序言后面)
\getenv[\Introduction]{Introduction}
\ifx \Introduction\UNDEFINEDvar
\def \Introduction{Introduction.draft0.tex}
\fi
现在我收到此错误:
! 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...
答案1
你不能\getenv
这样使用:
\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}