7

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?

9
  • 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. Jul 30, 2012 at 13:51
  • (See minimal working example (MWE) for more details on MWEs) Jul 30, 2012 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). Jul 30, 2012 at 13:54
  • I fixed the typo. Thanks for pointing that out, but I don't see how this isn't a MWE. Jul 30, 2012 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. Jul 30, 2012 at 13:57

1 Answer 1

7

This is a MWE that works:

enter image description here

\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.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .