1

I created a table, but the last column is moved to the left, although I set it as central. I cannot find what I am doing wrong, so if someone can suggest me how I can solve my problem I would be very grateful. Below is my latex code and the screenshot what it is generating

\documentclass{article}[11pt]
\usepackage[T1]{fontenc}
\usepackage{multirow}
\usepackage{booktabs}
\usepackage{colortbl}
\usepackage{xcolor}
\usepackage{array}
\usepackage{arydshln}
\usepackage{tabu}

\newcolumntype{G}{>{\columncolor{black!5}\centering\arraybackslash} m{5.5cm} } 
\newcolumntype{C}{>{\centering\arraybackslash} m{1.8cm} } 

\begin{document}
\begin{table}[ht!]
    \begin{center}
    \taburulecolor{black!20}
    \begin{tabular}{G : C | C | C |}%| C |}
        %\toprule
        \cline{3-4}
             \multicolumn{2}{C |}{}  & \cellcolor{blue!25}\sf Incomes & \cellcolor{blue!25} \sf Education \\[10pt]
        \cline{1-4}
        \rowcolor{black!5}\multirow{2}{*}{\sf  Test 1}  &\textsf{p-value} & \textsf{1.0} & \textsf{0.0} \\[10pt]
        \cline{2-4}
                                            &\textsf{D} & \sf 0.0  & \sf 0.1904  \\[10pt]
        \hline
        \rowcolor{black!5}\multirow{2}{*}{\sf Test 2}   &\textsf{p-value} & \sf 0.0 & \sf 0.0 \\[10pt]
        \cline{2-4}
                                        &\textsf{P} & \sf 0 & \sf 0.8 \\[10pt]
    \bottomrule
    \end{tabular}
    \end{center}
\caption{Text}
\label{table:table1}
\end{table}
\end{document}

enter image description here

  • 2
    The column types G, : and C are all undefined by default so your example is no help at all and your question is about code you have not shown. Please fix the example to be a complete document that demonstrates the problem. – David Carlisle Aug 27 '15 at 17:39
  • @DavidCarlisle I added the definitions of G and C. Now, I think the code contains everything. The problem appears after compile. This table is a part of a very large document, so I cannot complete all code, but I think now it is very easy to fast create a document to compile. – Ziva Aug 27 '15 at 18:15
  • 1
    @Ziva: Please create a stand-alone, compilable document rather than code snippets. From the looks of it, one might need booktabs, multirow, array and tabu, but even then you still need a definition for the column rule :. Help us to help you. – Werner Aug 27 '15 at 18:17
  • @Ziva: Please see how to create a minimal working example (MWE). – Werner Aug 27 '15 at 18:18
  • 1
    Off-topic: don't use two-letter font switches in LaTeX documents. The new ones have been around for 20+ years and those are obsolete. Use e.g. \sffamily or \textsf{} etc. – cfr Aug 27 '15 at 23:01
3

The additional space forced between the lines seems problematic. It works better if you change \arraystretch instead. However, this causes the text in the left-most column to be overwritten by the colour of the next row. This can be avoided by setting the text in the next row with the number of rows passed to \multirow given as a negative, rather than positive, number.

Although this works, I do not recommend it because it depends on the use of tabu. tabu has various bugs which will not be fixed until the next update. This will break existing documents and no support will be provided in the form of a backwards-compatibility option. Only use tabu if you do not care that your documents will break suddenly at some point, possibly when you least expect it and could most do without it.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{multirow}
% \usepackage{booktabs}
\usepackage[table]{xcolor}
\usepackage{array}
\usepackage{arydshln}
% \usepackage{tabu}

\newcolumntype{G}{>{\columncolor{black!5}\centering\arraybackslash}m{5.5cm}}
\newcolumntype{C}{>{\centering\arraybackslash}m{1.8cm}}

\begin{document}
\begin{table}[ht!]
  \centering
%   \taburulecolor{black!20}
  {\sffamily
    \renewcommand\arraystretch{2}%
  \begin{tabular}{G : C | C | C |}%| C |}
    %\toprule
    \cline{3-4}
    \multicolumn{2}{C |}{}  & \cellcolor{blue!25} Incomes & \cellcolor{blue!25}  Education \\
    \cline{1-4}
    \rowcolor{black!5}  & p-value & 1.0 & 0.0 \\
    \cline{2-4}
    \multirow{-2}{*}{  Test 1} & D &  0.0  &  0.1904  \\
    \hline
    \rowcolor{black!5}   & p-value &  0.0 &  0.0 \\
    \cline{2-4}
    \multirow{-2}{*}{ Test 2} & P &  0 &  0.8 \\
%   \bottomrule
  \end{tabular}}
\caption{Text}
\label{table:table1}
\end{table}
\end{document}

Coloured cells do not work well with booktabs rules and the use of \hline and \cline as well as \bottomrule and \toprule is always going to look odd anyway, so I've commented out the \bottomrule. If you want booktabs rules, read the documentation and follow the guidelines, sticking exclusively to that package's rules. Otherwise, stick to \hline and \cline. Obviously, you'll want to have a policy for your document so that all tables are formatted in a consistent way.

fixed up (partially) table

| improve this answer | |

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.