Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Andrew Stacey Jul 30 '12 at 13:51
    
(See minimal working example (MWE) for more details on MWEs) –  Andrew Stacey 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). –  Andrew Stacey 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. –  Andrew Stacey Jul 30 '12 at 13:57
show 4 more comments

1 Answer

up vote 7 down vote accepted

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.

share|improve this answer
    
That was exactly the problem. –  Edward Stumperd Jul 30 '12 at 14:11
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.