Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How Do I Use \newcommand Inside An Environment?

1,088 views
Skip to first unread message

Kurt R. Todoroff

unread,
Dec 11, 2011, 2:49:15 PM12/11/11
to
I want to do the following:

\begin{quote}

Jack and Jill went up the hill etcetera.\label{jack-and-jill-example}
\newcommand{\jackandjill}{Jack and Jill should not have walked up the
hill. See Page \pageref{jack-and-jill-nursery-rhyme}.}

Old Mother Hubbard went to the cupboard
etcetera.\label{old-mother-hubbard-example}
\newcommand{\oldmotherhubbard}{Old Mother Hubbard should have kept her
cupboard stocked. See Page \pageref{old-mother-hubbard-nursery-rhyme}.}

more of the same

more of the same

\end{quote}


.
.
.

[several chapters later]


\textbf{LESSONS}

\begin{itemize}

\item \jackandjill

\item \oldmotherhubbard

\end{itemize}



LaTeX does not recognize the new commands, \jackandjill and
\oldmotherhubbard, that I created inside of the Quote environment. I
tried preceding the \newcommands with \protect, but this did not solve
the problem. I also tried placing \newcommand inside of an itemize
environment and inside of an enumerate environment, unsuccessfully. Is
there a method to get LaTeX to recognize \newcommand inside of an
environment?

Thank you.

--


Kurt Todoroff

Markets, not mandates and mob rule.
Consent, not coercion.

Martin Scharrer

unread,
Dec 11, 2011, 3:44:55 PM12/11/11
to
Environments create groups around their content, so any new
command/macro definition is local to this group and will be reset to its
original definition (which includes being undefined) after the end of
the environment. In general, you should avoid define macros inside
groups when you plan to use them globally. Define them in the preamble
instead.
You can however make the definition global, either by using
\gdef\macroname{..} instead of \newcommand{\macroname}{..} or by using
\global\let\macroname=\macroname after your \newcommand.
Note that \gdef doesn't has an optional argument to specify the number
of arguments, instead you need to list them in order, e.g. three
arguments is not [3] but #1#2#3 (after the macro name).

Best Regards,
Martin Scharrer



Enrico Gregorio

unread,
Dec 11, 2011, 5:51:49 PM12/11/11
to
Kurt R. Todoroff <kurt.r....@comcast.net> wrote:

> I want to do the following:
>
> \begin{quote}
>
> Jack and Jill went up the hill etcetera.\label{jack-and-jill-example}
> \newcommand{\jackandjill}{Jack and Jill should not have walked up the
> hill. See Page \pageref{jack-and-jill-nursery-rhyme}.}
>
> [...]
>
> LaTeX does not recognize the new commands, \jackandjill and
> \oldmotherhubbard, that I created inside of the Quote environment. I
> tried preceding the \newcommands with \protect, but this did not solve
> the problem. I also tried placing \newcommand inside of an itemize
> environment and inside of an enumerate environment, unsuccessfully. Is
> there a method to get LaTeX to recognize \newcommand inside of an
> environment?

Martin has given the right answer: TeX "kills" assignments
made in a group (for instance, an environment).

Either say

\gdef\jackandjill{...}

or the more complicated

{\globaldefs=1 \newcommand{\jackandjill}{...}}

However, putting together all personal commands in the
document preamble is better, as there's only that place
to check for those personal commands.

Ciao
Enrico

Kurt R. Todoroff

unread,
Dec 11, 2011, 6:53:22 PM12/11/11
to
In article <jc34o3$d1v$1...@speranza.aioe.org>,
Gentlemen,

Thank you for your replies. I have always placed my document specific
\newcommands in the preamble and my global \newcommands in a separate
file. This is the first time that I have needed to embed them elsewhere
in the document.

Kurt R. Todoroff

unread,
Dec 11, 2011, 7:06:47 PM12/11/11
to
In article <jc34o3$d1v$1...@speranza.aioe.org>,
Martin Scharrer <mar...@scharrer-online.de> wrote:

Gentlemen,

\gdef\macroname{..} worked perfectly. Thank you very much.
0 new messages