Graphics, Figures & TablesVertical centering of images in tables

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
donmartin
Posts: 2
Joined: Sat Feb 13, 2010 4:17 pm

Vertical centering of images in tables

Post by donmartin »

Hello,

Summary:
How does one get images (and other content) in a tabular environment to center vertically? Heres the catch: it has to be universal and work with every picture I want because thats why I switched to LaTeX in the first place. Anything that requires me to specify a width by hand, such as m{width}, therefore does not meet this requirement.

Full text:
I have this dream. I am a chemist and thought it would be nice to draw structural formulae by using seperate images of my molecules and organising them together as a kind of mosaic using the tabular environment.

Example of the desired result:
300px-Preparation_of_acyl_azides_from_acyl_chlorides.png
300px-Preparation_of_acyl_azides_from_acyl_chlorides.png (8.25 KiB) Viewed 2458 times
(Please ignore the R= part, I googled this image)

So the way I want it; the molecule on the left is an image, the arrow and text are made using the mhchem package, and the molecule on the right is again an image. Therefore the table has three columns.

The advantages of this approach would be that I can create a library of all the molecules I ever draw, and that I can create a 'molecule' environment so that I can \ref to the molecules in my papers.

The only thing standing in the way of my reaching this goal is the vertical centering thingy. The m{width} is not an option because it requires a lot of trail and error and is basically 'what you see is what you get'. (The way I understand it.) And if I was the 'what you see is what you get'-kind of person, I would still be using word.

How can I do this? Some of the packages I have tried are: subfig, floatrow, tabularx. Although I might off course have made a mistake.

And just because I cant believe how hard it is to got this working, I was able to do it in Word in a minute :S.

Below is some of the code I use. The first part is how I count the molecules, it might be useful to someone. And the second part is the tabular environment for a reaction I want to draw, using my \molecule command.

Help is much appreciated!

Code: Select all

Preamble:

%These new commands add a special counter for molecules. It requires all molecules to be added as a seperate image.
%It adds the functionality to \ref to molecules.
\newcounter{molno}									%This declares the molecule counter
\newenvironment{molno}[1]{\refstepcounter{molno} #1}{}		%Environment to increment the counter when called
\newcommand{\molecule}[2]							%Command to create a new molecule, which calls the molno environment to
{												%increase the counter and to create a label so you can \ref to it.
	\begin{molno}									%Call this command to actually create a a new molecule. Usage:
	\includegraphics{#2}								%\molecule{label}{picture name}
	\label{#1}
	\end{molno}
}

Code: Select all

\begin{tabular}{ccccc}
\molecule{glycol}{glycol} & $+$ &\molecule{tscl}{TsCl} &\ce{->} &\molecule{tsglycol}{tsglycol} \\
\ref{glycol} & & \ref{tscl} &	 & \ref{tsglycol} \\
\end{tabular}

Recommended reading 2024:

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

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

jun0
Posts: 5
Joined: Sat Oct 17, 2009 9:36 am

Vertical centering of images in tables

Post by jun0 »

Hello, looks like this was posted a while ago, but I hope it's not too late for a reply.

One solution to your problem is to measure the size of the picture and to jail the picture in a minipage with vertical centering, setting the width of the minipage to the picture width that you just measured. The following \molecule command should work as the one you wrote, except it centers the image vertically.

Code: Select all

\newlength{\mypicwidth}
\newsavebox{\mysavebox}

\newcommand{\molecule}[2]                     %Command to create a new molecule, which calls the molno environment to
{                                    %increase the counter and to create a label so you can \ref to it.
  \begin{molno}                           %Call this command to actually create a a new molecule. Usage:
    \savebox{\mysavebox}{\includegraphics{#2}}
    \settowidth{\mypicwidth}{\usebox{\mysavebox}}
    \begin{minipage}[c]{\mypicwidth}
      \usebox{\mysavebox}
    \end{minipage}
     % \molecule{label}{picture name}
   \label{#1}
   \end{molno}
}
Post Reply