Question:
For a macro that has only one parameter, are there any issues with defining it as follows:
\NewDocumentCommand{\MyMacro}{%
s% #1 = starred variant (*** unused as of yet ***)
O{}% #2 = optional parameter (*** unused as of yet ***)
O{}% #3 = another optional parameter (*** unused as of yet ***)
m% #4 = the mandatory parameter
}{%
\emph{#4}%
}%
Note that the first three parameters are unused and are intended for possible future enhancements.
Notes:
The example usage here is just for illustrative purposes. This is for more complex macros where enhancements which were not obvious at the time of creation may be needed in the future.
I initially tried defining macros to hold each of the parameters as:
\def\GivenText{#1}% \def\ColorToUse{#2}%
but found that this has its own issues (see Reference) if the macro that called this macro had used the same name, so have moved away from this approach.
References:
- An example of why I do not always use a
\defto hold the value of the parameters is provided in What is the preferred way of defining a TikZ constant?
Background:
In the process of developing my macros, I noticed that I tend to start with something like:
\newcommand{\MyMacro}[1]{\emph{#1}}%
The I later realize that I want to add an optional parameter:
\newcommand{\MyMacro}[2][black]{\textcolor{#1}{\emph{#2}}}%
where I have to locate all the occurrences of #1, replace them with #2, which isn't too difficult, but gets more difficult with more mandatory parameters.
Then later, I realize that I want to define a starred variant (or have more than one optional parameter) at which time I switch to using the xparse's \NewDocumentCommand:
\NewDocumentCommand{\MyMacro}{%
s% #1 = starred variant
O{black}% #2 = optional parameter
m% #3 = the mandatory parameter
}{%
\textcolor{#2}{%
\IfBooleanTF{#1}{%
#3%
}{%
\emph{#3}%
}%
}%
}%
Code:
\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
%\newcommand{\MyMacro}[1]{\emph{#1}}% Version 1
%\newcommand{\MyMacro}[2][black]{\textcolor{#1}{\emph{#2}}}% Version 2
\NewDocumentCommand{\MyMacro}{% Version 3
s% #1 = starred variant
O{black}% #2 = optional parameter
m% #3 = the mandatory parameter
}{%
\textcolor{#2}{%
\IfBooleanTF{#1}{%
#3%
}{%
\emph{#3}%
}%
}%
}%
\begin{document}
Some \MyMacro*[red]{not so important text} and even less important text.
\end{document}