Graphics, Figures & TablesCustom-boxes with float??

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
kirik
Posts: 4
Joined: Tue Jul 20, 2010 11:43 am

Custom-boxes with float??

Post by kirik »

Hi,

I would like to have infoboxes in my thesis work, with background color, where I can define parameters and some code snippets. I have the following code as of now, which is sort of what I want except the boxes end up showing huge margins top and bottom, and they are not floating in text, leaving large gaps in text.

Code: Select all

% info-boxes
\definecolor{MyGray}{rgb}{0.95,0.95,0.95}
\makeatletter\newenvironment{graybox}{%
    \begin{center}
        \begin{lrbox}{\@tempboxa}\begin{minipage}{0.9\textwidth}}{\end{minipage}\end{lrbox}%
	\colorbox{MyGray}{\usebox{\@tempboxa}}
    \end{center}
}\makeatother
I have done some searching on google, and I have found some tips for defining new float environments. The following code, for example, creates very neat floatboxes; however they are not colored and they are a bit too wide for my eye..

Code: Select all

%infoboxes
\usepackage{float}

%allows use of "@" before \begin{document}
\makeatletter
% this creates a custom and simpler ruled box style
\newcommand\floatc@simplerule[2]{{\@fs@cfont #1 #2}\par}
\newcommand\fs@simplerule{\def\@fs@cfont{\bfseries}\let\@fs@capt\floatc@simplerule
  \def\@fs@pre{\hrule height.8pt depth0pt \kern4pt}%
  \def\@fs@post{\kern4pt\hrule height.8pt depth0pt \kern4pt \relax}%
  \def\@fs@mid{\kern8pt}%
  \let\@fs@iftopcapt\iftrue}
\makeatother

%this code block defines the new and custom floatbox float environment
\floatstyle{simplerule}
\newfloat{floatbox}{thp}{lob}[section]
\floatname{floatbox}{Text Box}
Is there a way to combine these two code snippets to produce a new floating environment which has the same caption as the floatbox, but a custom width and a custom background color?

Thanks in advance!

Recommended reading 2024:

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

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

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Custom-boxes with float??

Post by gmedina »

Hi,

you can try something similar to the myenv new environment that I defined in the following simple example code:

Code: Select all

\documentclass{article}
\usepackage{caption}
\usepackage{framed}
\usepackage{xcolor}

% definition of a new float type (refer to the caption package documentation)
\DeclareCaptionType{mytype}

% definition of a shaded-like environment (see framed.sty)
\newenvironment{myshaded}
  {\def\FrameCommand{\colorbox{shadecolor}}%
    \MakeFramed {\advance\hsize-\width \FrameRestore}}%
 {\endMakeFramed}

% main environment
% syntax: \begin{myenv}{placement-specifiers}{color}{width}...\end{myenv}
\newenvironment{myenv}[3]
  {\colorlet{shadecolor}{#2}%
    \begin{mytype}[#1]%
    \noindent\begin{minipage}{#3}
      \begin{myshaded}}
  {\end{myshaded}\end{minipage}\end{mytype}}

\begin{document}

\begin{myenv}{!ht}{blue!20}{5cm}
Test example 1
\end{myenv}

\begin{myenv}{!ht}{red!20}{\textwidth}
Test example 2
\end{myenv}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
kirik
Posts: 4
Joined: Tue Jul 20, 2010 11:43 am

Custom-boxes with float??

Post by kirik »

Thanks for your reply gmedina,

I have tried out your code and I like the fact that it's more flexible in terms of choosing color, vertical placement, and width. However I am having hard times decrypting the code. You modify an already defined style (framed) or what is happening there essentially? I got confused since you don't define a new float, or you somehow manage to do so without:

Code: Select all

\usepackage{float}
\floatstyle{simplerule}
\newfloat{floatbox}{thp}{lob}[section]
\floatname{floatbox}{Info Box}
Is it possible to create something similar like yours using the float package? Or, let me reverse the question, is it possible to use a custom defined rule to style a float defined as you have posted?
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Custom-boxes with float??

Post by gmedina »

kirik wrote:...However I am having hard times decrypting the code. You modify an already defined style (framed) or what is happening there essentially? I got confused since you don't define a new float, or you somehow manage to do so without:

Code: Select all

\usepackage{float}
\floatstyle{simplerule}
\newfloat{floatbox}{thp}{lob}[section]
\floatname{floatbox}{Info Box}
The command \DeclareCaptionType internally creates a new floating object (refer to the documentation of the caption package for details). The environment myenv that I defined in my previous reply simply uses a variant of the shaded environment (as defined in framed.sty); this shaded-like environment is then boxed using a minipage to allow control over the width and finally the box is placed inside the floating object given by mytype.
kirik wrote:...Is it possible to create something similar like yours using the float package? Or, let me reverse the question, is it possible to use a custom defined rule to style a float defined as you have posted?...
To the first question I would answer "Maybe" (I haven't done any tests, so I cannot be sure, but I suspect that it's possible). The answer to the second question is "Yes": try the following modification to my previous example (now the floating environment starts and ends with a rule):

Code: Select all

\documentclass{article}
\usepackage{caption}
\usepackage{framed}
\usepackage{xcolor}

% definition of a new float type (refer to the caption package documentation)
\DeclareCaptionType{mytype}

% definition of a shaded-like environment (see framed.sty)
\newenvironment{myshaded}
  {\def\FrameCommand{\setlength\fboxsep{0pt}\colorbox{shadecolor}}%
    \MakeFramed {\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}

% main environment
% syntax: \begin{myenv}{placement-specifiers}{color}{width}...\end{myenv}
\newenvironment{myenv}[3]
  {\colorlet{shadecolor}{#2}%
    \begin{mytype}[#1]%
    \noindent\begin{minipage}{#3}
      \begin{myshaded}
      \hrulefill\par}
  {\par\hrulefill\end{myshaded}\end{minipage}\end{mytype}}

\begin{document}

\begin{myenv}{!ht}{blue!20}{5cm}
  Test example 1
  \caption{this is the first example}
\end{myenv}

\begin{myenv}{!ht}{red!20}{\textwidth}
  Test example 2
  \caption{this is the second example}
\end{myenv}

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
Post Reply