5

LaTeX & Macros: Macros with Arguments

 2 years ago
source link: https://dev.to/latexteada/latex-macros-macros-with-arguments-2a42
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 modeExit 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

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

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

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

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


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK