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}
  • 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 04 '18 at 11:33
  • The condition return " Monday " with spaces, someone know how to delete them? ;( – Alan Arriaga Jun 17 '20 at 07:34

1 Answers1

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

Heiko Oberdiek
  • 271,626
  • 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 04 '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 04 '18 at 11:50
  • What happen if the day is a string variable for changing the day automatically during a week? – Andrey Vinajera May 04 '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 04 '18 at 15:45
  • I tried with the example of https://tex.stackexchange.com/questions/451/when-to-use-edef-noexpand-and-expandafter/430100#430100 anybody can help me. – Andrey Vinajera May 04 '18 at 20:51
  • the expandable is good, but when you work with any input encode (latin1, utf, ...) I get this error "Missing endcsname inserted...". The solution is in add the package \usepackage[T1]{fontenc}. Thank to all... – Andrey Vinajera May 07 '18 at 17:18