[Tex/LaTex] How to make a “condition” in a macro

conditionalsmacros

I would like to make a macro with two parameters which return a text. For instance, I want \M{1}{5} to return [1,5], and \M{2}{2} to return [2] (because the two arguments are the same). So I need to have a "check" or condition in my macro to see if the two parameters are the same and perform different executions… How do I implement this "if" in a macro?

Best Answer

To complete the list I want to present the package etoolbox. If you are loading biblatex the package etoolbox will be loaded automatically.

\documentclass{article}
\usepackage{etoolbox}% 
\newcommand{\M}[2]{%
  \ifstrequal{#1}{#2}%
    {[#1]}% #1 = #2 -> [#1]
    {[#1,#2]}% [#1,#2]
}
\begin{document}
Here is \M{2}{3}. Also, \M{2}{2} and \M{5}{1}.
\end{document}
Related Question