4

I have this latex table, how can I center every value in every column, it seems like it is not centered and shifted to left. Also any recommendation to make it a bit more professional?

\begin{table}[h!]
\centering
\begin{tabular}{ |m{1cm}||m{2cm}|m{2cm}|m{2cm}| m{2cm}|m{2cm}|m{2cm}| }

 \hline
 Graph & Maximum Degree & Minimum Degree & Average Degree  & Profit Change & Mean Price Change & Price Variance Change\\
 \hline
 1 & 5 & 4 & 4 & 5 & 6 & 7 \\
 2 & 4 & 4 & 2 &   & 5 & 7 \\
 3 & 3 & 3 & 3 & 4 & 7 & 8 \\
\hline
\end{tabular}
\caption{Table to test captions and labels}
\label{table:1}
\end{table}

Thank you in advance! Stay safe :)

2
  • 1
    Welcome to TeX.SE. – Mico Apr 10 '20 at 15:47
  • Thanks :) Pretty new to Latex. – ELLA Apr 10 '20 at 15:49
9

how can I center every value in every column, it seems like it is not centered and shifted to left.

The m column type performs vertical, not horizontal, centering of cell contents. It is possible to create a version of the m column type that centers the cells' contents. However, it's still a bit clumsy to have to set absolute column widths for the six data columns. I suggest you switch to a tabularx environment and let LaTeX calculate the columns widths of the six data columns. The results of this change are shown in Table 1 below.

Also any recommendation to make it a bit more professional?

While Table 1 centers the column contents and is guaranteed to fit inside the textblock, the result is, shall we say, rather bland. All those vertical bars create an uninspiring "prison window grid" look, and the horizontal lines aren't well-spaced. I have two suggestions.

  • Just get rid of all vertical lines and replace the \hline directives with \toprule, \midrule, and \bottomrule directives. (The latter macros are provided by the booktabs package.) The result of this change is displayed in Table 2 below. Compared with Table 1, the table is distinguished by a much more open "look".

  • While I would be happy to argue that Table 2 is better-looking than Table 1, it still suffers from a lack of visual grouping and rhythm. "What's the relationship between the columns, if any?", one is tempted to ask. In Table 3, I suggest a reorganization of the header cells that indicates a so-far hidden hierarchy among the columns. Making this hierarchy explicit not only helps improve the table's visual appeal, it should also help your readers understand what table is all about.

enter image description here

\documentclass{article}
\usepackage{tabularx,ragged2e}
\newcolumntype{C}{>{\Centering\arraybackslash}X} % centered version of 'X' col. type
\renewcommand{\tabularxcolumn}[2]{m{#1}} % omit if vertical centering isn't needed
\usepackage{booktabs} % for \toprule, \midrule, and \bottomrule macros

\begin{document}
\begin{table}[h!]
\setlength\tabcolsep{3pt} % default: 6pt

\begin{tabularx}{\textwidth}{ |c||*{6}{C|}}
\hline
Graph & Maximum Degree & Minimum Degree & Average Degree  & Profit Change & Mean Price Change & Price Variance Change\\
\hline
 1 & 5 & 4 & 4 & 5 & 6 & 7 \\
 2 & 4 & 4 & 2 &   & 5 & 7 \\
 3 & 3 & 3 & 3 & 4 & 7 & 8 \\
\hline
\end{tabularx}
\caption{Lots of vertical lines, poorly-spaced horizontal lines}\label{table:1}

\vspace{1cm}
\begin{tabularx}{\textwidth}{@{} l *{6}{C} @{}}
\toprule
Graph & Maximum Degree & Minimum Degree & Average Degree  & Profit Change & Mean Price Change & Price Variance Change\\
\midrule
 1 & 5 & 4 & 4 & 5 & 6 & 7 \\
 2 & 4 & 4 & 2 &   & 5 & 7 \\
 3 & 3 & 3 & 3 & 4 & 7 & 8 \\
\bottomrule
\end{tabularx}
\caption{No vertical lines, well-spaced horizontal lines}\label{table:2}

\vspace{1cm}
\begin{tabularx}{\textwidth}{@{} l *{6}{C} @{}}
\toprule
Graph & \multicolumn{3}{c}{Degree} & \multicolumn{3}{c@{}}{Change}\\
\cmidrule(lr){2-4} \cmidrule(l){5-7}
& Maximum & Minimum & Average & Profit & Mean Price & Price Variance \\
\midrule
 1 & 5 & 4 & 4 & 5 & 6 & 7 \\
 2 & 4 & 4 & 2 &   & 5 & 7 \\
 3 & 3 & 3 & 3 & 4 & 7 & 8 \\
\bottomrule
\end{tabularx}
\caption{Clearer header structure}\label{table:3}

\end{table}
\end{document}  
5
  • Thank you Mico! Thorough explanation. You're awesome! – ELLA Apr 10 '20 at 16:19
  • +1 for explanation, today I'm too lazy to bother with English ... – Zarko Apr 10 '20 at 16:33
  • I read before that I don't need to use \arraybackslash with \Centering when loading ragged2e. Is this true? – Diaa Apr 10 '20 at 21:19
  • @Diaa - Short answer: "No". Somewhat longer answer: "It depends on the circumstances. But the claim is not true under all circumstances." Long Answer: In the present circumstance, it so happens that the answer would work without \arraybackslash. From lengthy experience, though, I know better than to omit the directive, as otherwise I'm certain to get queries and comments as to why my answer doesn't work with \centering, etc. Many users simply don't notice that \centering and \Centering aren't the same. Sigh. – Mico Apr 10 '20 at 21:52
  • @Mico I understand your point from the perspective of practicality. On the other hand, from the functionality point of view, would using \arraybackslash in general make something work better when using \Centering of ragged2e? – Diaa Apr 11 '20 at 2:37
7

Assuming that numbers in table is now dummy but in the real table will be decimal numbers, for example with two integer and three decimal digits. In this case you may consider to use S columns defined by siunitx package and align numbers at decimal points:

\documentclass{article}
\usepackage{geometry}
\usepackage{booktabs, multirow}
\usepackage{siunitx}

\begin{document}
\begin{table}[h!]
\caption{Table to test captions and labels}
\label{table:1}
    \centering
\begin{tabular}{c *{6}{S[table-format=2.3]} }
    \toprule
\multirow{2.4}{*}{Graph}
    &   \multicolumn{3}{c}{Degree}
        &   \multicolumn{3}{c}{Change}    \\
    \cmidrule(lr){2-4}
    \cmidrule(l){5-7}
    & {Maximum} & {Minimum}     & {Average} 
    & {Profit}  & {Mean Price}  & { Variance}    \\
    \midrule
 1  & 5.67  & 1.23 & 2.45   & 15.671  & 6.789 & 7.89  \\
 2  & 11.23 & 2.34 & 6.785  &         & 5.678 & 7.891 \\
 3  & 7.89  & 3.45 & 5.67   & 4.567   & 7.89  & 8.912 \\
    \bottomrule
\end{tabular}
\end{table}
\end{document}

enter image description here

3
  • I will asap, result is nicer. Thank you very much! – Zarko Apr 10 '20 at 16:39
  • Thank you Zarko:) great tip, it is indeed dummy.. – ELLA Apr 10 '20 at 16:44
  • @ELLA, if you liked answer, you can (now) up vote it (as well others, if you like them). – Zarko Apr 10 '20 at 16:55
3

enter image description here

\documentclass{article}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage{array}

\begin{document}

    \begin{table}[h!]
        \centering
        \begin{tabular}{ccccccc } \toprule
            Graph & Maximum & Minimum & Average  & Profit & Mean Price & Price Variance\\
                    &Degree & Degree    &Degree     &Change     &Change             &Change\\ \midrule
            1 & 5 & 4 & 4 & 5 & 6 & 7 \\
            2 & 4 & 4 & 2 &   & 5 & 7 \\
            3 & 3 & 3 & 3 & 4 & 7 & 8 \\\bottomrule
        \end{tabular}
        \caption{Table to test captions and labels}
        \label{table:1}
    \end{table}
\end{document}
1
  • Thank you! Appreciate it. – ELLA Apr 10 '20 at 16:20

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.