DEV Community

latexteada
latexteada

Posted on

LaTeX & Macros: Math in Macros

Hello, how are you? :)

In this post you I tell you

  • How to put or declare math in a macro

Introduction

We have checked two types of macros

  1. Macros without arguments
  2. Macros with arguments

And, we have seen that they help us to avoid repeating the same structure many times, but with normal text, not with math.

Math in macros

Today we will meet the instruction

\ensuremath{myEquation}
Enter fullscreen mode Exit fullscreen mode

This command allow us to define macros with math which works in text and math modes, the general structure is

\newcommand{\name}{\ensuremath{myEquation}}
Enter fullscreen mode Exit fullscreen mode

So, you could had thought, I just put the instructions between $ and it is all but no, because if you do that in the macros, in the normal text you need to put the math between $ too, and it is not correct

Image description

Here I declared the macro with math using our new instruction, it produces

Image description

Did you notice, with ensuremath you can use your command either between $ or without them

The recommendation is to use the math inside the $ symbols or \[\] as we have seen, to have an order and know when something is about math and when it is not related with math

Here is an example where you can play.

What happens if you do not use ensuremath and want to use a math expression without using $?

This is all for today

Do not forget to follow me on Twitter @latexteada

Greetings :)

Top comments (0)