I just went through part of the wikipage for LaTeX Macros and tried the following syntax to create a new command with an optional argument:
\documentclass{article}
\newcommand{\wbalTwo}[2][Wikimedia]{This is the Wikibook about LaTeX supported by {#1} and {#2}}
\begin{document}
\wbalTwo[lots of users]{John Doe}
\end{document}
This compiles fine and I get a result as follows:

I also tried switching the optional and required argument above:
\documentclass{article}
\newcommand{\wbalTwo}[2][Wikimedia]{This is the Wikibook about LaTeX supported by {#1} and {#2}}
\begin{document}
\wbalTwo{John Doe}[lots of users]
\end{document}
This also compiles fine, but in the resulting pdf the square brackets are preserved:
Which indicates to me that LaTeX needs to have the optional arguments specified first before specifying the required arguments for the newly defined command to work properly.
However, in page 4 of the titlesec manual the following is stated:
\titleformat{<command>}[<shape>]{<format>}{<label>}{<sep>}{<before-code>}[<after-code>]
I'm guessing titleformat is a command just like any other. How is it that it allows for square bracket arguments to be placed in between braced arguments? Am I missing something here?


\newcommandcan only have one optional argument and that optional argument must be the first. It is possible to code more complex argument structures using tools other than\newcommand. Previously this would have been quite painful with internal commands like\@ifnextchar[, but sincexparsewas baked into the LaTeX kernel you can now use\NewDocumentCommandfor a more flexible argument structure (https://tex.stackexchange.com/q/29973/35864, https://www.texdev.net/2020/08/20/newdocumentcommand-versus-newcommand-versus). – moewe Aug 16 '21 at 14:59\newcommand(though the total number of arguments is 9, not the total number of mandatory arguments). Though notably\newcommanditself has two optional arguments. So go figure. – moewe Aug 16 '21 at 15:12#1to#9is not due to latex that is built in to tex. – David Carlisle Aug 16 '21 at 15:18\newcommand? Why keep bothnewcommandandNewDocumentCommand? – user32882 Aug 16 '21 at 15:18\defcan't have more than nine arguments, either.) So that might be rooted in a mixture of performance considerations and 'no one in their right mind should use more than nine arguments for a macro'. (Plus in a way any limit can feel arbitrary, so 9 might not be too bad if you consider how macro arguments declared and are parsed in TeX.) The\newcommandlimitation is a bit odd, but given the interface it is understandable and again one probably felt that normal commands wouldn't need to be too complex. – moewe Aug 16 '21 at 15:19#1to#25. But really if there are more than a couple of arguments better to use a key=value syntax like\includegraphics[width=3pt, clip, angle=90]{zzz]– David Carlisle Aug 16 '21 at 15:21NewDocumentCommand. Why not simply refactor thenewcommandcode in the new LaTeX release instead of making a whole new function? That would ensure backwards compatibility... Or the LaTeX developers could simply communicate to their user base that they are "dropping" support for\newcommand. Consider all the code that had to be ported from python2 to python3 when this became a necessity.... – user32882 Aug 16 '21 at 15:22\newcommandis fundamentally different from\NewDocumentCommand, plus they have (deliberately) different behaviour w.r.t. expansion. For 'shortcuts',\newcommandremains the better choice, but for 'real' document commands I would use\NewDocumentCommand. – Joseph Wright Aug 16 '21 at 17:54\foo(\bar{a}{b}}{\bar{c}{d}}...to pass more than 9 arguments (indirectly). – John Kormylo Aug 16 '21 at 18:35