Text Formatting ⇒ Macros vs Defining Commands vs Defining Environments
-
- Posts: 139
- Joined: Tue Mar 10, 2015 11:06 am
Macros vs Defining Commands vs Defining Environments
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
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
}
\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.
- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
Macros vs Defining Commands vs Defining Environments
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
-
- Posts: 139
- Joined: Tue Mar 10, 2015 11:06 am
Macros vs Defining Commands vs Defining Environments
I have no idea what LaTeX2e, TeX, kernels, LaTeX3, current LaTeX or xparse are... Nor do I have any idea what a test is.Johannes_B wrote:...
LaTeX2e providesnewcommand
andnewenvironment
in different variations, TeX usesdef
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.

- Stefan Kottwitz
- Site Admin
- Posts: 10335
- Joined: Mon Mar 10, 2008 9:44 pm
Re: Macros vs Defining Commands vs Defining Environments
But if you would like to know something about any of those subjects, let us know (in a new topic).
Stefan