0

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
    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 '18 at 11:33
6

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

  • 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 '18 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. – Heiko Oberdiek May 4 '18 at 11:50
  • What happen if the day is a string variable for changing the day automatically during a week? – Andrey Vinajera May 4 '18 at 15:40
  • @AndreyVinajera Then, you need an expandable string comoparison, see your other question. There is a duplicate for this. – Heiko Oberdiek May 4 '18 at 15:45
  • I tried with the example of tex.stackexchange.com/questions/451/… anybody can help me. – Andrey Vinajera May 4 '18 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.