2

In the following example I want write Tuesday if the variable dia is Monday. The following example write rest not matter the string of variable dia.

\documentclass{article}
\def\dia#1{\if{#1=monday}{tuesday}\else{rest}\fi}
\begin{document}
    \dia{monday}
\end{document}
2
  • 4
    TeX's conditionals may be a bit unintuitive at first. You can get slightly more intuitive with etoolbox: \usepackage{etoolbox} \newcommand*{\dia}[1]{\ifstrequal{#1}{monday}{tuesday}{rest}}
    – moewe
    May 4, 2018 at 11:33
  • The condition return " Monday " with spaces, someone know how to delete them? ;( Jun 17, 2020 at 7:34

1 Answer 1

9

There are many ways to do it. The following also works with plain TeX and without e-TeX or other extensions. However, it is not expandable, but this does not seems to be needed here.

\documentclass{article}
\newcommand*{\dia}[1]{%
  \begingroup % keep macro definitions local
    \def\param{#1}%
    \def\monday{monday}%
    \ifx\param\monday
      Tuesday%
    \else
      Rest%
    \fi
  \endgroup
}
\begin{document}
  \dia{monday}
\end{document}

Result

Links:

An expandable solution without direct string comparison:

\documentclass{article}

\makeatletter
\@namedef{dia@Monday}{Tuesday}
\@namedef{dia@Tuesday}{Wednesday}
\@namedef{dia@Wednesday}{Thursday}
\@namedef{dia@Thursday}{Friday}
\@namedef{dia@Friday}{Saturday}
\@namedef{dia@Saturday}{Sunday}
\@namedef{dia@Sunday}{Monday}
\newcommand*{\dia}[1]{%
  \expandafter\ifx\csname dia@#1\endcsname\relax
    Rest%
  \else
    \csname dia@#1\endcsname
  \fi
}
\makeatother

\begin{document}
  \dia{Monday}
  \dia{\dia{Monday}}
  \dia{\dia{\dia{Monday}}}
  \dia{\dia{\dia{\dia{Monday}}}}
  \dia{\dia{\dia{\dia{\dia{Monday}}}}}
  \dia{\dia{\dia{\dia{\dia{\dia{Monday}}}}}}
  \dia{\dia{\dia{\dia{\dia{\dia{\dia{Monday}}}}}}}
  \dia{\dia{\dia{\dia{\dia{\dia{\dia{\dia{Monday}}}}}}}}
\end{document}

Result

6
  • Maybe you can say a word or two about why the OP's approach didn't work. \if{#1=monday} looks like an intuitive approach if one is used to other languages.
    – moewe
    May 4, 2018 at 11:38
  • 1
    @moewe I have interpreted this as pseudo-code with no intention to be real TeX code. I have added some links. May 4, 2018 at 11:50
  • What happen if the day is a string variable for changing the day automatically during a week? May 4, 2018 at 15:40
  • @AndreyVinajera Then, you need an expandable string comoparison, see your other question. There is a duplicate for this. May 4, 2018 at 15:45
  • I tried with the example of tex.stackexchange.com/questions/451/… anybody can help me. May 4, 2018 at 20:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .