1

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.

7
  • 1
    Well, that's not entirely surprising since you're asking for a 2pt vertical space after each row. Is your intention with this to increase the row height?
    – Troy
    Jun 2, 2018 at 12:33
  • 1
    Also, your previous questions have had some answers. If they were helpful, please go back and upvote and/or accept the answers. It's the TeX.SE way of saying "Thank you".
    – Troy
    Jun 2, 2018 at 12:35
  • 1
    This doesn't answer your question, but maybe you should consider using booktabs and siunitx to produce something like i.stack.imgur.com/RVctk.png, see gist.github.com/moewew/1eb8a0331be29ac9e878399ae0ef2e22
    – moewe
    Jun 2, 2018 at 12:35
  • @ Troy, with regards to you first point, yes it is my intention to add some spacing between each row. However, even when I remove \[2pt] and replace with \\ the contexts are still top aligned. Could you maybe suggest why this is the case? Many thanks.
    – Bob1986
    Jun 2, 2018 at 12:39
  • @Bob1986 See meta.tex.stackexchange.com/q/1852.
    – Troy
    Jun 2, 2018 at 12:50

1 Answer 1

4

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}
7
  • Is there any reason why you don't use S for the second-to-last column?
    – moewe
    Jun 2, 2018 at 12:50
  • @moewe There is no need for an additional macro layer and counting digits in this column. Of course, S[format=1.7] could also be used. Jun 2, 2018 at 12:51
  • @HeikoOberdiek Ah, that makes sense. Thanks.
    – moewe
    Jun 2, 2018 at 12:52
  • Dear Mico, this is perfect! Thank you for your help.
    – Bob1986
    Jun 2, 2018 at 12:53
  • 1
    The sum without the sum line looks a little strange to me. Instead of the space, I would use something like \cmidrule(lr){5-5}. The last column looks a little to much shifted to the right, because most numbers except the sum are using format 1.2. Thus, the column could be set with S[format=1.2] and the sum could be patched as \llap{2}7.12. Jun 2, 2018 at 12:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .