LaTeX forum ⇒ GeneralUse 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

Postby kostka » Wed Feb 17, 2010 9:44 pm

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

Example: I want to somehow define

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


such that

  1. \begin{boxthis}
  2. this text is boxed
  3. \end{boxthis}


is equivalent to

  1. \fbox{this text is boxed}


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

User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Postby frabjous » Wed Feb 17, 2010 9:52 pm

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

Postby kostka » Wed Feb 17, 2010 9:59 pm

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
  1. \newcommand{\boxedeqn}[1]{%
  2. \[\fbox{%
  3. \addtolength{\linewidth}{-2\fboxsep}%
  4. \addtolength{\linewidth}{-2\fboxrule}%
  5. \begin{minipage}{\linewidth}%
  6. \begin{equation}#1\end{equation}%
  7. \end{minipage}%
  8. }\]%
  9. }

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.

User avatar
localghost
Site Moderator
Posts: 9204
Joined: Fri Feb 02, 2007 12:06 pm

Postby localghost » Wed Feb 17, 2010 10:24 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
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes


¹ 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

Postby kostka » Thu Feb 18, 2010 12:18 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
  1. \newcommand{\boxedeqn}[1]{%
  2. \[\fbox{%
  3. \addtolength{\linewidth}{-2\fboxsep}%
  4. \addtolength{\linewidth}{-2\fboxrule}%
  5. \begin{minipage}{\linewidth}%
  6. \begin{equation}#1\end{equation}%
  7. \end{minipage}%
  8. }\]%
  9. }

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

Thanks for all the comments. :)

User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Postby frabjous » Thu Feb 18, 2010 12:35 am

Did you try the solution using fancybox and the Sbox environment?

Something along these lines:

  1. \documentclass{article}
  2. \usepackage{lipsum}
  3. \usepackage{fancybox}
  4. \newlength{\boxedeqnlen}
  5. \newenvironment{boxedeqn}{%
  6. \begin{Sbox}\setlength{\boxedeqnlen}{\linewidth}
  7. \addtolength{\boxedeqnlen}{-2\fboxsep}%
  8. \addtolength{\boxedeqnlen}{-2\fboxrule}%
  9. \begin{minipage}{\boxedeqnlen}%
  10. \begin{equation}}%
  11. {\end{equation}%
  12. \end{minipage}%
  13. \end{Sbox}\noindent\fbox{\TheSbox}}
  14. \begin{document}
  15. \lipsum[1-2]
  16. \begin{boxedeqn}
  17. e^{ix}=-1
  18. \end{boxedeqn}
  19. \lipsum[3-4]
  20. \begin{boxedeqn}
  21. 2+2=4
  22. \end{boxedeqn}
  23. \end{document}

kostka
Posts: 11
Joined: Tue Sep 15, 2009 3:53 am

Postby kostka » Thu Feb 18, 2010 12:52 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

Postby phi » Thu Feb 18, 2010 1:56 am

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:
  1. \documentclass{minimal}
  2.  
  3. \newsavebox{\mybox}
  4. \newenvironment{boxthis}{%
  5. \begin{lrbox}{\mybox}%
  6. }{%
  7. \end{lrbox}%
  8. \fbox{\usebox{\mybox}}%
  9. }
  10.  
  11. \begin{document}
  12.  
  13. \begin{boxthis}
  14. abc
  15. \end{boxthis}
  16.  
  17. \end{document}

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

Postby ignasi » Thu Feb 18, 2010 10:21 am

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

Postby kostka » Thu Feb 18, 2010 10:19 pm

Thanks for the replies, but I still haven't found a solution. The closest I can get using lrbox (or sbox) is:
  1. \documentclass{minimal}
  2.  
  3. \newsavebox{\mybox}
  4.  
  5. \newenvironment{equationbox}
  6. {\begin{lrbox}{\mybox}\begin{math}}
  7. {\end{math}\end{lrbox}\\%
  8. \fbox{\begin{minipage}{\linewidth}%
  9. \begin{equation}\usebox{\mybox}\end{equation}%
  10. \end{minipage}}%
  11. \\\ignorespacesafterend}
  12.  
  13. \begin{document}
  14.  
  15. The solution to
  16. \begin{equation}
  17. 2x-1=0
  18. \end{equation}
  19. is
  20. \begin{equationbox}
  21. x=\frac{1}{2}
  22. \end{equationbox}
  23. where we employ the use of a \textit{fraction}.
  24.  
  25. \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
  1. ...
  2. \newenvironment{equationbox}
  3. {\begin{lrbox}{\mybox}\begin{displaymath}}
  4. {\end{displaymath}\end{lrbox}\\%
  5. ...

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.


Return to “General”

Who is online

Users browsing this forum: No registered users and 12 guests