As noted in comments the standard minipage
and lrbox
environments are designed for this use, or for the particular case of framing a box, mdframed
package.
To note why the definition shown does not work, you can not delimit macro arguments with \begingroup
.
\fbox \begingroup
is the same as
\fbox{\begingroup}
and passes the \begingroup
token as the content of the box. After that things are bound to go wrong.
Similarly
\parbox{10cm} \begingroup
is
\parbox{10cm}{\begingroup}
with \begingroup
being passed as the argument to \parbox
.
This uses an environment - wrapper, via \NewDocumentCommand
from xparse
package for possible better configurability (and \par
content as argument values).
Possible vertical spacing issues are not addressed.
It's however not possible to say \mytheorem{Title} content ...
since the environment needs a clear end.
\documentclass{article}
\usepackage{xparse}
\usepackage{amsthm}
\newtheorem{theorem}{Theorem}[section]
\renewcommand{\thetheorem}{\arabic{theorem}}
\NewDocumentCommand{\mytheorem}{+m+m}{%
\begin{theorem}{#1}
#2
\end{theorem}
}
\begin{document}
\section{First}
\mytheorem{Theory on Brontosaurs, by Miss Ann Elk}{%
Brontosaurs are thin at one end, thick in the middle and thin again at the other end.
}
\end{document}
Edit: Update ... a 'generic' wrapper command for such theorem environments
This defines a generic wrapper command for such newtheorem
issues:
\GenericWrapper[section]{theoremname}{Theoremname}
will generate a command \mytheoremname
with is wrapper for \begin{theoremname}...\end{theoremname}
and reset every section (default value of the optional 1st. argument). (xparse
is used with \NewDocumentCommand
for this, in order to facilitate the usage of optional arguments in contrast to the \newcommand[][]
way.)
The command changes the theorem label to \arabic{theoremname}
.
\documentclass{article}
\usepackage{xparse}
\usepackage{amsthm}
\NewDocumentCommand{\GenerateWrapper}{+O{section}+m+m}{%
\newtheorem{#2}{#3}[#1]
\expandafter\renewcommand\csname the#2\endcsname{\arabic{#2}}%
\expandafter\DeclareDocumentCommand\csname my#2\endcsname{+m}{%
\begin{#2}%
##1%
\end{#2}
}%
}%
\GenerateWrapper{theorem}{Theorem}%
\GenerateWrapper{exercise}{Exercise}%
\GenerateWrapper{question}{Question}%
\begin{document}
\section{First}
\mytheorem{%
Theory on Brontosaurs, by Miss Ann Elk%
Brontosaurs are thin at one end, thick in the middle and thin again at the other end.
}%
\myexercise{Find a Brontosaur%
Excavate a Brontosaur
}%
\myquestion{Mathematics
Prove that
\[ \int\limits_{-\pi}^{\pi} e^{-x^2} = \sqrt{\pi} \]
}%
\end{document}
Best Answer
\begin
and\end
build a group, which can keep settings local. Simply writing\command ... \endcommand
doesn't. Such a scope is very important for me, that's why I prefer the\begin ... \end
syntax.Example: