5

I have very little experience with LaTeX. So, pardon my ignorance, but is there a float equivalent of \framebox?

Ideally, I'd like a simple construct for framed text that I can position at the top [t] or bottom [b] of the page.

I found two posts that didn't quite seem to address my needs:

  1. Framed text with a float in it
  2. Frame around text and figure

3 Answers 3

3

Here is an alternative using tcolorbox to created the floating box.

The first version is a minimalistic approach:

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox{framefloat}[1][!tb]{arc=0pt,outer arc=0pt,boxrule=0.4pt,
  colframe=black,colback=white,float=#1}

\begin{document}

\begin{framefloat}
\textbf{Box on top:}
\lipsum[2]
\end{framefloat}

\begin{framefloat}[b]
\textbf{Box on bottom:}
\lipsum[2]
\end{framefloat}

\lipsum[1-3]

\end{document}

enter image description here

tcolorbox allows to use much more options to create a fancier style, if your are looking for something like that. The second version uses some color options and exchanges the option parameter to take a comma separated list of options. The float option takes the known floating parameters.

\documentclass{article}
\usepackage[many]{tcolorbox}
\usepackage{lipsum}

\newtcolorbox{framefloat}[1][]{fonttitle=\bfseries,
  colframe=yellow!30!black,colback=yellow!10!white,float=!tb,#1}

\begin{document}

\begin{framefloat}[title=Box on top]
\lipsum[2]
\end{framefloat}

\begin{framefloat}[title=Box on bottom,float=b]
\lipsum[2]
\end{framefloat}

\lipsum[1-3]

\end{document}

enter image description here

2
\documentclass{scrartcl}
\usepackage{varwidth}
\newsavebox\FBox
\newenvironment{framefloat}[1][!htb]
  {\begin{table}[#1]\centering\begin{lrbox}{\FBox}
   \varwidth{\dimexpr\linewidth-2\fboxsep-2\fboxrule}}
  {\endvarwidth\end{lrbox}\fbox{\usebox\FBox}\end{table}}

\usepackage{blindtext}

\begin{document}

\blindtext

\begin{framefloat}[t]
\blindtext
\end{framefloat}

\blindtext

\begin{framefloat}
\blindtext
\end{framefloat}

\end{document}

enter image description here

0

Here's an option using a combination of the mdframed, newfloat, and xparse package (which is loaded by mdframed).

The following line:

\DeclareFloatingEnvironment[placement={!ht}]{myfloat}

declares a new floating environment called myfloat, which has default placement of !ht, and can be overridden.

The following lines make a new floating environment that combines myfloat together with an environment from the mdframed package:

\NewDocumentEnvironment{framefloat}{O{}O{}}
    {% #1: float position (optional)
     % #2: options for mdframed (optional)
     \begin{myfloat}[#1]
    \begin{mdframed}[roundcorner=10pt,backgroundcolor=green,#2]
    }
    {\end{mdframed}\end{myfloat}
    }

As you can see, it uses the syntax from the xparse package to declare two optional arguments:

    % #1: float position (optional)
    % #2: options for mdframed (optional)

It can be used in any of the following ways (for example):

\begin{framefloat}

\begin{framefloat}[t]

\begin{framefloat}[b][backgroundcolor=blue]

\begin{framefloat}[][backgroundcolor=red]

Here's a complete MWE to play with. Each of the packages that I have used have a lot more features; explore as you see fit.

% arara: pdflatex
\documentclass{article}
\usepackage{newfloat}
\usepackage{lipsum}
\usepackage[framemethod=TikZ]{mdframed}

% new float
\DeclareFloatingEnvironment[placement={!ht}]{myfloat}

% new floating framed environment
\NewDocumentEnvironment{framefloat}{O{}O{}}
    {\begin{myfloat}[#1]
    \begin{mdframed}[roundcorner=10pt,backgroundcolor=green,#2]
    }
    {\end{mdframed}\end{myfloat}
    }

\begin{document}

\lipsum

\begin{framefloat}
\lipsum[1]
\end{framefloat}

\lipsum

\begin{framefloat}[t]
\lipsum[1]
\end{framefloat}

\lipsum

\begin{framefloat}[b][backgroundcolor=blue]
\lipsum[1]
\end{framefloat}

\lipsum

\begin{framefloat}[][backgroundcolor=red]
\lipsum[1]
\end{framefloat}


\end{document} 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .