我有类似以下 MWE 代码的东西(见下文),\if
仅当相应的if 命令已定义。否则,条件\if
将被跳过。但是,当“if 命令”(即)未定义时,我会收到错误。\else
\ifdefined
\ifiskomascriptloaded
! 额外\其他。
l.13 \其他
这两种if嵌套有问题吗?
\documentclass[]{article}
% Defining an 'if command'
% !!! The ERROR pops up IF THIS LINE IS COMMENTED OUT
\newif\ifiskomascriptloaded \iskomascriptloadedfalse
%Checking if the 'if command' is defined
\ifdefined\ifiskomascriptloaded
%In case defined we use the 'if command'
\ifiskomascriptloaded
\newcommand{\testtext}{Package Not Loaded}
\else
%Note: Never to use with a komma script document
\usepackage{scrextend} %with the nice functionality addmargin.
\newcommand{\testtext}{Package Loaded}
\fi
\else
%In case not defined we do something else
\newcommand{\testtext}{Not defined}
\fi
\begin{document}
The result is: \testtext
\end{document}
答案1
看看如果没有定义会发生什么
\ifdefined\ifiskomascriptloaded % false so skip to matching \else
\ifiskomascriptloaded % skip over this (this is not an if remember)
\newcommand{\testtext}{Package Not Loaded}% skip this
\else % _this_ else matches the \ifdefined
% so do these
\usepackage{scrextend}
\newcommand{\testtext}{Package Loaded}
\fi % this closes the \ifdefined
\else %oops this is an error an \else with no if
\newcommand{\testtext}{Not defined}% this is an error as the command was defined just above
\fi % and so is this an error as no matching if.
如果你使用标准 Latex 语法
\@ifundefined{ifiskomascriptloaded}
{undefined case}
{defined case}
那么问题就不会出现。这是因为 latex 语法......
答案2
我找到了一篇好文章这里我发现了一个答案'清洁工'(使用ifthen
包;)),无需使用@(makeatletter
和 co.)
另一方面,if 条件稍微长一些,因为\isundefined
它不适用于 if 命令,例如\ifiskomascriptloaded
,所以我需要检查两个宏-true
并-false
与 if 相关联。
我相信 LaTeX 专家可以定义一个很好的宏\isifundefined
,使整个表达式更加紧凑。:)
\ifthenelse{\isundefined{\iskomascriptloadedtrue} \AND \isundefined{\iskomascriptloadedfalse}}
{\newcommand{\testtext}{Not defined}} %then code
{ \ifiskomascriptloaded
\newcommand{\testtext}{Package Not Loaded}
\else
\usepackage{scrextend} %with the nice functionality addmargin.
\newcommand{\testtext}{Package Loaded}
\fi} %end ifthenelse