GeneralCreate own box environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
tehingo
Posts: 6
Joined: Tue Oct 28, 2008 1:51 pm

Create own box environment

Post by tehingo »

Hey guys,

my goal is to create simple new environment. The plan is, to have a box with a simple line frame around it. However, the following code does not work, since LaTeX gives me the following error when trying to compile it.

Code: Select all

 \newenvironment{exampleo}%
 {%
 \begin{framebox}
 \textbf{\textsf{EXAMPLE\vspace*{2em}}}%
 }
 {%
 \end{framebox}%
 }%

Code: Select all

Argument of \textbf has an extra }. \begin{exampleo}
It would be really nice if you could quickly help me out with this. It would be a great contribution for Googles everlasting archives ;-).

Thanks in advance. Cheers!
Ingo

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

Create own box environment

Post by Stefan Kottwitz »

Hi Ingo,

welcome to the board!
\framebox is not suitable. Try the framed environment of the framed package instead:

Code: Select all

\usepackage{framed}
...
\newenvironment{exampleo}%
{%
\begin{framed}
  \textbf{\textsf{EXAMPLE}}\vspace*{2em}%
}
{%
\end{framed}%
}%
Btw. "Example" in caps and bold is much emphazation, I wouldn't use uppercase words.

Stefan
LaTeX.org admin
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

Create own box environment

Post by phi »

Hey,

\framebox is a command that expects the things to be put inside the frame as an argument. You cannot use it that way. With your code, the \framebox command only gets the \textbf token as argument.
You can use the framed package to make frame environments (look into the source code framed.sty for examples):

Code: Select all

\usepackage{framed}
\newenvironment{exampleo}{\framed\textbf{\textsf{EXAMPLE\vspace*{2em}}}}{\endframed}
If you want to mimic the behavior of \framebox, you can use a neat trick from the AMSLaTeX packages to convert environment contents into a macro argument:

Code: Select all

\usepackage{amsmath}
\makeatletter
\newenvironment{exampleo}{\collect@body\framebox\textbf{\textsf{EXAMPLE\vspace*{2em}}}}{}
\makeatother
This won't work if your environment contains paragraph separators; also the vertical space doesn't work. The framed package is the best solution.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Create own box environment

Post by Juanjo »

Concerning the framed package, a forum search will bring you some threads about it, like this one, from which you can take examples. However, if you plan to use your environment like minipage, try the boxedminipage environment, defined in the boxedminipage package. It behaves like minipage, but with a frame around. It has the same syntax[*]:

Code: Select all

\begin{boxedminipage}[pos]{width}  text \end{minipage}
where pos is one of usual descriptors t, b or c (the default is c). Other very useful package is fancybox. Its manual provides a short and very instructive course on boxes, with many examples. Surely, one of them may be almost what you need. Don't miss it!


[*] To be precise, minipage has two more optional arguments to control the box height and the internal position of text.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
tehingo
Posts: 6
Joined: Tue Oct 28, 2008 1:51 pm

Re: Create own box environment

Post by tehingo »

Thanks a lot for your help. This made my life much easier and my publication looking much better.

I will try to return the favour!
martin_scharrer
Posts: 3
Joined: Tue Apr 28, 2009 6:51 pm

Create own box environment

Post by martin_scharrer »

I know that the question is quite old, but for reference:

You can store the whole environment content in a TeX box register (think \savebox und \usebox). This is faster and better then \collect@body, because it doesn't scan the TeX code twice and therefore also allows for verbatim and similar fragile code.

To make this work the plain TeX \setbox is used instead of \savebox to avoid the need for an macro argument.

Code: Select all

\makeatletter
\newenvironment{myboxenv}
  {\setbox\z@\hbox\bgroup\color@setgroup}
  {\color@endgroup\egroup\myboxmacro{\usebox\z@}}
\makeatother
The temp box \z@ is used here, but any other box defined by \newsavebox works also fine. The \bgroup and \egroup mark the begin and end of the box. The \color@setgroup and \color@endgroup handle some color specific issues.

After saving the box content it can be used as an macro argument as shown
above with

Code: Select all

\myboxmacro{\usebox\z@}
.

For understanding compare the definition of \sbox (used by \savebox):

Code: Select all

\sbox:
\long macro:#1#2->\setbox #1\hbox {\color@setgroup #2\color@endgroup }.
Post Reply