4

I was wondering if there is any way to shade some cells in table without removing lines. I starged with this command

\cellcolor[gray]{0.9}

however upper line of the cell disapeard. Is there any better way to color some cells ?? PS I use longtable

Here is the code

    \documentclass[a4paper,12pt]{mwrep}
\usepackage[MeX]{polski}
\usepackage[cp1250]{inputenc}
\usepackage[left=3.0cm,right=3cm,top=3.0cm,bottom=3.0cm]{geometry}
\usepackage{amsmath,amssymb}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{algorithmic}
\usepackage{colortbl}
\usepackage{dcolumn}
\usepackage{float}
\usepackage{tabularx}
\usepackage{longtable}
\usepackage[centerlast]{caption}
\usepackage{multirow}
\usepackage[section] {placeins}
\usepackage{booktabs}
\usepackage{xcolor}
\linespread{1.3}
\begin{document}
\begin{longtable}{!{\vrule width 2pt}c!{\vrule width 2pt}c!{\vrule width 2pt}c|c|c!{\vrule width 2pt}c|c|c!{\vrule width 2pt}c!{\vrule width 2pt}c!{\vrule width 2pt}}
\caption{Zależność wydajności algorytmu od jego parametrów.}
\label{tab:parameters}
\\ \hline
\multirow{2}{*}{Parametr} & \multirow{2}{*}{Wartość} & \multicolumn{3}{|c!{\vrule width 2pt}}{\textbf{Czas[s]}}  & \multicolumn{3}{|c!{\vrule width 2pt}}{$\rho_{a}$} \\ \cline{3-10}
& & Zestaw 1 & Zestaw 2  & Zestaw 3  & Zestaw 1  & Zestaw 2  & Zestaw 3 \\ \hline
\multirow{4}{*}{$\rho_{select}$} &  0.5 &   58.0    &   266.4   &   131.4   &   0.98    &   0.8 &   0.95        \\ \cline{2-10}
    & 0.6   &   55.0    &   270.1   &   145.6   &   0.95    &   0.85    &   0.85        \\ \cline{2-10}
    & \cellcolor[gray]{0.9} 0.7 &   55.3    &   256.6   &   143.1   &   0.99    &   0.75    &   0.90        \\ \cline{2-10}
    & 0.8   &   72.0    &   347.2   &   156.9   &   1.00    &   0.87    &   0.97        \\ \hline
\end{longtable}
\end{document}
lockstep
  • 250,273
geronimo
  • 165

2 Answers2

6

Your problem here is noted as a 'feature' of \cline{...} when using it in conjunction with \cellcolor{...}. The colortbl package documentation mentions the following:

10 Less fun with \cline

Lines produced by \cline are coloured if you use \arrayrulecolor but you may not notice as they are covered up by any colour pannels in the following row. This is a 'feature' of \cline. If using this package you would probably better using the - rule type in a \hhline argument, rather than \cline.

Therefore, my suggestion uses the hhline package from the same author (David Carlisle). See the package documentation for more details on how to specify the \hhline rules:

\documentclass[a4paper,12pt]{mwrep}
...
\usepackage{hhline}% http://ctan.org/pkg/hhline
\begin{longtable}{!{\vrule width 2pt}c!{\vrule width 2pt}c!{\vrule width 2pt}c|c|c!{\vrule width 2pt}c|c|c!{\vrule width 2pt}c!{\vrule width 2pt}c!{\vrule width 2pt}}
\caption{Zaleznosc wydajnosci algorytmu od jego parametrów.}
\label{tab:parameters}
\\ \hline
\multirow{2}{*}{Parametr} & \multirow{2}{*}{Wartosc} & \multicolumn{3}{|c!{\vrule width 2pt}}{\textbf{Czas[s]}}  & \multicolumn{3}{|c!{\vrule width 2pt}}{$\rho_{a}$} \\ \cline{3-10}
& & Zestaw 1 & Zestaw 2  & Zestaw 3  & Zestaw 1  & Zestaw 2  & Zestaw 3 \\ \hline
\multirow{4}{*}{$\rho_{select}$} &  0.5 &   58.0    &   266.4   &   131.4   &   0.98    &   0.8 &   0.95        \\ \cline{2-10}
    & 0.6   &   55.0    &   270.1   &   145.6   &   0.95    &   0.85    &   0.85        \\ \hhline{|~|-|-|-|-|-|-|-|}% New line specification
    & \cellcolor[gray]{0.9} 0.7 &   55.3    &   256.6   &   143.1   &   0.99    &   0.75    &   0.90        \\ \cline{2-10}
    & 0.8   &   72.0    &   347.2   &   156.9   &   1.00    &   0.87    &   0.97        \\ \hline
\end{longtable}
\end{document}

Longtable with \hhline horizontal lines

However, the problem is not entire gone as you can see when you zoom in to the spread of the empty horizontal line (via the ~ \hhline specifier) across column 1:

\hhline artefact in longtable

I think this stems from your use of \vrule width 2pt as your column rules/widths. And, as such, may be hard to circumvent. This minute problem will most certainly not show up in print, and may therefore be not a problem. Moreover, similar misalignment have gone unnoticed across some of the vertical lines in your columns:

Longtable with vertical misaligned lines

These are more readily fixed using

\multicolumn{3}{c!{\vrule width 2pt}}{\textbf{Czas[s]}} &
\multicolumn{3}{c!{\vrule width 2pt}}{$\rho_{a}$} \

instead of

\multicolumn{3}{|c!{\vrule width 2pt}}{\textbf{Czas[s]}} &
\multicolumn{3}{|c!{\vrule width 2pt}}{$\rho_{a}$} \

Also note the slight misaligned 0.7 with the rest of the columns entries. That's from leaving a space between the cell colour definition and the actual cell entry:

\cellcolor[gray]{0.9} 0.7

Fix this with:

\cellcolor[gray]{0.9}0.7
Werner
  • 603,163
  • Hmm, I checked my document in quite a lot of zoom levels, but all the time the upper lines are missing – geronimo Aug 06 '11 at 23:18
  • 2
    Then post a minimal working example of the longtable for others to work with. – Werner Aug 06 '11 at 23:36
  • I edited my question and added some code – geronimo Aug 07 '11 at 10:51
  • See my revised answer that (somewhat) addresses your concern. Perhaps not optimal, but I'm not sure whether this is sufficient. Your problem deals with a 'feature' of the \cline command. – Werner Aug 07 '11 at 16:42
0

The latest version of nicematrix provides a solution to that problem.

\documentclass[a4paper,10pt,landscape]{article}
\usepackage{nicematrix,tikz}

\begin{document}

\NiceMatrixOptions { custom-line = { letter = I , tikz = { line width = 2 pt } } }

\renewcommand{\arraystretch}{1.2}

\begin{NiceTabular}{Iwc{17mm}Iwc{13mm}Ic|c|cIc|c|cI}[hlines,color-inside] \Block{2-1}{Parametr} & \Block{2-1}{Wartość} & \Block{1-3}{\textbf{Czas[s]}} &&& \Block{1-3}{$\rho_{a}$} &&\ & & Zestaw 1 & Zestaw 2 & Zestaw 3 & Zestaw 1 & Zestaw 2 & Zestaw 3 \ \Block{4-1}{$\rho_{\text{select}}$} & 0.5 & 58.0 & 266.4 & 131.4 & 0.98 & 0.8 & 0.95 \ & 0.6 & 55.0 & 270.1 & 145.6 & 0.95 & 0.85 & 0.85 \ & \cellcolor{gray!15} 0.7 & 55.3 & 256.6 & 143.1 & 0.99 & 0.75 & 0.90 \ & 0.8 & 72.0 & 347.2 & 156.9 & 1.00 & 0.87 & 0.97 \ \end{NiceTabular}

\end{document}

You need several compilations.

Output of the first code

However, the output is not perfect in the corners (it's not usual to have a frame with rules of different widths...).

Zoom on a corner of the tabular

It's possible to adjust the first and the last horizontal rules to have a perfect output.

\documentclass[a4paper,10pt,landscape]{article}
\usepackage{nicematrix,tikz}

\begin{document}

\NiceMatrixOptions { custom-line = { letter = I , tikz = { line width = 2 pt } } }

\newcommand{\MyHline}{\Hline[tikz={shorten < = -1pt, shorten > = -0.8pt}]}

\renewcommand{\arraystretch}{1.2}

\begin{NiceTabular}{Iwc{17mm}Iwc{13mm}Ic|c|cIc|c|cI}[color-inside] \MyHline \Block{2-1}{Parametr} & \Block{2-1}{Wartość} & \Block{1-3}{\textbf{Czas[s]}} &&& \Block{1-3}{$\rho_{a}$} &&\ \Hline & & Zestaw 1 & Zestaw 2 & Zestaw 3 & Zestaw 1 & Zestaw 2 & Zestaw 3 \ \Hline \Block{4-1}{$\rho_{\text{select}}$} & 0.5 & 58.0 & 266.4 & 131.4 & 0.98 & 0.8 & 0.95 \ \Hline & 0.6 & 55.0 & 270.1 & 145.6 & 0.95 & 0.85 & 0.85 \ \Hline & \cellcolor{gray!15} 0.7 & 55.3 & 256.6 & 143.1 & 0.99 & 0.75 & 0.90 \ \Hline & 0.8 & 72.0 & 347.2 & 156.9 & 1.00 & 0.87 & 0.97 \ \MyHline \end{NiceTabular}

\end{document}

Part of the output of the second code

F. Pantigny
  • 40,250