General\savebox in \newenvironment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
jdege
Posts: 6
Joined: Sun Nov 02, 2008 4:38 am

\savebox in \newenvironment

Post by jdege »

What I want is a new definition environment that will take a word and a definition, and format it in place in a \newtheorems environment, and will add the word and the definition to the glossary at the end of the document.

My idea is to use theorems.sty and glossaries.sty, and to define a \newenvironment that captures the content of the definition, and outputs a \newglossaryentry with the word and the definition, and wraps the word in a \gls{}.

So I'm trying to capture the contents of the environment in a \savebox. I'm not up to trying to save the contents of the environment, I'm just trying to save an arbitrary string in a savebox, during the \begin(), and then to output it in the \end{}. And as is typical, it's not working.

\newsavebox{\glossdefbox}
\newenvironment{glossdef}[1]{%
\begin{definition}#1%
\savebox{\glossdefbox}{arbitrary string}}{%
\usebox{\glossdefbox}\end{definition}}

\begin{glossdef}[word]
This is the definition.
\end{glossdef}

I've tried using saveboxes outside the environment definition, and they're working fine. But in the example above, either the arbitrary string isn't being saved to \glossdefbox in \begin{glossdef}, or the contents of \glossdefbox isn't being output in \end{glossdefbox}.

Whatever the reason, the string isn't displayed at all.

Any ideas?

--
Mellita, domi adsum.

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

\savebox in \newenvironment

Post by localghost »

Please create a complete minimal working example (MWE) to explain the problem more clearly. People are rarely motivated to do that themselves. Follow carefully the advices on the linked site when creating this MWE.


Best regards and welcome to the board
Thorsten¹
jdege
Posts: 6
Joined: Sun Nov 02, 2008 4:38 am

\savebox in \newenvironment

Post by jdege »

OK.

This works (or, rather it outputs the contents of the savebox):

Code: Select all

\documentclass{article}
\usepackage{ntheorem}
\newtheorem{definition}{Definition}
\newsavebox{\mybox}
\newenvironment{glossdef}{%
\begin{definition}\savebox{\mybox}{Extra stuff in a savebox}}{%
\usebox{\mybox}\end{definition}}
\begin{document}
\begin{glossdef}
This is a test.
\end{glossdef}
\end{document}
And produces this output;
Definition 1 This is a test. Extra stuff in a savebox
This does not work:

Code: Select all

\documentclass{article}
\usepackage{ntheorem}
\newtheorem{definition}{Definition}
\newsavebox{\mybox}
\newenvironment{glossdef}[1]{%
\begin{definition}#1\savebox{\mybox}{Extra stuff in a savebox}}{%
\usebox{\mybox}\end{definition}}
\begin{document}
\begin{glossdef}[word]
This is a test.
\end{glossdef}
\end{document}
And produces this output:
Definition 1 (word) This is a test.
Passing the argument seems to make the savebox not work.

Ideas?
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

\savebox in \newenvironment

Post by Juanjo »

Is this what you want?

Code: Select all

\documentclass{article}
\usepackage{ntheorem}
\newtheorem{definition}{Definition}
\newsavebox{\mybox}
\newenvironment{glossdef}[1]{%
\begin{definition}[#1]\savebox{\mybox}{Extra stuff in a savebox}}{%
\usebox{\mybox}\end{definition}}
\begin{document}
\begin{glossdef}{Word}
This is a test.
\end{glossdef}
\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
jdege
Posts: 6
Joined: Sun Nov 02, 2008 4:38 am

\savebox in \newenvironment

Post by jdege »

That's one step closer, thanks.

What I was trying to do was to capture the contents of the environment in the savebox. When that didn't work, I tried to capture an arbitrary string, and print that. When that didn't work, I went asking for help.

Your changes make my test work, thanks. But when I try to extend them to do what I'm actually trying to do, it doesn't work.

Trying something simple - this works:

Code: Select all

\documentclass{article}
\usepackage{ntheorem}
\newsavebox{\mybox}
\newenvironment{myenviron}{%
\savebox{\mybox}{test}}{%
\usebox{\mybox}}
\begin{document}
\begin{myenviron}
This is a test.
\end{myenviron}
\end{document}
This gives me syntax errors:

Code: Select all

\documentclass{article}
\usepackage{ntheorem}
\newsavebox{\mybox}
\newenvironment{myenviron}{%
\savebox{\mybox}{}{}%
\usebox{\mybox}}
\begin{document}
\begin{myenviron}
This is a test.
\end{myenviron}
\end{document}
How do I open the \savebox in the beginning of the environment, and the closing in the closing of the environment?
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

\savebox in \newenvironment

Post by phi »

With \bgroup and \egroup instead of the braces:

Code: Select all

\documentclass{article}
\usepackage{ntheorem}
\newsavebox{\mybox}
\newenvironment{myenviron}{%
\savebox{\mybox}\bgroup}{\egroup%
\usebox{\mybox}}
\begin{document}
\begin{myenviron}
This is a test.
\end{myenviron}
\end{document}
jdege
Posts: 6
Joined: Sun Nov 02, 2008 4:38 am

Re: \savebox in \newenvironment

Post by jdege »

That got me the rest of the way, thanks.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

\savebox in \newenvironment

Post by Juanjo »

IMHO, you should complete the definition or use the lrbox environment, as the following example shows:

Code: Select all

\documentclass{article}

\newsavebox{\mybox}

\newenvironment{myenviron}%
   {\savebox{\mybox}\bgroup}%
   {\egroup\usebox{\mybox}}
   
\newenvironment{Myenviron}%
   {\savebox{\mybox}\bgroup\ignorespaces}%
   {\egroup\usebox{\mybox}}

\newenvironment{MyEnviron}%
   {\begin{lrbox}{\mybox}}%
   {\end{lrbox}\usebox{\mybox}}
   
\begin{document}

\noindent Text before the tests.

\begin{myenviron}
  This is a test. There is an unwanted space at the beginning.
\end{myenviron}

\begin{Myenviron}
  This is a test. This works O.K.
\end{Myenviron}

\begin{MyEnviron}
  This is a test. This also works O.K.
\end{MyEnviron}

\end{document}
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
Post Reply