5

I'm trying to make a table which lists some math functions. I've been searching the net on how to center the equations in the cells both vertically and horizontally. And I think I've found an answer to it, except that no matter what I do the equations in the 3rd column won't align, and just stays on the top. I use the array package. What am I doing wrong?

    \begin{table}[H]
    \centering
    \newcolumntype{A}{ >{\centering\arraybackslash} m{1cm} }
    \newcolumntype{B}{ >{\centering\arraybackslash} m{4cm} }
    \newcolumntype{C}{ >{\centering\arraybackslash} m{2cm} }
    \begin{tabular}{|A|B|C|}
    \hline

    $n$ & $a_{n}$ & $\frac{k_{0}^{n}}{n!}a_{0}$ \\[2ex]

    \hline

    1 & $k_{0}a_{0}$ & $\frac{k_{0}^{1}}{1}a_{0}$ \\[2ex]

    \hline

    2 & $\frac{k_{0}}{2}a_{1}=\frac{k_{0}}{2}k_{0}a_{0}$ 
    & $\frac{k_{0}^{2}}{2 \cdot 1}a_{0}$ \\[2ex]

    \hline

    3 & $\frac{k_{0}}{3}a_{2}=\frac{k_{0}}{3}\frac{k_{0}}{2}k_{0}a_{0}$ 
    & $\frac{k_{0}^{3}}{3 \cdot 2 \cdot 1}a_{0}$ \\[2ex]

    \hline

    4 & $\frac{k_{0}}{4}a_{3}=\frac{k_{0}}{4}\frac{k_{0}}{3}
    \frac{k_{0}}{2}k_{0}a_{0}$ 
    & $\frac{k_{0}^{4}}{4 \cdot 3 \cdot 2 \cdot 1}a_{0}$ \\[2ex]

    \hline
    \end{tabular}
    \end{table}
2

For comparison, I think that ConTeXt provides a cleaner solution in this case.

  • Use a setup to specify the width of the three columns (width=...), specify that all cells should be horizontally and vertically middle aligned (align={middle,lohi}), and specify that each cell should have a 1ex top and bottom offset (toffset=1ex, boffset=1ex):

    \startsetups table:align
      \setupTABLE[align={middle,lohi}, toffset=1ex, boffset=1ex]
      \setupTABLE[column][1][width=1cm]
      \setupTABLE[column][2][width=4cm]
      \setupTABLE[column][3][width=2cm]
    \stopsetups
    
  • Then use that setup for the tabular data

    \startTABLE[setups={table:align}]
       \NC $n$ \NC $a_{n}$      \NC $\frac{k_{0}^{n}}{n!}a_{0}$                        
       \NC \NR
       \NC 1   \NC $k_{0}a_{0}$ \NC $\frac{k_{0}^{1}}{1}a_{0}$
       \NC \NR
       \NC 2   \NC $\frac{k_{0}}{2}a_{1}=\frac{k_{0}}{2}k_{0}a_{0}$                               
       \NC $\frac{k_{0}^{2}}{2 \cdot 1}a_{0}$
       \NC \NR
       \NC 3   \NC $\frac{k_{0}}{3}a_{2}=\frac{k_{0}}{3}\frac{k_{0}}{2}k_{0}a_{0}$                
       \NC $\frac{k_{0}^{3}}{3 \cdot 2 \cdot 1}a_{0}$
       \NC \NR
       \NC 4   \NC $\frac{k_{0}}{4}a_{3}=\frac{k_{0}}{4}\frac{k_{0}}{3} \frac{k_{0}}{2}k_{0}a_{0}$
       \NC $\frac{k_{0}^{4}}{4 \cdot 3 \cdot 2 \cdot 1}a_{0}$
       \NC \NR
    \stopTABLE
    

enter image description here

1

I suspect it isn't your fault...

You can avoid the problem by making sure the m is not on the last column:

enter image description here

Also please always make your questions contain complete documents showing all packages used:

\documentclass{article}
\usepackage{array}
\setlength\extrarowheight{7pt}
\begin{document}
    \centering
    \newcolumntype{A}{ >{\centering\arraybackslash} m{1cm} }
    \newcolumntype{B}{ >{\centering\arraybackslash} m{4cm} }
    \newcolumntype{C}{ >{\centering\arraybackslash} m{2cm} }
    \begin{tabular}{|A|B|C|@{}c@{}}
    \hline

    $n$ & $a_{n}$ & $\frac{k_{0}^{n}}{n!}a_{0}$ &\\[2ex]

    \hline

    1 & $k_{0}a_{0}$ & $\frac{k_{0}^{1}}{1}a_{0}$ &\\[2ex]

    \hline

    2 & $\frac{k_{0}}{2}a_{1}=\frac{k_{0}}{2}k_{0}a_{0}$ 
    & $\frac{k_{0}^{2}}{2 \cdot 1}a_{0}$ &\\[2ex]

    \hline

    3 & $\frac{k_{0}}{3}a_{2}=\frac{k_{0}}{3}\frac{k_{0}}{2}k_{0}a_{0}$ 
    & $\frac{k_{0}^{3}}{3 \cdot 2 \cdot 1}a_{0}$ &\\[2ex]

    \hline

    4 & $\frac{k_{0}}{4}a_{3}=\frac{k_{0}}{4}\frac{k_{0}}{3}
    \frac{k_{0}}{2}k_{0}a_{0}$ 
    & $\frac{k_{0}^{4}}{4 \cdot 3 \cdot 2 \cdot 1}a_{0}$ &\\[2ex]

    \hline
    \end{tabular}

\end{document}
  • Thank you sir :) I will do my best to include everything NeXT time I – Møller Nov 27 '12 at 18:23
  • run in to problems – Møller Nov 27 '12 at 18:23
  • feel free to ask another question with an example showing the problem – David Carlisle Nov 27 '12 at 20:45
1

Given that it would appear that you're trying to make sure that math material is centered vertically, I think it may be a good idea not to insert something like [2ex] of extra vertical whitespace and, instead, insert "math struts" on each of the lines. The following MWE defines a macro named \RTS -- short for "really tall strut" -- that should be inserted somewhere in each row whose height you want to see enlarged.

enter image description here

\documentclass{article}
\usepackage{array,float}
\newcolumntype{A}{ >{\centering\arraybackslash} p{1cm} }
\newcolumntype{B}{ >{\centering\arraybackslash} p{4cm} }
\newcolumntype{C}{ >{\centering\arraybackslash} p{2cm} }
  % RTS is short for "really tall strut":
\newcommand\RTS{$\vphantom{\int\limits_0^1}$}
\begin{document}
\begin{table}[H]
\centering
\begin{tabular}{|A|B|C|}
\hline
$n$ & $a_{n}$ & $\frac{k_{0}^{n}}{n!}a_{0}$ \RTS \\
\hline
1 
& $k_{0}a_{0}$ 
& $\frac{k_{0}^{1}}{1}a_{0}$ \RTS \\
\hline
2 
& $\frac{k_{0}}{2}a_{1}=\frac{k_{0}}{2}k_{0}a_{0}$ 
& $\frac{k_{0}^{2}}{2 \cdot 1}a_{0}$ \RTS \\
\hline
3 & 
$\frac{k_{0}}{3}a_{2}=\frac{k_{0}}{3}\frac{k_{0}}{2}k_{0}a_{0}$ 
& $\frac{k_{0}^{3}}{3 \cdot 2 \cdot 1}a_{0}$ \RTS \\
\hline
4 
& $\frac{k_{0}}{4}a_{3}=\frac{k_{0}}{4}\frac{k_{0}}{3}
\frac{k_{0}}{2}k_{0}a_{0}$ 
& $\frac{k_{0}^{4}}{4 \cdot 3 \cdot 2 \cdot 1}a_{0}$ \RTS \\
\hline
\end{tabular}
\end{table}
\end{document}
0

I would use an array environment nested inside an math environment of some sort:

{\renewcommand{\arraystretch}{3}
\[\begin{array}{|l|>{\displaystyle{}}c|>{\displaystyle{}}c|}\hline
    n & a_{n}                                                                        & \frac{k_{0}^{n}}{n!}a_{0}                \\\hline
    1 & k_{0}a_{0}                                                                   & \frac{k_{0}^{1}}{1}a_{0}                 \\\hline
    2 & \frac{k_{0}}{2}a_{1} =\frac{k_{0}}{2}k_{0}a_{0}                              & \frac{k_{0}^{2}}{2 \cdot 1}a_{0}         \\\hline
    3 & \frac{k_{0}}{3}a_{2}=\frac{k_{0}}{3}\frac{k_{0}}{2}k_{0}a_{0}                & \frac{k_{0}^{3}}{3 \cdot 2 \cdot 1}a_{0} \\\hline
    4 & \frac{k_{0}}{4}a_{3}=\frac{k_{0}}{4}\frac{k_{0}}{3}\frac{k_{0}}{2}k_{0}a_{0} & \frac{k_{0}^{4}}{4 \cdot 3 \cdot 2 \cdot 1}a_{0} \\\hline
\end{array}\]

enter image description here

  • Ah great! Nice and simple, just the way I like it. Thank you very much – Møller Nov 27 '12 at 18:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.