数字大于或等于条件

数字大于或等于条件

难以置信有一种语言,让如此简单的事情需要如此多的努力。

嗯,我想对greater or equal to宏中的一个参数进行比较。我目前的尝试是错误的。

这是我的妈妈:

\documentclass{beamer}

\mode<presentation> {\usetheme{metropolis}}

\usepackage{tikz}
 
\NewDocumentCommand\putlabel{ O{1} }{
\begin{tikzpicture}
  \node at (0,0) (base) {base};
  \ifnum#1=1\relax\else\ifnum#1>1\relax
    \node at (base.south) (premier) [anchor=north] {au moins un};
  \fi\fi

  \ifnum#1=2\relax\else\ifnum#1>2\relax
    \node at (premier.south) (deuxieme) [anchor=north] {au moins deux};
  \fi\fi

\end{tikzpicture}
}
\begin{document}
\begin{frame}{test}
  \putlabel[1]
\dots
  \putlabel[2]
\dots
  \putlabel[3]
\dots
\end{frame}
\end{document}

答案1

您可以定义一个\?具有以下用法的宏:

\ifnum number\? >= number\relax

或者

\ifnum number\? <= number\relax

它可以这样定义:

\def\?#1#2#3\relax{\ifx#1>>\numexpr#3-1\else<\numexpr#3+1\fi\relax}

测试:

\def\?#1#2#3\relax{\ifx#1>>\numexpr#3-1\else<\numexpr#3+1\fi\relax}

\def\test#1{\ifnum #1\? >= 1\relax yes, #1>=1\else no, #1<1\fi}

\test 0 \par \test 1 \par \test 2

\def\test#1{\ifnum #1\? <= 1\relax yes, #1<=1\else no, #1>1\fi}

\test 0 \par \test 1 \par \test 2

\bye

但我明白你会说这有点棘手。不幸的是,TeX 在原始级别不支持\ifnumwith<=和,>=因为 TeX 的作者希望程序员能够在需要时使用否定。

答案2

测试整数 #1大于或等于 1 是

\ifnum#1>0

对于 2 来说也是同样的道理。

\documentclass{beamer}
\usepackage{tikz}

\mode<presentation> {\usetheme{metropolis}}

\NewDocumentCommand\putlabel{ O{1} }{% <--- don't forget
  \begin{tikzpicture}
  \node at (0,0) (base) {base};
  \ifnum#1>0
    \node at (base.south) (premier) [anchor=north] {au moins un};
  \fi

  \ifnum#1>1
    \node at (premier.south) (deuxieme) [anchor=north] {au moins deux};
  \fi
  \end{tikzpicture}% <--- don't forget
}

\begin{document}

\begin{frame}{test}

\putlabel[1] \dots \putlabel[2] \dots \putlabel[3] \dots

\end{frame}

\end{document}

在此处输入图片描述

expl3

\documentclass{beamer}
\usepackage{tikz}

\mode<presentation> {\usetheme{metropolis}}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\intCompareTF}{mmm}
 {% #1 = condition, #2 = true text, #3 = false text
  \int_compare:nTF { #1 } { #2 } { #3 }
 }
\ExplSyntaxOff

\NewDocumentCommand\putlabel{ O{1} }{% <--- don't forget
  \begin{tikzpicture}
  \node at (0,0) (base) {base};
  \intCompareTF{#1>=1}{\node at (base.south) (premier) [anchor=north] {au moins un};}{}
  \intCompareTF{#1>=2}{\node at (premier.south) (deuxieme) [anchor=north] {au moins deux};}{}
  \end{tikzpicture}% <--- don't forget
}

\begin{document}

\begin{frame}{test}

\putlabel[1] \dots \putlabel[2] \dots \putlabel[3] \dots

\end{frame}

\end{document}

如果您希望参数是一个浮点数,则可以使用expl3

\documentclass{beamer}
\usepackage{tikz}

\mode<presentation> {\usetheme{metropolis}}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\fpCompareTF}{mmm}
 {% #1 = condition, #2 = true text, #3 = false text
  \fp_compare:nTF { #1 } { #2 } { #3 }
 }
\ExplSyntaxOff

\NewDocumentCommand\putlabel{ O{1} }{% <--- don't forget
  \begin{tikzpicture}
  \node at (0,0) (base) {base};
  \fpCompareTF{#1>=1}{\node at (base.south) (premier) [anchor=north] {au moins un};}{}
  \fpCompareTF{#1>=2}{\node at (premier.south) (deuxieme) [anchor=north] {au moins deux};}{}
  \end{tikzpicture}% <--- don't forget
}

\begin{document}

\begin{frame}{test}

\putlabel[1.1] \dots \putlabel[2] \dots \putlabel[2.1] \dots

\end{frame}

\end{document}

输出将是相同的。

答案3

正如问题下所评论的,a>=b 只是 b<a 的否定,因此以下是另一种形式,产生与 egreg 显示的相同的输出。

\documentclass{beamer}
\usepackage{tikz}

\mode<presentation> {\usetheme{metropolis}}

\NewDocumentCommand\putlabel{ O{1} }{% <--- don't forget
  \begin{tikzpicture}
  \node at (0,0) (base) {base};
  \ifnum1>#1\relax\else
    \node at (base.south) (premier) [anchor=north] {au moins un};
  \fi

  \ifnum2>#1\relax\else
    \node at (premier.south) (deuxieme) [anchor=north] {au moins deux};
  \fi
  \end{tikzpicture}% <--- don't forget
}

\begin{document}

\begin{frame}{test}

\putlabel[1] \dots \putlabel[2] \dots \putlabel[3] \dots

\end{frame}

\end{document}

相关内容