Problema com um teste na declaração if

Sep 20 2020

Estou tendo problemas com a implementação de uma instrução if. Quero saber se hoje e daqui a cinco dias acontecem no mesmo mês. Recebo os valores corretos do mês correspondente para imprimir, mas quando quero testar se os dois números são iguais, não funciona. O teste é sempre falso. Provavelmente tem algo a ver com o \ newcommand que não armazena o resultado esperado ou algo parecido, mas não consigo encontrar uma solução alternativa.


\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}

Obrigado!

Respostas

3 egreg Sep 21 2020 at 04:51

A macro \ddnão entrega diretamente \the\month, mas faz várias instruções de atribuição para obtê-la.

Você também não quer fazer atribuições globais para \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}