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 :)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay