Text FormattingMacros vs Defining Commands vs Defining Environments

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Macros vs Defining Commands vs Defining Environments

Post by LaTexLearner »

Newb question: What is the difference between a defining commands, custom environments, and macros? Or are they all the same thing?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Macros vs Defining Commands vs Defining Environments

Post by Johannes_B »

LaTeX is a macro language, meaning it is based on macros and you can easily define new macros. The word macro is mainly used for new user commands. On the other hand, macros defined by the kernel or packages are called commands. It is just a strange naming convention.
You can also call them control sequences, as well. Internally, a lot of stuff is going on using the internal csname, cs standing for control sequence.

What are environments then? The usual command to define an environment is:

Code: Select all

\newenvironment{area}{code beginning the environment
}{code ending the environment
}
To put it in a nutshell, this defines a comman/macro/controlsequence \newcommand{\area}{code beginning the environment} and a macro \newcommand{\endmacro}{code to end the environment}.
Of course, this is very simplified and not the real internal code. There is also more going on in the background, for example the test if the environment is already defined and a lot more.


LaTeX2e provides newcommand and newenvironment in different variations, TeX uses def which is low level and shouldn't be used without care and experience.
The LaTeX3 kernel, that is in development for the last 20 years, provides package xparse, that you can use with the current version of LaTeX. It provides very powerful extensions and tests and is really worth a look.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Macros vs Defining Commands vs Defining Environments

Post by Stefan Kottwitz »

Some comments also by me. The mentioned term control sequence is pretty general and may for example mean commands or lengths. It's more relevant for TeX users, while LaTeX users should work on a pretty abstract level (even if it bases on TeX and there are the same basic elements). In a perfect world, LaTeX users would use LaTeX commands and environments and macros.

While meaning of the term command is pretty clear, a macro means a set of commands (which can mean a new command). It's like collecting some code, which would otherwise be typed again and again, giving a name and using later just the name. This is not only for quicker typing of shorter text. Macros allow easy and consistent changes in document design. While users make macros when developing documents, they often use macros prepared by designers, programmers, class authors or package authors.

An environment is like a set of two commands, one command begins it, the other command ends it, and it has a special effect: commands or settings with local meaning (many have that) have only effect within the environment. This is a great advantage, because so you don't need to worry about restoring original settings. For example, we simply use \centering within a figure environment, which centers the image, but the outer text is not effected. The \begin and \end statements clearly show the scope of the local changes.

Finally, macros, commands and environments can have arguments, which makes them more variable.

Stefan
LaTeX.org admin
LaTexLearner
Posts: 139
Joined: Tue Mar 10, 2015 11:06 am

Macros vs Defining Commands vs Defining Environments

Post by LaTexLearner »

Johannes_B wrote:...


LaTeX2e provides newcommand and newenvironment in different variations, TeX uses def which is low level and shouldn't be used without care and experience.
The LaTeX3 kernel, that is in development for the last 20 years, provides package xparse, that you can use with the current version of LaTeX. It provides very powerful extensions and tests and is really worth a look.
I have no idea what LaTeX2e, TeX, kernels, LaTeX3, current LaTeX or xparse are... Nor do I have any idea what a test is. :D
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Re: Macros vs Defining Commands vs Defining Environments

Post by Stefan Kottwitz »

Don't worry. LaTeX2e is current LaTeX. The other things were surely mentioned for completeness, but are more for advanced users, such as package authors or advanced macro programmers.

But if you would like to know something about any of those subjects, let us know (in a new topic).

Stefan
LaTeX.org admin
Post Reply