DEV Community

latexteada
latexteada

Posted on

LaTeX & Macros: Macros with Arguments

Hello, how are you? :)

In this post I tell you

  • How to declare macros with arguments

Do you remember that in this post I gave you a brief introduction to Macros without arguments, these are helpful in those situations where you need the same expression multiple times, but what if you need to have the same expression but just modify something?

How to declare macros with arguments

The command is the following



\newcommand{\name}[args]{expr}


Enter fullscreen mode Exit fullscreen mode
  • \name is the name of the command
  • args is the number of arguments in the command, top 9
  • expr is the expression to manipulate

You already know what is \name, but if you have forgotten it, you can check it here

args, this is the number of arguments in the command, in other words, the blanks to be filled

  • If you want 1 argument you put 1, if you want 2 put 2 and so on. The maximum number of arguments is 9

The same number of arguments declared in args must match with the arguments used in the definition of expr

  • To put the position of the commands, we need to use #

The arguments have an order

Let's see some examples to clarify the information

Image description

Let's analyze the code :)

Here I declare two new commands

The command \oneArg has one argument (Which is declared in the square brackets) and this argument is going to be the exponent of x. Can you notice, there is a # this indicates Here is going to be my argument

The command \twoArgs has two arguments, one is going to be the variable, and the second is going to be the exponent, again, I use # to say Here is going to be my argument

See, when I use these new commands, check that \oneArg has one curly brackets while \twoArgs has two curly brackets

  • The curly brackets match with the number of arguments

That code produces

Image description

But wait, do you remember that the arguments have an order while declaring them, let's check it

Image description

Here we have two new commands \first and \second they have two arguments, but there are a subtle difference, did you notice it? Yes The position of the arguments Let's see what is the output

Image description

Now you have grasped what have an order means

This is all :)

Thanks for reading.

Do not forget to follow me on Twitter @latexteada

Greetings :)

Top comments (0)