#Optional arguments in `{ }` instead of `[ ]`

1 messages · Page 1 of 1 (latest)

lost barn
#

It doesn't have to be like that, I only want to be able to provide a command with some amount of inputs, from left to right. But while trying to create \mycommand{A}{B}{C}, the closest syntax I got to work so far was \mycommand[B]{A}{C}, which obviously isn't the correct order, and the [ ] looks out of place. Here's what I expect the working command would look like:

  • \mycommand{A}{B}{C}
  • \mycommand{A}{B}
  • \mycommand{A}

I'd like to achieve this without the need for external packages, I hope it's not too convoluted.

summer idol
#

I mean it completely breaks the LaTeX's convention but you can probably use latex3

lost barn
#

Yeah, I get that, but that's the only possibility I could think of. Is it perhaps possible to create \mycommand{A}[B][C] instead? I think I saw commands in that form before.

summer idol
#

that is much easier with latex3

#
\ExplSyntaxOn
\NewDocumentCommand{\printargs}{m o o}{
    Mandatory:~#1\par
    \IfNoValueTF{#2}{
        First~optional:~none\par
    }{
        First~optional:~#2\par
    }
    \IfNoValueTF{#3}{
        Second~optional:~none\par
    }{
        Second~optional:~#3\par
    }
}
\ExplSyntaxOff

\printargs{hello}
\medskip
\printargs{hello}[world]
\medskip
\printargs{hello}[world][!]
vale drumBOT
summer idol
#

so the key thing is the "type" you supply when making a document command

#

i.e. m is mandatory, o is optional

#

and there's other forms too so feel free to expand on that

#

🐮

lost barn
#

That sure is powerful. Before, I was trying to create the command consisting just of

#1
\ifx#2\@empty\else#2\fi
\ifx#3\@empty\else#3\fi

I guess I was sort of on the right track. Thanks for all the help 😁

#

Is there a downside to using latex3 or is it too new to show up in stack exchange posts?

summer idol
#

i don't think it's a downside

#

but a small loud proportion of people fear its syntax

lost barn
#

Looks similar enough to regular latex syntax to me.

summer idol
#

that's what i think too lol