DEV Community

latexteada
latexteada

Posted on

LaTeX & Macros: Macros with Optional Arguments

Hello, how are you? :)

In this post I tell you

  • How to create macros with optional arguments

We have seen that we can create our own instructions, macros without arguments, and macros with arguments. Sometimes is convenient to set a default argument in a macros and modify it just when we need it, that is what we are going to learn today

Optional Arguments

Macros with one argument

The general structure of that is

\newcommand{name}[Narg][default]{instructions}
Enter fullscreen mode Exit fullscreen mode
  • name is the name of the command
  • Narg is the number of arguments
  • default is going to be the value which will be the default value, but, also, the argument which will change
  • instructions is what the command will do

Image description

Produces

Image description

Did you notice? If we want to change the argument we use [] instead of {}

If we do not specify the argument it will show the default command, but if we change, the instruction will change

Macros with multiple arguments

We already know how to set an optional argument but with macros with one argument, what if we want a macros with multiple argument?

Only the first argument may be optional

Image description

Produces

Image description

We need to call the optional argument with [] as the previous example

This is all for today.

Do not forget to follow me on Twitter @latexteada

Greetings :)

Discussion (0)