## LaTeX forum ⇒ General ⇒ Use unbalanced brackets in /newenvironment

LaTeX specific issues not fitting into one of the other forums of this category.
kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am

### Use unbalanced brackets in /newenvironment

I want to use unbalanced brackets in the start and end arguments of /newenvironment

Example: I want to somehow define

\newenvironment{boxthis}{ \fbox{ }  % <-- note this is unbalanced{ } } % <-- note this is unbalanced, but balances with the brackets in the above

such that

\begin{boxthis}this text is boxed\end{boxthis}

is equivalent to

\fbox{this text is boxed}

Is there a way to accomplish this? Thanks!
Last edited by kostka on Thu Feb 18, 2010 12:52 am, edited 1 time in total.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am
Can you just use the framed environment from the framed package? Or the Sbox environment of fancybox?

kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am
frabjous wrote:Can you just use the framed environment from the framed package? Or the Sbox environment of fancybox?

For this example, yes, but what I actually want is a lot more complicated. I just wanted the example to be quickly understood for anyone reading this forum.

I actually want to convert something close to
\newcommand{\boxedeqn}[1]{%   $\fbox{% \addtolength{\linewidth}{-2\fboxsep}% \addtolength{\linewidth}{-2\fboxrule}% \begin{minipage}{\linewidth}% \begin{equation}#1\end{equation}% \end{minipage}% }$%}

into an environment.

Edit: I suppose using the framed environment would work as well, as long as the functionality is identical to fbox, but I'd still like to know if the scenario in the OP is possible.

localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm
kostka wrote:[...] I suppose using the framed environment would work as well, as long as the functionality is identical to fbox, but I'd still like to know if the scenario in the OP is possible.

Don't suppose, just test. The framed package as suggested by frabjous is the solution to your scenario. But for emphasized equations the empheq package from the mh bundle is better.

Best regards
Thorsten
LaTeX Community Moderator

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1

kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am
localghost wrote:
kostka wrote:[...] I suppose using the framed environment would work as well, as long as the functionality is identical to fbox, but I'd still like to know if the scenario in the OP is possible.

Don't suppose, just test. The framed package as suggested by frabjous is the solution to your scenario. But for emphasized equations the empheq package from the mh bundle is better.

Best regards
Thorsten

Thanks for mentioning the empheq package, which I was not aware of.

Anyhow, the framed environment does not seem to work as 1) it resets the paragraph indentation for the next line, even if no space is skipped and 2) it adds too much space (though this point can likely be fixed).

The empheq package doesn't seem to be able to box both the equation and its number, which is what I'm after.

For now I suppose I'll have to settle for using
\newcommand{\boxedeqn}[1]{%   $\fbox{% \addtolength{\linewidth}{-2\fboxsep}% \addtolength{\linewidth}{-2\fboxrule}% \begin{minipage}{\linewidth}% \begin{equation}#1\end{equation}% \end{minipage}% }$%}

which produces exactly the behavior I want, even though it makes boxed equations look different in the .tex file from normal equations.

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am
Did you try the solution using fancybox and the Sbox environment?

Something along these lines:

\documentclass{article}\usepackage{lipsum}\usepackage{fancybox}\newlength{\boxedeqnlen}\newenvironment{boxedeqn}{%       \begin{Sbox}\setlength{\boxedeqnlen}{\linewidth}       \addtolength{\boxedeqnlen}{-2\fboxsep}%       \addtolength{\boxedeqnlen}{-2\fboxrule}%       \begin{minipage}{\boxedeqnlen}%       \begin{equation}}%       {\end{equation}%       \end{minipage}%       \end{Sbox}\noindent\fbox{\TheSbox}}\begin{document}\lipsum[1-2]\begin{boxedeqn}e^{ix}=-1\end{boxedeqn}\lipsum[3-4]\begin{boxedeqn}2+2=4\end{boxedeqn}\end{document}

kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am
frabjous wrote:Did you try the solution using fancybox and the Sbox environment?

...

Thanks! This needs some work to get spacing how I want, but it will work.

I'm going to mark this topic as solved, but I'm still surprised there isn't an easy way to use unbalanced brackets inside of an argument.

phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm
kostka wrote:I'm going to mark this topic as solved, but I'm still surprised there isn't an easy way to use unbalanced brackets inside of an argument.

It's a syntactic problem: The \newenvironment command needs to know where its arguments end, and allowing unbalanced braces would result in ambiguities.
It is possible to grab the contents of an environment as a macro parameter (in fact, this is done by most of the amsmath environments), but generally you should rely on packages that provide a higher level abstraction.
Something that is always possible and requires no tricks is grabbing the contents in a LR box using the lrbox environment:
\documentclass{minimal} \newsavebox{\mybox}\newenvironment{boxthis}{%   \begin{lrbox}{\mybox}%}{%   \end{lrbox}%   \fbox{\usebox{\mybox}}%} \begin{document} \begin{boxthis}   abc\end{boxthis} \end{document}

ignasi
Posts: 20
Joined: Tue Jul 28, 2009 5:10 pm
Hi,

I think you can use \bgroup and \egroup commands instead of open and close brackets. Look at this thread, maybe it helps:

http://www.latex-community.org/forum/viewtopic.php?f=5&t=2945

Ignasi

kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am
Thanks for the replies, but I still haven't found a solution. The closest I can get using lrbox (or sbox) is:
\documentclass{minimal} \newsavebox{\mybox} \newenvironment{equationbox}{\begin{lrbox}{\mybox}\begin{math}}{\end{math}\end{lrbox}\\%\fbox{\begin{minipage}{\linewidth}%\begin{equation}\usebox{\mybox}\end{equation}%\end{minipage}}%\\\ignorespacesafterend} \begin{document} The solution to\begin{equation}2x-1=0\end{equation}is\begin{equationbox}x=\frac{1}{2}\end{equationbox}where we employ the use of a \textit{fraction}. \end{document}

But since I want equationbox to mimic equation, I need it to display in the displaymath style. Bizzarly, changing a portion of the code to
...\newenvironment{equationbox}{\begin{lrbox}{\mybox}\begin{displaymath}}{\end{displaymath}\end{lrbox}\\%...

causes latex to break and complain about a missing \$ when it reaches the \frac command.

Still working on it. I'll reply if I find something.