[Tex/LaTex] Vertical and horizontal table alignment

horizontal alignmenttables

enter image description hereI am trying to create a 5 x 13 table in which the contexts of each cell are aligned both vertically and horizontally. So far the contents of each cell are top aligned. I have tried to implement the solutions provided to similar questions asked on here but I dont seem to be having any luck. My code is as follows:

 \begin{center}
\begin{table}
        \centering
        \captionof{table}{Example.} \label{tab:title} 
        \begin{tabular}{|c|c|c|c|c|}
            \hline Iter. & $N$ & $\epsilon$ & $J = r_{f}$ & CPU (sec)   \\[2pt]
            \hline 0 & 7 & $9.71 \times 10^{-3}$ & 1.0125612 & 0.21   \\[2pt] 
            \hline 1 & 13 & $1.01 \times 10^{-3}$ & 1.0125873 & 0.13   \\[2pt]
            \hline 2 & 16 & $8.74 \times 10^{-4}$ & 1.0126011 & 0.79   \\[2pt] 
            \hline 3 & 21 & $5.32 \times 10^{-4}$ & 1.0126056 & 1.03 \\[2pt]  
            \hline 4 & 30 & $3.21 \times 10^{-4}$ & 1.0126099 & 2.34 \\[2pt]  
            \hline 5 & 36 & $9.82 \times 10^{-5}$ & 1.0127022 & 1.64 \\[2pt]  
            \hline 6 & 45 & $5.19 \times 10^{-5}$ & 1.0127067 & 1.19 \\[2pt]  
            \hline 7 & 64 & $4.70 \times 10^{-5}$ & 1.0127143 & 2.15 \\[2pt]  
            \hline 8 & 80 & $7.57 \times 10^{-6}$ & 1.0127156 & 3.10 \\[2pt]  
            \hline 9 & 99 & $8.73 \times 10^{-6}$ & 1.0127174 & 6.56 \\[2pt]  
            \hline 10 & 165 & $9.80 \times 10^{-7}$ & 1.0127176 & 7.98 \\[2pt]  
            \hline  &  &  &  & 27.12 \\ 
            \hline 
        \end{tabular}
\end{table}
\end{center}

I would be very grateful if someone could kindly advise where I might be going wrong in my implementation.

Many thanks

Bob.

I have updated my question with an image of the table rendered in TexStudio where it can be seen that the cell contents are aligned to the top of each cell as opposed to the centre of each cell.

Best Answer

All those horizontal and vertical lines are actually quite distracting. Best to get rid of all vertical lines and almost all horizontal lines. That way, there's no need to waste time on aligning the cell contents, vertically or horizontally, since there are no reference lines to begin with.

I actually would not center the cell contents horizontally. Instead, I'd align the number on the implicit or explicit decimal markers.

enter image description here

\documentclass{article}
\usepackage{siunitx,booktabs}
\usepackage[skip=0.333\baselineskip]{caption}
\begin{document}
\begin{table}
\renewcommand\arraystretch{1.1}
\centering
\caption{Example.} 
\label{tab:title} 
\begin{tabular}{@{}S[table-format=2.0]
                   S[table-format=3.0]
                   S[table-format=1.2e-1,tight-spacing]
                   c
                   S[table-format=2.2] @{}}
\toprule
{Iter.} & {$N$} & {$\epsilon$} & $J = r_{f}$ & {CPU}  \\
 & & & & {(sec)} \\
\midrule
 0 &   7 & 9.71e-3 & 1.0125612 & 0.21 \\ 
 1 &  13 & 1.01e-3 & 1.0125873 & 0.13 \\
 2 &  16 & 8.74e-4 & 1.0126011 & 0.79 \\ 
 3 &  21 & 5.32e-4 & 1.0126056 & 1.03 \\  
 4 &  30 & 3.21e-4 & 1.0126099 & 2.34 \\ 
 5 &  36 & 9.82e-5 & 1.0127022 & 1.64 \\  
 6 &  45 & 5.19e-5 & 1.0127067 & 1.19 \\  
 7 &  64 & 4.70e-5 & 1.0127143 & 2.15 \\  
 8 &  80 & 7.57e-6 & 1.0127156 & 3.10 \\  
 9 &  99 & 8.73e-6 & 1.0127174 & 6.56 \\  
10 & 165 & 9.80e-7 & 1.0127176 & 7.98 \\
\cmidrule(l){5-5}
& & & \multicolumn{1}{r}{Sum}  &27.12 \\ 
\bottomrule
\end{tabular}
\end{table}
\end{document}