# Savebox from within enumerate [duplicate]

I'm completely puzzled by this: I'm trying to create a simple command that will modify a savebox from an enumerate, and I've worked it down to a minimal example of some weirdness:

...
% Create our command
\newcommand{\hello}{
\savebox{\tmp}{Works!}
}
% Setup the box
\newsavebox{\tmp}
\savebox{\tmp}{Doesn't work!}

\hello

\usebox{\tmp}
% Will put: Works!


If I run the command from inside an enumerate, it does not work:

% Create our command
\newcommand{\hello}{
\savebox{\tmp}{Works!}
}
% Setup the box
\newsavebox{\tmp}
\savebox{\tmp}{Doesn't work!}

\begin{enumerate}
\item
Something \hello
\end{enumerate}

\usebox{\tmp}
% Will put: Doesn't work!


I have absolutely no clue why it does this. No warnings or errors are given, and it does this on both pdfLatex and XeLaTex (the only two I had available to test). Any clues would be much appreciated!

## marked as duplicate by egreg macros StackExchange.ready(function() { if (StackExchange.options.isMobile) return; $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var$hover = $(this).addClass('hover-bound'),$msg = $hover.siblings('.dupe-hammer-message');$hover.hover( function() { $hover.showInfoMessage('', { messageElement:$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); May 13 at 17:17

This issue is that the \savebox is exercised in a group, and is scope limited to within the enumerate.

Stealing David's answer here, Create a \savebox in a Group and have it available outside of group, and applying it to the OP's situation. In essence, substitute the primitives for \savebox and make them \global.

\documentclass{article}
\begin{document}
% Create our command
\newcommand{\hello}{
\global\expandafter\setbox\csname tmp\endcsname\hbox{Works!}
}
% Setup the box
\newsavebox{\tmp}
\savebox{\tmp}{Doesn't work!}

\begin{enumerate}
\item
Something \hello
\usebox{\tmp} inside group

\end{enumerate}

\usebox{\tmp} outside group
% Will put: Doesn't work!
\end{document}


Technically, a given savebox should always be handled globally or locally. Here is one way to do that, by introducing global macros

\documentclass{article}
\newcommand\gnewsavebox[1]{\global\newsavebox#1}
\newcommand\gsavebox[2]{\global\setbox#1\hbox{#2}}
\begin{document}
% Create our command
\newcommand{\hello}{\gsavebox{\tmp}{Works!}
}
% Setup the box
\gnewsavebox{\tmp}
\gsavebox{\tmp}{Doesn't work!}

\begin{enumerate}
\item
Something \hello
\usebox{\tmp} inside group

\end{enumerate}

\usebox{\tmp} outside group
% Will put: Doesn't work!
\end{document}