General ⇒ \savebox in \newenvironment
\savebox in \newenvironment
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.
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.
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
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¹
Best regards and welcome to the board
Thorsten¹
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
\savebox in \newenvironment
OK.
This works (or, rather it outputs the contents of the savebox):
And produces this output;
And produces this output:
Ideas?
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}
This does not work:Definition 1 This is a test. Extra stuff in a savebox
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}
Passing the argument seems to make the savebox not work.Definition 1 (word) This is a test.
Ideas?
\savebox in \newenvironment
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.
\savebox in \newenvironment
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:
This gives me syntax errors:
How do I open the \savebox in the beginning of the environment, and the closing in the closing of the environment?
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}
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}
\savebox in \newenvironment
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}
Re: \savebox in \newenvironment
That got me the rest of the way, thanks.
\savebox in \newenvironment
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.