LaTex: Making Floatng Text Boxes

Here is a problem I faced. In my thesis I had descriptions of several algorithms that I used. They were essentially written as long numbered lists. Unfortunately because of the way the paper was paged, almost all of them were spanning two pages. This made them more confusing to read.

I needed an environment that would not allow a page break to occur in the middle of it, but let me use enumerated lists and math equations inside. Enter the boxedminipage package. It will create a nice pseudo page environment with a border within your actual page. If you don’t want a border you can use the more standard \minipage command. Here is how to use it. First import it in a preamble:

\usepackage{boxedminipage}

Then in the body of your document do:

\begin{boxedminipage}{\textwidth}
	your text goes here
\end{boxedminipage}

You can specify your own width in place of \textwidth. I simply wanted my boxes to be as wide as the regular text on the page.

The boxes created this way will not allow for a page break to occur inside of them. If they won’t fit on a current page, they will be pushed to the next one. If this is all you want, you can stop here. I however wanted to add a caption to each of these boxes, and a label by which they can be referred in text – just like a normal figure.

\begin{figure}[htp]
	\begin{boxedminipage}{\textwidth}
		your text goes here
	\end{boxedminipage}
	\cite{My Text Box}
	\label{box:mybox}
\end{figure}

Once I did this, I ran into another issue. One of my boxes was so big it had to be put on a page of its own. Unfortunately LaTex decided to push that full page figure to the end of the chapter (ie. 4 relatively large sections and over 15 pages away from it’s intended location).

It appears that the default latex settings for floating objects are somewhat dumb, especially when dealing with large figures. If you override the defaults however, you can get the damn thing to do what you want it to do – put the large float as close to the intended location as possible.

The following settings helped me:

 \renewcommand{\topfraction}{0.9}	
    \renewcommand{\bottomfraction}{0.8}	
    \setcounter{topnumber}{2}
    \setcounter{bottomnumber}{2}
    \setcounter{totalnumber}{4} 
    \setcounter{dbltopnumber}{2} 
    \renewcommand{\dbltopfraction}{0.9}	
    \renewcommand{\textfraction}{0.07}	
    \renewcommand{\floatpagefraction}{0.7}	
	% floatpagefraction MUST be less than topfraction !!
    \renewcommand{\dblfloatpagefraction}{0.7}

I took this directly from one of the LaTex help pages of the San Diego State Department of Astronomy & Mount Laguna Observatory website and it worked for me. You can tweak the numbers for better effects, but I found that these are just fine for what I needed to do.

I added that preamble block as a permanent part to my thesis template. It seems to generally improve the handling of floats throughout the document.

[tags]latex, boxes, text boxes, boxedminipage, minipage, floats, figures, san diego state, mount laguna observatory[/tags]

This entry was posted in programming and tagged . Bookmark the permalink.



One Response to LaTex: Making Floatng Text Boxes

  1. Hi,

    Thanks for the tip! I wanted to create a macro for a nice information box and your tip solved it for me. Using also the color package, here’s my macro for anyone else that may be interested:

    \newcommand{\infobox}[2]{
    \begin{boxedminipage}{0.9\textwidth}
    \vspace{-0.1cm}\hspace{-0.1cm}\colorbox{black}{\makebox[\textwidth][ c]{\textcolor{white}{\textbf{#1}}}}

    \small{#2}
    \end{boxedminipage}\vspace*{0.6cm}
    }

    And to use it:

    \infobox{Box Title}{
    First paragraph in the box.

    Second paragraph in the box.

    Third paragraph in the box, and so on and so forth…
    }

    Now I have to figure out how to change the spacing of the lines in a paragraph, because when I reduced the font size the spacing seems too large.

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>