# global confusion

Simply put, when should I care if something is global or not? What does this really mean?

If my package, for example, provides

\newcommand*{\setfooter}[1]{\def\mypackage@footer{#1}}


And I later use \mypackage@footer inside the package, is it correct that i used \def and not \gdef?

To append to \maketitle, why do i use \gappto{\maketitle}{\thispagestyle{fancy}} (\gappto is from etoolbox, equivalent to \g@addto@macro) rather than simply \appto, when both seem to work? (from this answer)

-

If you issue

\setfooter{foo}


at the top level, that is not inside a group or an environment (and document doesn't count as environment in this respect), there's no difference.

However, an input such as

{\setfooter{foo}}


would result in \mypackage@footer not be defined any more as soon as the closing brace is scanned.

If you suspect that users of your package may issue \setfooter in a group, then use \gdef.

Why using \gappto and not \appto? That's the same reasoning. I'd recommend to use "global" in both cases, so you won't incur in obscure malfunctions.

In the answer you link, Werner used \g@addto@macro because the LaTeX kernel doesn't provide a "local" equivalent.

-
So it would make sense to "default" to global definitions unless I have a reason not to? –  Mk12 Jun 29 '12 at 20:07
@Mk12 No: in general local definitions should be preferred; it depends on what you're defining: something that should remain the same throughout the document is a candidate to be defined globally or at the top level (and usually in the preamble). Something that must transcend the group in which it possibly appears also should be "globally defined". –  egreg Jun 29 '12 at 20:10
With my footer example, that should remain the same throughout the document, but it also should be defined in the preamble at top level—to me it really doesn't make sense to use it any other way. –  Mk12 Jun 29 '12 at 20:11

Macro definitions are considered as the "replacement text" for the given "macro and argument". So, with a definition like

\newcommand*{\setfooter}[1]{\def\mypackage@footer{#1}}


the use of \setfooter in your code is replaced with \def\mypackage@footer{#1}. If \setfooter is [not] inside a group, then \def\mypackage@footer{#1} will also [not] be inside that group. So, if the usage the macro and its eventual replacement is not required globally (outside the group), you don't need to declare it globally (with a \gdef or \xdef).

Here's a small example highlighting the above discussion:

\documentclass{article}
\newcommand{\mymacro}[1]{\def\myothermacro{#1}}
\newcommand{\mygmacro}[1]{\gdef\myothergmacro{#1}}
\begin{document}
\begingroup% Start a group
\mymacro{test1}% Local definition of \myothermacro as test1
\endgroup% End a group
\show\myothermacro% \myothermacro is undefined
\mymacro{test1}% Local definition of \myothermacro as test1
\show\myothermacro% \myothermacro is defined as test1
\myothermacro% test1

\begingroup% Start a group
\mygmacro{test2}% Global definition of \myothergmacro as test2
\endgroup% End a group
\show\myothergmacro% \myothergmacro is defined as test2
\mygmacro{test2}% Global definition of \myothergmacro as test2
\show\myothergmacro% \myothergmacro is defined as test2
\myothergmacro% test2
\end{document}​


which yields the .log output of

> \myothermacro=undefined.
l.8 \show\myothermacro

> \myothermacro=macro:
->test1.
l.10 \show\myothermacro

> \myothergmacro=macro:
->test2.
l.16 \show\myothergmacro

> \myothergmacro=macro:
->test2.
l.18 \show\myothergmacro


Note that \myothermacro is undefined on l.8, since its definition was local on l.6 (inside a group). \myothergmacro, however, is defined as test2 on l.16, since it was globally defined (or \gdef-ed) on l.14.

It is also possible to precede the required global definition with \global, if its needed. So, in the above example, using

\global\mymacro{test1}


would translate to

\global\def\myothermacro{test1}


which is equivalent to

\gdef\myothermacro{test1}


Note that some commands "transcend" groups. See, for example, Why do \setcounter and \addtocounter commands have global effect, while \setlength and \addtolength commands obey the normal scoping rules?

-