if 语句中的测试问题

if 语句中的测试问题

我在执行 if 语句时遇到了麻烦。我想知道今天和五天后是否在同一个月。我得到了相应月份的正确值来打印,但当我想测试两个数字是否相等时,它不起作用。测试总是错误。它可能与 \newcommand 不存储预期结果或类似的东西有关,但我找不到解决方法。


\documentclass{article}
\usepackage{advdate}

\makeatletter
\renewcommand\AdvanceDate[1][\@ne]{\global\advance\day#1 \FixDate}
\makeatother
\usepackage{ifthen}

\begin{document}
\SetDate[30/08/2020]

\newcommand{\dd}{{\AdvanceDate[0]\the\month}}
\newcommand{\ddo}{{\AdvanceDate[1]\the\month}}
\newcommand{\ddf}{{\AdvanceDate[5]\the\month}}


\today \quad is during month \dd \quad and tomorrow is during month \ddo \quad but we are still {\AdvanceDate[-1]\today}
\newline

\today \quad is during month \dd \quad and five days from now is during month \ddf \quad but we are still {\AdvanceDate[-5]\today}
\newline

"Today and tomorrow happen in the same month" is \ifthenelse{\equal{\dd}{\ddo}}{TRUE}{FALSE} and we are still \today
\newline

"Today and five days from now happen in the same month" is \ifthenelse{\equal{\dd}{\ddf}}{TRUE}{FALSE} but we are still {\AdvanceDate[-5]\today}

\end{document}

谢谢!

答案1

该宏\dd并不直接传递\the\month,而是执行几条赋值指令才能得到它。

您也不想对 进行全局分配\day

\documentclass{article}
\usepackage{advdate}
\usepackage{ifthen}

\newcommand{\ifsamemonthTF}[4]{%
  \begingroup % compute the first date's month
  \AdvanceDate[#1]%
  \edef\x{\endgroup\def\noexpand\firstincrementmonth{\the\month}}\x
  \begingroup % compute the first date's month
  \AdvanceDate[#2]%
  \edef\x{\endgroup\def\noexpand\secondincrementmonth{\the\month}}\x
  \ifthenelse{\firstincrementmonth=\secondincrementmonth}{#3}{#4}%
}
\newcommand{\dd}{{\AdvanceDate[0]\the\month}}
\newcommand{\ddo}{{\AdvanceDate[1]\the\month}}
\newcommand{\ddf}{{\AdvanceDate[5]\the\month}}

\begin{document}
\SetDate[30/08/2020]

\today\ is during month \dd\ and tomorrow is during 
month \ddo\ but we are still \today

\today\ is during month \dd\ and five days from now is during 
month \ddf\ but we are still \today

``Today and tomorrow happen in the same month'' is 
\ifsamemonthTF{0}{1}{TRUE}{FALSE} and we are still \today

``Today and five days from now happen in the same month'' is 
\ifsamemonthTF{0}{5}{TRUE}{FALSE} but we are still \today

\end{document}

在此处输入图片描述

相关内容