以下条件宏在乳胶中可行吗?

以下条件宏在乳胶中可行吗?

我有一个简单的宏,我想将其定义为

\newcommand{\myalert}[1]{\textcolor{red}{ALERT: #1}}

但是,我想要一个条件,如果#1是两个字符串之一(比如“noalert”和“null”),那么宏就相当于忽略#1并将其吃掉:

\newcommand{\myalert}[1]{}

我不确定在乳胶中定义命令时在多大程度上允许此类条件。

答案1

这是一种使用可能性\str_case:nnF

\documentclass{article}
\usepackage{xcolor}

\ExplSyntaxOn

\NewDocumentCommand \myalert { m }
  {
    \str_case:nnF {#1}
      {
        { noalert } { }
        { null } { }
      }
      { \textcolor{red}{ALERT:~#1} }
  }

\ExplSyntaxOff

\begin{document}

\noindent
\myalert{fire!}\\
\myalert{water!}\\
X\myalert{noalert}\myalert{null}X\\
\myalert{earthquake!}

\end{document}

在此处输入图片描述

同样的,没有expl3类别代码,以防你对它们感到不舒服:

\documentclass{article}
\usepackage{xcolor}

\ExplSyntaxOn
\cs_new_eq:NN \strcasennF \str_case:nnF
\ExplSyntaxOff

\NewDocumentCommand \myalert { m }
  {%
    \strcasennF {#1}
      {
        {noalert} {}
        {null} {}
      }
      {\textcolor{red}{ALERT: #1}}%
  }

\begin{document}

\noindent
\myalert{fire!}\\
\myalert{water!}\\
X\myalert{noalert}\myalert{null}X\\
\myalert{earthquake!}

\end{document}

答案2

使用纯 (La)TeX:

\documentclass{article}
\usepackage{xcolor}

\newcommand{\myalert}[1]{%
    \def\compareA{#1}%
    \def\compareB{noalert}%
    \def\compareC{null}%
    \ifx\compareA\compareB\else%
        \ifx\compareA\compareC\else%
            \textcolor{red}{ALERT: #1}%
        \fi%
    \fi%
}


\begin{document}

\myalert{hello}

\myalert{null}

\myalert{noalert}

\myalert{bye}

\end{document}

在此处输入图片描述

答案3

您没有指定如果事物被包裹在\MakeUppercase// /中时您希望有什么样\MakeLowercase的行为。\uppercase\lowercase

如果您对临时宏感到不舒服,\ifx..\else..\fi无论出于什么原因并且喜欢“oldschool-coding”,那么您可以通过分隔参数的方式进行完全扩展。

\makeatletter
%%=============================================================================
%% Paraphernalia:
%%    \UD@firstoftwo, \UD@secondoftwo, \UD@CheckWhetherNull,
%%    \UD@CheckWhetherBlank
%%=============================================================================
\newcommand\UD@firstoftwo[2]{#1}%
\newcommand\UD@secondoftwo[2]{#2}%
\@ifdefinable\UD@stopromannumeral{\chardef\UD@stopromannumeral=`\^^00}%
%%-----------------------------------------------------------------------------
%% Check whether argument is empty:
%%.............................................................................
%% \UD@CheckWhetherNull{<Argument which is to be checked>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is empty>}%
%%                     {<Tokens to be delivered in case that argument
%%                       which is to be checked is not empty>}%
%%
%% The gist of this macro comes from Robert R. Schneck's \ifempty-macro:
%% <https://groups.google.com/forum/#!original/comp.text.tex/kuOEIQIrElc/lUg37FmhA74J>
\newcommand\UD@CheckWhetherNull[1]{%
  \romannumeral\expandafter\UD@secondoftwo\string{\expandafter
  \UD@secondoftwo\expandafter{\expandafter{\string#1}\expandafter
  \UD@secondoftwo\string}\expandafter\UD@firstoftwo\expandafter{\expandafter
  \UD@secondoftwo\string}\expandafter\UD@stopromannumeral\UD@secondoftwo}{%
  \expandafter\UD@stopromannumeral\UD@firstoftwo}%
}%
%%-----------------------------------------------------------------------------
%% Check whether argument is blank, i.e., is empty or consists of space tokens
%% only:
%%.............................................................................
%% -- Take advantage of the fact that TeX discards space tokens when
%%    "fetching" _un_delimited arguments: --
%% \UD@CheckWhetherBlank{<Argument which is to be checked>}%
%%                      {<Tokens to be delivered in case that
%%                        argument which is to be checked is blank>}%
%%                      {<Tokens to be delivered in case that argument
%%                        which is to be checked is not blank>}%
\newcommand\UD@CheckWhetherBlank[1]{%
  \romannumeral\expandafter\expandafter\expandafter\UD@secondoftwo
  \expandafter\UD@CheckWhetherNull\expandafter{\UD@firstoftwo#1{}{}}%
}%
%%=============================================================================
\@ifdefinable\UD@GobbleToExclam{\long\def\UD@GobbleToExclam#1!{}}%
%
\newcommand\myalert[1]{%
  \UD@CheckWhetherBlank{#1}{% #1 is blank, i.e., either empty or space tokens only
  }{%  #1 is not blank
    \expandafter\UD@CheckWhetherNull\expandafter{\UD@GobbleToExclam#1!}{%
       \ForkNoalertNullElse
       !#1!null!{}% #1 = noalert
       !noalert!#1!{}% #1 = null
       !noalert!null!{\textcolor{red}{ALERT: #1}}% #1 s. th. else withhout !
       !!!!%
    }{\textcolor{red}{ALERT: #1}}% #1 s. th. else with !
  }%
}%
\@ifdefinable\ForkNoalertNullElse{\long\def\ForkNoalertNullElse#1!noalert!null!#2#3!!!!{#2}}%
\makeatother

\documentclass{article}
\usepackage{xcolor}
\colorlet{RED}{red}

\begin{document}

\begin{tabular}{ll}
\textbf{Command}&\textbf{Result}\\
\hline
\verb|(\myalert{Some text})|&(\myalert{Some text})\\
\verb|(\myalert{Some ! text})|&(\myalert{Some ! text})\\
\verb|(\myalert{Some {more} text})|&(\myalert{Some {more} text})\\
\verb|(\myalert{})|&(\myalert{})\\
\verb|(\myalert{ })|&(\myalert{ })\\
\verb|(\myalert{noalert})|&(\myalert{noalert})\\
\verb|(\myalert{null})|&(\myalert{null})\\
\verb|\MakeUppercase{(\myalert{Some text})}|&\MakeUppercase{(\myalert{Some text})}\\
\verb|\MakeUppercase{(\myalert{null})}|&\MakeUppercase{(\myalert{null})}\\
\verb|\uppercase{(\myalert{Some text})}|&\uppercase{(\myalert{Some text})}\\
\verb|\uppercase{(\myalert{null})}|&\uppercase{(\myalert{null})}
\end{tabular}

\end{document}

在此处输入图片描述

注意当事物被包裹在\uppercase
大写null产生的结果NULL与不同,因此传递了null一个以作为其参数的警报消息。NULL

相关内容