Generalxcolor, savebox, bgroup problem

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
beeper
Posts: 4
Joined: Tue Jan 11, 2011 6:25 pm

xcolor, savebox, bgroup problem

Post by beeper »

Although they do not seem related to me, I am having trouble using the xcolor package with a savebox and bgroup.

\documentclass{article}
\usepackage{xcolor}
\newsavebox{\mybox}
\savebox{\mybox}\bgroup hello world \egroup
\begin{document}
\usebox{\mybox}

\bgroup good bye world \egroup
\end{document}

gives me a missing "}" error with texlive 2010. If I do not use the package xcolor, I do not get an error. If I replace:
\savebox{\mybox}\bgroup hello world \egroup
with:
\savebox{\mybox}{hello world}
I do not get an error.

What am I doing wrong?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

xcolor, savebox, bgroup problem

Post by localghost »

Usually the argument for the \savebox command needs to be grouped with curly braces.

Code: Select all

\documentclass{article}
\usepackage{xcolor}

\newsavebox{\mybox}
\savebox{\mybox}{\bgroup hello world \egroup}

\begin{document}
\usebox{\mybox}

\bgroup good bye world \egroup
\end{document}

Best regards and welcome to the board
Thorsten
beeper
Posts: 4
Joined: Tue Jan 11, 2011 6:25 pm

xcolor, savebox, bgroup problem

Post by beeper »

Thank you, I have now read the rules.
localghost wrote:Usually the argument for the \savebox command needs to be grouped with curly braces.
Yes, I know. A slightly less minimal example that I think requires \bgroup and \egroup instead of curly braces is:

Code: Select all

\documentclass{article}
\usepackage{xcolor}
\newsavebox{\mybox}
\newenvironment{myenv}{\savebox{\mybox}\bgroup}{\egroup\usebox{\mybox}}
\begin{document}
	\begin{myenv}hello world\end{myenv}
	
	\bgroup good bye world \egroup
\end{document}
which results in:
! Missing } inserted.
<inserted text>
}
l.6 \begin{myenv}
hello world\end{myenv}
?
! Emergency stop.

when the xcolor package is used, but no error when the xcolor package is not used.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

xcolor, savebox, bgroup problem

Post by localghost »

I don't see any sense in your definition of the new environment.

Code: Select all

\newenvironment{myenv}{\savebox{\mybox}\bgroup}{\egroup\usebox{\mybox}
The additional grouping is superfluous since an environment itself already represents a group.
beeper
Posts: 4
Joined: Tue Jan 11, 2011 6:25 pm

xcolor, savebox, bgroup problem

Post by beeper »

I was trying to add some text in an environment and then use that text elsewhere. The environ package lets me do this pretty easily, but before I found that package my solution involved:

Code: Select all

\savebox{\mybox}\bgroup hello world \egroup
which worked fine until I loaded the xcolor package. While I understand that this solution is potentially flawed and that one might not ever want to make a \savebox with \bgroup and \egroup, I am really just curious why the xcolor package breaks my solution.

localghost, thanks for the help so far.
Post Reply