Archive for the 'latex' Category

LaTex: Making Floatng Text Boxes

Sunday, February 4th, 2007

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.

Convert PNG to EPS

Thursday, January 18th, 2007

If you are a LaTex user like me you will instantly recognize what I’m getting at here. LaTex does not work well with most of the popular image formats such as JPG, BMP, PNG or GIF. It does like encapsulated postscript images though (EPS). So if you want nice figures in your papers you can either use tools that can generate eps files, or convert.

I usually just go straight to EPS - most of Linux based graphing tools have support for either than or straight postscript which is also perfectly fine for LaTex. Between OpenOffice.org, Gnuplot, and the charting tools in KOffice I rarely need to convert anything. But the other day I was doing a batch of screenshots of an application and the default output format was PNG.

How do you convert from PNG to EPS? There are lots of ways, but since I was on ubuntu, I opted for using sam2p. It’s a small application that converts raster images into bunch of different formats. You probably won’t find it in a default installation so just do:

apt-get install sam2p

Note that you need to have universe enabled in your apt sources. Once you install it, the rest is easy:

sam2p Screenshot-1.png EPS: Screenshot-1.eps

First argument is the source image, second is the desired format followed by a colon, and the third is the desired name of the output file. Easy as pie. The only slight disadvantage of doing this is that you pay a considerable size penalty for this conversion:

Screenshot-1.png: 6.4KB
Screenhsot-1.eps: 20.0 KB

But that’s to be expected - after all we are goinf from tight binary image format to a textual postscript monstrosity. Note that you can cat the EPS file you will see nice coded segments composed of only ASCII characters. If you try to do that to a PNG file, you will see garbled binary mess and hear the system bell sound off once or twice (encountering the ASCII code for system bell in the output).

Latex: Rotate Inserted Images

Monday, January 8th, 2007

When you insert an image into your latex document you can rotate, and resize it it at the same time. It is useful, especially if you are importing a full page postscript graph that has a landscape layout:

\begin{figure}
\centering
\includegraphics[width=.5\textwidth, angle=270]{graph.ps}
\label{fig:sample-graph}
\caption{Sample Graph}
\end{figure}

In the example above I’m rotating the image by 270 degree, and set the image width to be half the width of the text on the page.

Latex: Numbered Subsubsections

Sunday, December 17th, 2006

LaTex lets you break the document down into chapters, sections, subsections, subsubsections and paragraphs. By default subsubsections and paragraphs are not numbered or included in the table of contents. Sometimes this might be desirable. However, I usually find that in cases when I do not need them numbered, I usually never use them (ie. my documents are broken down by chapter, section, and subsection only). At other times, when I desire more granularity, I actually want subsubsections to show up in the TOC and have distinctive headings and numbers.

To enable this you will need to put 2 lines of code somewhere in your preamble:

\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}

The first line enables numbering of subsubsections. The second line includes subsubsections in the table of contents.

Btw, if you need to break down your document even more you can switch 3 to 4 in these lines to enable numbering for paragraphs.

Latex: Citations in Figures

Wednesday, July 26th, 2006

For some unknown, mysterious reason you can’t put a \cite command as an argument of the \caption statement. I stumbled upon this today when I was trying to attribute an image that I shamelessly snurched from the web.

Apparently, the cause is not as mysterious as I thought. If you want to know why this happens, do some research on fragile commands and moving arguments. Do you need to understand these things throughly? Probably not. You just need to remember that you may sometimes need to protect a \cite when it is an attribute of something else:

\caption{Very Stupid Image \protect\cite{source}}

So here is a new rule of thumb: if you nest a command in an attribute of another command and latex freaks out, you might be dealing with a fragile command inside of a moving attribute. Sticking a \protect in front of the command is a good place to start troubleshooting the problem.

Update 12/15/2006 04:03:46 PM

One caveat here - if you put citations in image captions, then they will show up in your list of figures. This looks a little ugly, and it can mess up your citation order. For example, if you use IEEEtran.bst for your BibTex formating, then the first cited source should be [1], second source should be [2] and etc… Unfortunately if your list of figures is on page 3, then the first citation on that page will be counted as [1] even if the actual image does not show up till page 64.

So be careful with these.

Update 01/08/2007 10:05:32 AM

I figured out the caption and the list of figures issue. It turns out that \caption can take an optional parameter which is the text for the list like so:

\caption[Stupid Image]{Stupid Image \protect\cite{source}}

If you use it this way the citation only shows up under the figure, but not in the LoF and thus the order of references is preserved.