我试图在不使用额外包的情况下检查变量是否定义(因为我想在 .lco 文件中使用它),如果它已定义,则检查它是否为空。
定义检查有效,但空内容检查无效:
\documentclass{scrlttr2}
\newcommand{\stampfile}{}
\begin{document}
\ifdefined\stampfile
\ifx\stampfile\empty
\textbackslash stampfile is empty
\else
\textbackslash stampfile is not empty
\fi
\else
\textbackslash stampfile is not defined
\fi
\end{document}
我看看如何检查宏值是否为空或不会使用纯 TeX 条件创建文本?但解决方案确实有效......
例如我尝试过:
\documentclass{scrlttr2}
\newcommand{\stampfile}{}
\newcommand{\checkempty}[1]{
\if\relax\detokenize{#1}\relax
EMPTY%
\else
NON EMPTY%
\fi
}
\begin{document}
\ifdefined\stampfile
\ifx\stampfile\empty
\textbackslash stampfile is empty
\else
\textbackslash stampfile is not empty
\fi
\else
\textbackslash stampfile is not defined
\fi
\checkempty{\stampfile}
\end{document}
答案1
\empty
不长。如果你想在 \ifx 文本中使用它作为比较,你应该用 定义你的 stamp 命令\newcommand*
。我认为测试空白更容易:
\documentclass{scrlttr2}
\newcommand*{\stampfile}{} %<-- not long
\ExplSyntaxOn
\cs_set_eq:NN\tlifblankTF\tl_if_blank:VTF
\ExplSyntaxOff
\begin{document}
\ifdefined\stampfile
\ifx\stampfile\empty
\textbackslash stampfile is empty
\else
\textbackslash stampfile is not empty
\fi
\else
\textbackslash stampfile is not defined
\fi
\tlifblankTF\stampfile{empty}{not empty}
\renewcommand\stampfile{ }
\tlifblankTF\stampfile{empty}{not empty}
\renewcommand\stampfile{xxx}
\tlifblankTF\stampfile{empty}{not empty}
\end{document}