General ⇒ \savebox in \newenvironment
\savebox in \newenvironment
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.
Learn LaTeX easily with newest books:
The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis
The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more
LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis
- localghost
- Site Moderator
- Posts: 9202
- Joined: Fri Feb 02, 2007 12:06 pm
\savebox in \newenvironment
Best regards and welcome to the board
Thorsten¹
Board Rules
Avoidable Mistakes
¹ System: TeX Live 2025 (vanilla), TeXworks 0.6.10
\savebox in \newenvironment
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
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}
\savebox in \newenvironment
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
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
\savebox in \newenvironment
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}