3

I am trying to make a transportation tableau in LaTeX. The tableau I wanted looks like the one below.

enter image description here

Werner
  • 603,163

3 Answers3

4

It seems there's a partial solution given at Diagonal lines in table cell. The example there uses TikZ to produce the diagonal. But, I would find building the whole table in TikZ a bit too tedious. My solution would be to nest tabular environments and define a new column type (to avoid ugly breaks in the lines). Here's my code---hope it's not too clunky. I'm omitting the diagonal: for that see the link above.

\documentclass{article}
\usepackage{array}
\newcolumntype{C}{@{}c@{}}
\newcommand{\bottombox}[1]{\makebox[2em][r]{#1}\hspace*{\tabcolsep}\hspace*{2em}}%
\newcommand{\innerbox}[2]{%
    \begin{tabular}[b]{c|c}
       \rule{2em}{0pt}\rule[-2ex]{0pt}{5ex} & \makebox[2em]{#2} \\\cline{2-2}
       \multicolumn{2}{r}{{#1}\hspace*{1.5\tabcolsep}\hspace*{2em}\rule[-2ex]{0pt}{5ex}}
    \end{tabular}}
\renewcommand{\arraystretch}{1.25}
\begin{document}

   \begin{tabular}{c|C|C|C|r}\hline
          & A                 & B                 & C                 & Supply  \\\hline
   1      & \innerbox{}{12}   & \innerbox{}{10}   & \innerbox{600}{6} &   600   \\\hline
   2      & \innerbox{400}{4} & \innerbox{}{15}   & \innerbox{}{3}    &   400   \\\hline
   3      & \innerbox{300}{9} & \innerbox{}{7}    & \innerbox{}{M}    &   300   \\\hline
   4      & \innerbox{}{11}   & \innerbox{500}{8} & \innerbox{300}{6} &   800   \\\hline
   Dummy  & \innerbox{200}{0} & \innerbox{}{0}    & \innerbox{}{0}    &   200   \\\hline
   Demand & \bottombox{900}   & \bottombox{500}   & \bottombox{900}   & 2,300   \\\hline
   \end{tabular}

\end{document}
A.Ellett
  • 50,533
2

It looks like this can be done with TikZ. You could

  • build matrix of nodes for all the entries. Here I mean the small entries positioned in a matrix, not the big cells.

  • Each cell in a matrix can be refered to for drawing lines. So, even if there's not yet an automatic way, you can draw all lines in blue between the nodes yourself, anchoring as desired.

  • For cleaner code and resusability, define styles, such as node styles (italic font for some numbers, bold font for other entries, framed entries, text alignment in nodes).

If you would update your question, showing your efforts after you decided how to approach the problem, we could help you better.

Stefan Kottwitz
  • 231,401
1

I have added the part for diagonal (To/From). Thanks Ellett

  \documentclass{article}
  \usepackage{array}
  \usepackage{tikz}
  \newcommand\diag[4]{%
  \multicolumn{1}{p{#2}|}{\hskip-\tabcolsep
  $\vcenter{\begin{tikzpicture}[baseline=0,anchor=south west,inner sep=#1]
  \path[use as bounding box] (0,0) rectangle (#2+2\tabcolsep,\baselineskip);
  \node[minimum width={#2+2\tabcolsep},minimum height=\baselineskip+\extrarowheight] (box) {};
  \draw (box.north west) -- (box.south east);
  \node[anchor=south west] at (box.south west) {#3};
  \node[anchor=north east] at (box.north east) {#4};
  \end{tikzpicture}}$\hskip-\tabcolsep}}


    \newcolumntype{C}{@{}c@{}}
    \newcommand{\bottombox}[1]{\makebox[2em][r]{#1}\hspace*{\tabcolsep}\hspace*{2em}}%
    \newcommand{\innerbox}[2]{%
        \begin{tabular}[b]{c|c}
           \rule{2em}{0pt}\rule[-2ex]{0pt}{5ex} & \makebox[2em]{#2} \\\cline{2-2}
           \multicolumn{2}{r}{{#1}\hspace*{1.5\tabcolsep}\hspace*{2em}\rule[-2ex]{0pt}{5ex}}
        \end{tabular}}
    \renewcommand{\arraystretch}{1.25}
    \begin{document}

       \begin{tabular}{c|C|C|C|r}\hline
       \diag{.1em}{1.5cm}{From}{To}& A         & B     & C         & Supply  \\\hline
       1      & \innerbox{}{12}   & \innerbox{}{10}   & \innerbox{600}{6} &   600   \\\hline
       2      & \innerbox{400}{4} & \innerbox{}{15}   & \innerbox{}{3}    &   400   \\\hline
       3      & \innerbox{300}{9} & \innerbox{}{7}    & \innerbox{}{M}    &   300   \\\hline
       4      & \innerbox{}{11}   & \innerbox{500}{8} & \innerbox{300}{6} &   800   \\\hline
       Dummy  & \innerbox{200}{0} & \innerbox{}{0}    & \innerbox{}{0}    &   200   \\\hline
       Demand & \bottombox{900}   & \bottombox{500}   & \bottombox{900}   & 2,300   \\\hline
       \end{tabular}

    \end{document}