# Why doesn't this code make it possible to store/use a value?

I wrote this code

\documentclass{article}

% make a command called \insertfoo{..} for the user to insert foo
\newcommand{\insertfoo}[1]{%
\savebox{\boxthatcontainsfoo}{#1}%
}

% make a command called \showfoo that shows the value for foo
\newcommand{\showfoo}{%
\usebox{\boxthatcontainsfoo}%
}

\insertfoo{somefoo}

\begin{document}
The foo you choose was \showfoo .
end{document}
\end{document}


Why do I get a compiling error?

-
You have \newcommand{\}{% at the start of it all. That's causing all sorts of problems. I don't know if that's the real reason for your compilation error because you've only posted a snippet and not a compilable document. – Loop Space Jul 30 '12 at 13:51
(See minimal working example (MWE) for more details on MWEs) – Loop Space Jul 30 '12 at 13:53
Try without the braces around the box name. \savebox and \usebox expect a box, not a braced-group, as the next token (that's my understanding). – Loop Space Jul 30 '12 at 13:54
I fixed the typo. Thanks for pointing that out, but I don't see how this isn't a MWE. – Edward Stumperd Jul 30 '12 at 13:54
It's pretty close now, I'll admit. I should be able to cut-and-paste a single code block into my editor and run tex or latex or pdflatex directly on it without having to add anything else and see the problem. In this case it's not likely that the class is relevant, but in some cases knowing exactly what packages are loaded is crucial. – Loop Space Jul 30 '12 at 13:57

This is a MWE that works:

\documentclass{article}
% make a command called \insertfoo{..} for the user to insert foo
\newsavebox{\boxthatcontainsfoo}
\newcommand{\insertfoo}[1]{%
\savebox{\boxthatcontainsfoo}{#1}%
}

% make a command called \showfoo that shows the value for foo
\newcommand{\showfoo}{%
\usebox{\boxthatcontainsfoo}%
}
\begin{document}
\insertfoo{somefoo}
The foo you chose was \showfoo.

\insertfoo{some other foo}
The foo you chose was \showfoo.
\end{document}


You need to define the box that you use to store the content (\boxthatcontainsfoo), but also use \insertfoo{...} when passing arguments to the macro, not \insertfoo(...).

It is possible to modify \insertfoo to function with (round) brackets if needed.

-
That was exactly the problem. – Edward Stumperd Jul 30 '12 at 14:11