我怎样才能测试两个计数器的值?

我怎样才能测试两个计数器的值?

我想测试计数器的值(最好是原始的 TeX)。

\documentclass{article}
\newcounter{counterA}
\newcounter{counterB}
\setcounter{counterA}{2}
\setcounter{counterB}{2}

\newcommand\maccommand{
  \ifx\the\value{counterA}=\the\value{counterB} 
    Just so you know, counterA (= \the\value{counterA}) holds the same value as counterB (= \the\value{counterB}).
  \else 
    Sorry, but the counterA (= \the\value{counterA}) is not equal to counterB (= \the\value{counterB}).
  \fi

但是在 TeX 中你可以写:

\ifx\counterA>2 
  Just so you know, counterA is greater to than 2.
\else 
  Just so you know, counterA is not greater than 2.
\fi
}

为什么你不能写

\ifx\counterA=2 
  Just so you know, counterA is equal to 2.
\else 
  Just so you know, counterA is not equal to 2.
\fi
}

或者

\ifx\counterA=\counterB 
  Just so you know, counterA is equal to counterB.
\else 
  Just so you know, counterA is not equal to counterB.
\fi
}

答案1

\thecounterA仅当计数器未被重新定义时才有效:

\documentclass{article}
\newcounter{counterA}\renewcommand\thecounterA{A\arabic{counterA}}
\newcounter{counterB}\renewcommand\thecounterB{B\arabic{counterB}}
\setcounter{counterA}{2}
\setcounter{counterB}{2}

\newcommand\maccommand{%
  \ifnum\value{counterA}=\value{counterB}
    Just so you know, counterA (= \thecounterA) holds the same value as 
    counterB (=\thecounterB).%
  \else 
    Sorry, but the counterA (= \thecounterA) is not equal to counterB 
    (=\thecounterB).%
\fi}

\begin{document}
\maccommand

\stepcounter{counterA}
\maccommand

\end{document}

TeX 方式:

\makeatletter
\newcommand\maccommand{%
  \ifnum\c@counterA=\c@counterB
    Just so you know, counterA (= \thecounterA) holds the same value as 
    counterB (=\thecounterB).%
  \else 
    Sorry, but the counterA (= \thecounterA) is not equal to counterB 
    (=\thecounterB).%
  \fi}
\makeatother

内部\c@counterA保存值并\thecounterA表示。在 LaTeX 级别上\c@counterA不应使用。

答案2

使用\ifnum而不是\ifx

在此处输入图片描述

\documentclass{article}
\newcounter{counterA}
\newcounter{counterB}
\setcounter{counterA}{2}
\setcounter{counterB}{2}

\newcommand\maccommand{%
    \ifnum\the\value{counterA}=\the\value{counterB}
        Just so you know, counterA (= \the\value{counterA}) holds the same value as counterB (= \the\value{counterB}).%
    \else 
        Sorry, but the counterA (= \the\value{counterA}) is not equal to counterB (= \the\value{counterB}).%
    \fi}

\begin{document}
\maccommand

\stepcounter{counterA}
\maccommand
\end{document}

答案3

你当然可以在普通的 TeX 中这样做,

\newcount\counterA

\counterA=2

\ifx\counterA>2 
  Just so you know, counterA is greater to than 2.
\else 
  Just so you know, counterA is not greater than 2.
\fi

但这会将 token\counterA与 token进行比较>,发现它们具有不同的含义,因此直到的所有内容\else都将被吞噬,您将得到

您要知道,counterA 不大于 2。

但如果你现在尝试添加

\counterA=1000

\ifx\counterA>2 
  Just so you know, counterA is greater to than 2.
\else 
  Just so you know, counterA is not greater than 2.
\fi
}

你会得到完全一样的结果。

对于数字测试,您必须使用\ifnum;代码

\newcount\counterA

\counterA=2

\ifnum\counterA>2 
  Just so you know, counterA is greater to than 2.
\else 
  Just so you know, counterA is not greater than 2.
\fi

\counterA=1000

\ifnum\counterA>2 
  Just so you know, counterA is greater to than 2.
\else 
  Just so you know, counterA is not greater than 2.
\fi
}

将产生

您要知道,counterA 不大于 2。
您要知道,counterA 大于 2。

条件\ifx比较意义后面两个标记,而不对它们进行任何扩展。

如果之后的第一个或第二个标记\ifx\else\fi,则 TeX 认为代码是不完整的条件,并\relax根据需要添加一或两个“冻结”标记。

请注意,相反,\ifnum执行扩展,因为它需要找到一个,<number>后跟一个<relation>=12、12<12中的一个)和>另一个<number>

由于在 LaTeX 中该指令\newcounter{counterA}也执行\newcount\c@counterA,并且宏\value定义为

% latex.ltx, line 2084:
\def\value#1{\csname c@#1\endcsname}

因此,

\ifnum\value{counterA}>\value{counterB}

将为测试提供适当的令牌。因此你的代码可以是

\newcommand\maccommand{%
  \ifnum\value{counterA}=\value{counterB}% <--- don't forget this!
    Just so you know, counterA (= \thecounterA) holds the same value as counterB (= \thecounterB).%
  \else 
    Sorry, but the counterA (= \thecounterA) is not equal to counterB (= \thecounterB).%
  \fi
}

请注意,\the\value{counterA}不需要。但是,需要一个%after ,因为是一个计数器寄存器,所以 TeX 不会在它之后寻找一个。如果您使用,则会查找并吞噬它,但这种方法在概念上是错误的。\value{counterB}\c@counterB<optional space><optional space>\the\value{counterB}

在宏主体中,我使用了\thecounterA\thecounterB,它们扩展为计数器值的当前表示形式(默认为十进制数)。\thecounterA在数字测试中使用 是错误的\ifnum

相关内容