TeX - LaTeX Asked by Franck on January 4, 2021
I’m very new here even if I’ve used Stackexchange looking for answers a lot. For once I can’t find the one I’m looking for so I have to ask it.
I’m a bit afraid it is a very simple question but I’ve spent 2 days on it without finding the answer.
Let’s say I’ve defined a new command newcommand{boldit}[1]{textbf{#1}}
(I know this one is not really useful).
Now I’d like to build an environment out of it. But I have some problem with the braces:
newenvironment{makeitbold}{%
boldit{
}{%
}
}
I tried to replace some pair with either a pair of bgroup egroup
or begingroup endgroup
but it doesn’t work.
I also tried to change the command using longdef
instead of newcommand
to be able to use some other delimiters (for instance I tried <>
as delimiters). It worked for the command itself but not for the environment.
To sum it up, I’d like to build an environment which passes its whole (or part of its) content to a command.
I hope I explained it well.
Usually one does it the other way around: first define an environment and from it derive a command.
However, there are places where this can be useful. As far as I can tell, the problem arose with amsmath
(more precisely in the original amslatex
incarnation). This package was derived from amstex
and constructs such as align...endalign
were to become begin{align}...end{align}
in order to become LaTeX-savvy.
The align<math material>endalign
construct works by absorbing <math material>
as an argument to align
delimited by endalign
. Then the material could be processed twice in order to first measure it and then print it.
A method was devised for doing the same in LaTeX, by absorbing the material as the argument to a macro, delimited by end{align}
. I'd not enter the details of the procedure. Some years later, other people discovered that the same mechanism can be useful also in different situations, when we need the contents of the environment available for massaging, measuring, splitting it to later output what we need from it.
This made the package environ
to appear. So if you do
usepackage{environ}
newcommand{boldit}[1]{textbf{#1}}
NewEnviron{makeitbold}{boldit{BODY}}
you have the result you're aiming for. Of course this is just a (bad) example of usage.
A better replacement is with xparse
, where you can pass a b
argument type to NewDocumentEnvironment
like
usepackage{xparse}
NewDocumentCommand{boldit}{m}{textbf{#1}}
NewDocumentEnvironment{makeitbold}{b}
{boldit{#1}}
{}
You can use +b
if your environment is supposed to contain whole paragraphs.
Correct answer by egreg on January 4, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP