[Tex/LaTex] Optional parameter in newcommand

macrosoptional arguments

I have made a command

\newcommand{\mysec}[2]{%
 \textbf{#1} \\ #2
}

I use it with \mysec{A}{B}.

But sometimes I also want a third parameter. I could make a whole new command for those situations where I need 3 parameters. But I thought if I could handle it with an optional parameter in my original command?

So I have to check if the third parameter is set and how to print if it is set.

Best Answer

Optional parameters are in [] not {} and if using \newcommand always come first.

so

\newcommand{\mysec}[3][]{%
 \textbf{#2} \\ #3 #1%
}

allows

\mysec{aaa}{bbb}

or

\mysec[ccc]{aaa}{bbb}
Related Question