2

I have a problem with colors and lines in tables. I have this code:

\usepackage{multirow}
\usepackage{colortbl}
        \begin{tabular}{ll|c|c|c|c|c|c|}  \cline{3-8} \cline{3-8}
         & & \multicolumn{3}{c|}{\cellcolor{gray!25} Cramér von Mises} & \multicolumn{3}{c|}{\cellcolor{gray!25} Anderson-Darling} \\  \cline{3-8} \cline{3-8}
         & & \cellcolor{gray!25}$\alpha=$0.1 & \cellcolor{gray!25}$\alpha=$0.05  & \cellcolor{gray!25}$\alpha=$0.01 & \cellcolor{gray!25}$\alpha=$0.1 & \cellcolor{gray!25}$\alpha=$0.05 & \cellcolor{gray!25}$\alpha=$0.01 \\ \hline
        \multicolumn{1}{|c}{} & \cellcolor{gray!25}n=25 & 0.106 & 0.063 & 0.015 & 0.107 & 0.052 & 0.012 \\ 
        \multicolumn{1}{|c}{} & \cellcolor{gray!25}n=50 & 0.107 & 0.043 & 0.010 & 0.103 & 0.054 & 0.010 \\ 
        \multicolumn{1}{|c}{\multirow{3}{0.3cm}[7mm]{\begin{sideways}$\qquad$ $Exp(1)$ \end{sideways}}}&\cellcolor{gray!25} n=100 & 0.104 & 0.065 & 0.017 & 0.110 & 0.055 & 0.015 \\ \hline
        \multicolumn{1}{|c}{} & \cellcolor{gray!25}n=25 & 0.113 & 0.055 & 0.014 & 0.097 & 0.058 & 0.016 \\ 
         \multicolumn{1}{|c}{} & \cellcolor{gray!25}n=50 & 0.109 & 0.058 & 0.014 & 0.098 & 0.056 & 0.018 \\ 
         \multicolumn{1}{|c}{ \multirow{3}{0.3cm}[7mm]{\begin{sideways}$\qquad$ $Exp(2)$ \end{sideways}}} & \cellcolor{gray!25}n=100 & 0.110 & 0.048 & 0.011 & 0.108 & 0.044 & 0.009  \\ \hline
          \end{tabular}

I need to color the multirow cell and the only thing I could arise was to color the lower part of it.

My other problem is when I coloured the cells, I cant see properly the rules of the table.

I hope someone can help me.

Thanks!!!!

3

3 Answers 3

5

Always supply a complete, but minimal document. Yours is missing \documentclass as well as \begin and \end document. It is also missing the loading of xcolor (for the color names like gray!25) and rotating (for the sideways environment). To color the whole multirow you need to color all three cells of it. Also, your use of \multirow is incorrect. If you want to put the text into the cells above it, the number should be negative. If you do that, you don't need the optional adjustments of [7mm] nor the \qquads. I've done a few other things I think make the result better: Taking Exp out of math mode, putting the numbers after \alpha= into math mode, removing some errant spaces, and removing most of the vertical lines:

\documentclass{article}
\usepackage{multirow,xcolor,colortbl,rotating}
\begin{document}
\noindent\begin{tabular}{ll|cccccc|}  \cline{3-8} \cline{3-8}
   &&\multicolumn{3}{c} {\cellcolor{gray!25} Cram\'er von Mises}&
     \multicolumn{3}{c|}{\cellcolor{gray!25} Anderson-Darling}\\
  \cline{3-8} \cline{3-8}
   &&\cellcolor{gray!25}$\alpha=0.1 $ &\cellcolor{gray!25}$\alpha=0.05$&
     \cellcolor{gray!25}$\alpha=0.01$ &\cellcolor{gray!25}$\alpha=0.1 $&
     \cellcolor{gray!25}$\alpha=0.05$ &\cellcolor{gray!25}$\alpha=0.01$\\
  \hline
  \multicolumn{1}{|c}{\cellcolor{gray!25}} & \cellcolor{gray!25}n=25 & 0.106 & 
    0.063 & 0.015 & 0.107 & 0.052 & 0.012 \\
  \multicolumn{1}{|c}{\cellcolor{gray!25}} & \cellcolor{gray!25}n=50 & 0.107 & 
    0.043 & 0.010 & 0.103 & 0.054 & 0.010 \\
  \multicolumn{1}{|c}{\cellcolor{gray!25}%
    \multirow{-3}{*}{\begin{sideways}Exp$(1)$\end{sideways}}}&
    \cellcolor{gray!25}n=100 & 0.104 & 0.065 & 0.017 & 0.110 & 0.055 & 0.015 \\
  \hline
  \multicolumn{1}{|c}{\cellcolor{gray!25}} & \cellcolor{gray!25}n=25 & 0.113 &
    0.055 & 0.014 & 0.097 & 0.058 & 0.016 \\
  \multicolumn{1}{|c}{\cellcolor{gray!25}} & \cellcolor{gray!25}n=50 & 0.109 & 
    0.058 & 0.014 & 0.098 & 0.056 & 0.018 \\
  \multicolumn{1}{|c}{\cellcolor{gray!25}%
    \multirow{-3}{*}{\begin{sideways}Exp$(2)$\end{sideways}}}&
    \cellcolor{gray!25}n=100 & 0.110 & 0.048 & 0.011 & 0.108 & 0.044 & 0.009  \\
  \hline
\end{tabular}
\end{document}

(I've also broken the lines so people reading this don't have to scroll three screens over to see the whole code.)

4

I think that the ConTeXt interface for handling multiple rows and columns is much nicer as it provides a clean separation of content and presentation. For example, without any styling, the above table can be typeset as (I simply copy pasted the data in the Exp(2) part).

\starttext

\bTABLE
  \bTR
    \bTD[nc=2] \eTD
    \bTD[nc=3] Cramér von Mises \eTD
    \bTD[nc=3] Anderson-Darling \eTD
  \eTR
  \bTR
    \bTD[nc=2] \eTD
    \bTD $α = 0.1$  \eTD
    \bTD $α = 0.05$ \eTD
    \bTD $α = 0.01$ \eTD
    \bTD $α = 0.1$  \eTD
    \bTD $α = 0.05$ \eTD
    \bTD $α = 0.01$ \eTD
  \eTD
  \bTR
    \bTD[nr=3] \rotate{Exp(1)} \eTD
    \bTD $n = 25$ \eTD
    \bTD 0.106 \eTD
    \bTD 0.063 \eTD
    \bTD 0.015 \eTD
    \bTD 0.107 \eTD
    \bTD 0.052 \eTD
    \bTD 0.012 \eTD
  \eTR
  \bTR
    \bTD $n = 50$ \eTD
    \bTD 0.107 \eTD
    \bTD 0.043 \eTD
    \bTD 0.010 \eTD
    \bTD 0.103 \eTD
    \bTD 0.054 \eTD
    \bTD 0.010 \eTD
  \eTR
  \bTR
    \bTD $n = 100$ \eTD
    \bTD 0.10R \eTD
    \bTD 0.065 \eTD
    \bTD 0.017 \eTD
    \bTD 0.110 \eTD
    \bTD 0.055 \eTD
    \bTD 0.015 \eTD
  \eTR
  \bTR
    \bTD[nr=3] \rotate{Exp(2)} \eTD
    \bTD $n = 25$ \eTD
    \bTD 0.106 \eTD
    \bTD 0.063 \eTD
    \bTD 0.015 \eTD
    \bTD 0.107 \eTD
    \bTD 0.052 \eTD
    \bTD 0.012 \eTD
  \eTR
  \bTR
    \bTD $n = 50$ \eTD
    \bTD 0.107 \eTD
    \bTD 0.043 \eTD
    \bTD 0.010 \eTD
    \bTD 0.103 \eTD
    \bTD 0.054 \eTD
    \bTD 0.010 \eTD
  \eTR
  \bTR
    \bTD $n = 100$ \eTD
    \bTD 0.10R \eTD
    \bTD 0.065 \eTD
    \bTD 0.017 \eTD
    \bTD 0.110 \eTD
    \bTD 0.055 \eTD
    \bTD 0.015 \eTD
  \eTR

\eTABLE


\stoptext

which gives

enter image description here

Now, lets add styling one by one. First, create a setup to add the frames:

\startsetups table:frame
  \setupTABLE[frame=off]

  \setupTABLE[row][1,3,6][topframe=on]
  \setupTABLE[row][last][bottomframe=on]

  \setupTABLE[column][1,3][leftframe=on]
  \setupTABLE[column][last][rightframe=on]

  % Correct for spanning cells.
  \setupTABLE[6][2][rightframe=on]
  \setupTABLE[1][6][bottomframe=on]

  % Correct for missing top left cell
  \setupTABLE[1][1,2][topframe=off, leftframe=off]

\stopsetups

and use is using

\bTABLE[setups={table:frame}

which gives:

enter image description here

Next add the background to the top row and left column by creating another setup

\startsetups table:background
  \setupTABLE[background=color]
  \setupTABLE[row][1,2][backgroundcolor=gray]
  \setupTABLE[column][1,2][backgroundcolor=gray]

  % Correct for missing top left cell
  \setupTABLE[1,2][1,2][background=]
\stopsetups

and use it using:

\bTABLE[setups={table:frame, table:background}]

which gives:

enter image description here

Finally, correct the alignment and spacing by creating a new setups:

\startsetups table:style
  % Middle align all data columns
  \setupTABLE[align={middle,lohi}]

  % ... except the 2nd column
  \setupTABLE[column][5][align=flushleft]

  % Add spacing
  \setupTABLE[loffset=0.25em, roffset=0.25em]

\stopsetups

and using it using:

\bTABLE[setups={table:frame, table:background, table:style}]

which gives:

enter image description here

Note that the code is much more readable than in LaTeX, and it is relatively easy to change the style in the future.

1
  • Thank you very much!!! I'm new using this thing and that ugly code was the best I could haver done.
    – Maribel
    Commented Nov 30, 2013 at 10:53
0

Here is a way to construct that table with {NiceTabular} of nicematrix. That environment is similar to the classical {tabular} (as provided by array) but adds new functionalities.

In particular, when the key corners is used, the empty corners are automatically computed by nicematrix. Then the commands which draw rules (eg \Hline provided by nicematrix and similar to \hline) and color cells take into account that corners.

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{center}
\begin{NiceTabular}{|ll|cccccc|}[corners]  
\CodeBefore
  \rowcolor{gray!25}{1,2}
  \columncolor{gray!25}{1,2}
\Body
\Hline
   && \Block{1-3}{Cramér von Mises}&&& \Block{1-3}{Anderson-Darling} \\
   &&$\alpha=0.1 $ &$\alpha=0.05$& $\alpha=0.01$ &$\alpha=0.1 $& $\alpha=0.05$ &$\alpha=0.01$ \\
\Hline
  \Block{3-1}{\rotate Exp$(1)$} 
   & n=25 & 0.106 & 0.063 & 0.015 & 0.107 & 0.052 & 0.012 \\
   & n=50 & 0.107 & 0.043 & 0.010 & 0.103 & 0.054 & 0.010 \\
   & n=100 & 0.104 & 0.065 & 0.017 & 0.110 & 0.055 & 0.015 \\
\Hline
  \Block{3-1}{\rotate Exp$(2)$} 
   & n=25 & 0.113 & 0.055 & 0.014 & 0.097 & 0.058 & 0.016 \\
   & n=50 & 0.109 & 0.058 & 0.014 & 0.098 & 0.056 & 0.018 \\
   & n=100 & 0.110 & 0.048 & 0.011 & 0.108 & 0.044 & 0.009  \\
\Hline
\end{NiceTabular}
\end{center}

\end{document}

You need several compilations (because nicematrix uses PGF/Tikz nodes under the hood).

Output of the above code

The rules won't seem to disappear under the colored panels, whatever PDF viewer you use.

You must log in to answer this question.

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