GeneralTabular with cells in array environment

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
drumex
Posts: 12
Joined: Mon Apr 14, 2008 2:18 am

Tabular with cells in array environment

Post by drumex »

Hello. Once again I need your help.

For the coursework I'm doing, I needed tables in the following format.

Image

Basically what I need is that framed cells have value holders at four corners. Fiddling around with LaTeX and not being able to produce it myself, I asked our lecturer to send me the source that was used the produce the examples in the course notes. He did, and I tweaked it (added one more column and one more row) to produce what I needed.

The problem is, the alignment within cells is not perfect.

Image

The thing is, I have to put them into the following format:

Image

As you can see, adding the extra bits makes the alignment even worse. I have used \arraystretch{1.4} command to make sure the frame of the "-5" doesn't touch the cell borders. This is transportation problems for computational operations research, by the way, if you were wondering.

So, is there another way of producing this kind of a table that has perfect or at least better alignment?

The code I used that was sent to me by our lecturer is this:

Code: Select all

\begin{tabular}{c|c|c|c|c|c|}
\multicolumn{1}{c}{} & \multicolumn{1}{c}{$13$} & \multicolumn{1}{c}{$9$} & \multicolumn{1}{c}{$15$} & \multicolumn{1}{c}{$15$} & \multicolumn{1}{c}{$12$} \\[2mm]
\cline{2-6}
\multicolumn{1}{c|}{$0$}& 
$\begin{array}{lr}12&\\& 13\end{array}$ &
$\begin{array}{lr}15&\\& 9\end{array}$ &
$\begin{array}{lr}13 - \varepsilon &\\&  15\end{array}$ &
$\begin{array}{lr}\varepsilon & \fbox{-5}\\& 10\end{array}$ & 
$\begin{array}{lr}&0\\& 12\end{array}$ \\
\cline{2-6}
\multicolumn{1}{c|}{$-3$}& 
$\begin{array}{lr}&1\\& 11\end{array}$ &
$\begin{array}{lr}&4\\& 10\end{array}$ &
$\begin{array}{lr}7 + \varepsilon &\\& 12\end{array}$ &
$\begin{array}{lr}3 - \varepsilon &\\& 12\end{array}$ &
$\begin{array}{lr}&0\\&  9\end{array}$ \\
\cline{2-6}
\multicolumn{1}{c|}{$-3$}& 
$\begin{array}{lr}&2\\& 12\end{array}$ &
$\begin{array}{lr}&3\\&  9\end{array}$ &
$\begin{array}{lr}&-1\\& 11\end{array}$ &
$\begin{array}{lr}12&\\& 12\end{array}$ &
$\begin{array}{lr}8&\\&  9\end{array}$ \\
\cline{2-6}
\multicolumn{1}{c|}{$-2$}& 
$\begin{array}{lr}&2\\& 13\end{array}$ &
$\begin{array}{lr}&5\\& 12\end{array}$ &
$\begin{array}{lr}&0\\& 13\end{array}$ &
$\begin{array}{lr}&-1\\& 12\end{array}$ &
$\begin{array}{lr}10&\\& 10\end{array}$ \\
\cline{2-6}
\end{tabular}
Windows Vista (SP1), MiKTeX 2.7, TeXnicCenter 1.0 RC1

Recommended reading 2024:

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

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

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Tabular with cells in array environment

Post by localghost »

As a first approach I did some modifications to your code. Pay attention to the cells
  • in the first row and first column
  • in the third row and third column
You could alternatively choose other column types for the inner arrays which allow a certain width to get rid of the horizontal phantoms. The array package offers some useful enhancements for that.

Code: Select all

\begin{tabular}{c|c|c|c|c|c|}
\multicolumn{1}{c}{} & \multicolumn{1}{c}{$13$} & \multicolumn{1}{c}{$9$} & \multicolumn{1}{c}{$15$} & \multicolumn{1}{c}{$15$} & \multicolumn{1}{c}{$12$} \\[2mm]
\cline{2-6}
\multicolumn{1}{c|}{$0$}&
$\begin{array}{@{}lr@{}}12&\\& 13\end{array}$ &
$\begin{array}{lr}15&\\& 9\end{array}$ &
$\begin{array}{lr}13 - \varepsilon &\\&  15\end{array}$ &
$\begin{array}{lr}\varepsilon & \fbox{-5}\\& 10\end{array}$ &
$\begin{array}{lr}&0\\& 12\end{array}$ \\
\cline{2-6}
\multicolumn{1}{c|}{$-3$}&
$\begin{array}{lr}&1\\& 11\end{array}$ &
$\begin{array}{lr}&4\\& 10\end{array}$ &
$\begin{array}{lr}7 + \varepsilon &\\& 12\end{array}$ &
$\begin{array}{lr}3 - \varepsilon &\\ & 12\end{array}$ &
$\begin{array}{lr}&0\\&  9\end{array}$ \\
\cline{2-6}
\multicolumn{1}{c|}{$-3$}&
$\begin{array}{lr}&2\\& 12\end{array}$ &
$\begin{array}{lr}&3\\&  9\end{array}$ &
$\begin{array}{lr}&-1\\& 11\end{array}$ &
$\begin{array}{lr}12&\\\hphantom{3-\varepsilon} & 12\end{array}$ &
$\begin{array}{lr}8&\\&  9\end{array}$ \\
\cline{2-6}
\multicolumn{1}{c|}{$-2$}&
$\begin{array}{lr}&2\\& 13\end{array}$ &
$\begin{array}{lr}&5\\& 12\end{array}$ &
$\begin{array}{lr}&0\\& 13\end{array}$ &
$\begin{array}{lr}&-1\\& 12\end{array}$ &
$\begin{array}{lr}10&\\& 10\end{array}$ \\
\cline{2-6}
\end{tabular}

Best regards
Thorsten¹
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Tabular with cells in array environment

Post by Juanjo »

In my opinion, it is not posible to get a proper alignment while keeping the original code provided by lecturer. Starting from scratch, the table could be written either as tabular environment or as an array one. Look at the following code:

Code: Select all

\documentclass{article}
\usepackage[margin=3cm]{geometry}
\usepackage{multirow,array}
\begin{document}

\newcolumntype{C}{>{$}c<{$}}
\newcolumntype{R}{>{$}r<{$}}
\newcolumntype{L}{>{$}l<{$}}

\renewcommand{\arraystretch}{1.5}
\addtolength{\tabcolsep}{3pt}

\begin{tabular}{r|*{5}{RR|}}
   \multicolumn{1}{r}{}
  &\multicolumn{2}{c}{13}&\multicolumn{2}{c}{9}
  &\multicolumn{2}{c}{15}&\multicolumn{2}{c}{15}
  &\multicolumn{2}{c}{12} \\
  \cline{2-11}
  \multirow{2}{*}{$0$} 
  & 12&& 15&& 13-\varepsilon&& \varepsilon& \fbox{$-5$}&& 0 \\
  && 13&& 9&& 15&& 10&& 12 \\
  \cline{2-11}
  \multirow{2}{*}{$-3$}
  && 1&& 4& 7+\varepsilon&& 3-\varepsilon&&& 0 \\
  && 11&& 10&& 12&& 12&& 9 \\
  \cline{2-11}
  \multirow{2}{*}{$-3$}
  && 2&& 3&& -1& 12&& 8 & \\
  && 12&& 9&& 11&& 12&& 9 \\
  \cline{2-11}
  \multirow{2}{*}{$-2$}
  && 2&& 5&& 0&& -1& 10& \\
  && 13&& 12&& 13&& 12&& 10 \\
  \cline{2-11}
\end{tabular}

\addtolength{\arraycolsep}{3pt}
\[
\begin{array}{r|*{5}{rr|}}
  \multicolumn{1}{r}{}
  &\multicolumn{2}{c}{13}&\multicolumn{2}{c}{9}
  &\multicolumn{2}{c}{15}&\multicolumn{2}{c}{15}
  &\multicolumn{2}{c}{12} \\
  \cline{2-11}
  \multirow{2}{*}{$0$} 
  & 12&& 15&& 13-\varepsilon&& \varepsilon& \fbox{$-5$}&& 0 \\
  && 13&& 9&& 15&& 10&& 12 \\
  \cline{2-11}
  \multirow{2}{*}{$-3$}
  && 1&& 4& 7+\varepsilon&& 3-\varepsilon&&& 0 \\
  && 11&& 10&& 12&& 12&& 9 \\
  \cline{2-11}
  \multirow{2}{*}{$-3$}
  && 2&& 3&& -1& 12&& 8 & \\
  && 12&& 9&& 11&& 12&& 9 \\
  \cline{2-11}
  \multirow{2}{*}{$-2$}
  && 2&& 5&& 0&& -1& 10& \\
  && 13&& 12&& 13&& 12&& 10 \\
  \cline{2-11}
\end{array}
\]

\end{document}
I've chosen a right alignment, which can be easily changed to a different one (use, for example, L or C in tabular and l or c in array). Some elements are typeset in a box, so it is necessary to re-enter in math mode with $...$. If you use the amsmath package, you can replace \fbox{$-5$} by \boxed{-5}. The advantage of \boxed is that it puts a frame without leaving math mode.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
drumex
Posts: 12
Joined: Mon Apr 14, 2008 2:18 am

Re: Tabular with cells in array environment

Post by drumex »

Thanks for the replies!

I went with Juanjo's suggestion cause it produced very nice alignment. I'll be sure to inspect the array package in the future.
Windows Vista (SP1), MiKTeX 2.7, TeXnicCenter 1.0 RC1
Post Reply