1

I have a table and I want to highlight some cells with color with \cellcolor.

However the horizontal alignment is distorted, as you can see in the image.

Can this be corrected with the current approach?

I didn't find any relevant information and a radically new approach would be preferably avoided.

I found only the workaround of adding a(n extra) ~ in the beginning of the non-highlighted cells of the same column. It works, but this is a workaround.

\documentclass{article}

\usepackage{array}
\usepackage{hhline}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}

\begin{document}
\begin{table}[h]
    \footnotesize 
    \begin{center}
        \caption{blah}
        \label{fig:QResults}
        \setlength{\tabcolsep}{1pt}
        \begin{tabular}{|l|c|c|}                                                       \hhline{-|~|-}
            \multicolumn{1}{|c|}{~~Features~~} & \multicolumn{1}{ c|}{} & ~~error~~ \\ \hhline{-|~|-}
            \noalign{\smallskip}                                                       \hhline{-|~|-}
            \cellcolor[gray]{0.8} {~~feat1~~} & {} & \cellcolor[gray]{0.8} {$1.49$} \\ \hhline{-|~|-}
                                  {~~feat2~~} & {} &                       {$4.86$} \\ \hhline{-|~|-}
                                  {~~feat3~~} & {} &                       {$4.67$} \\ \hhline{-|~|-}
        \end{tabular}
    \end{center}
\end{table}
\end{document}

enter image description here

3
  • I'm not sure why you are using ~~ to force spaces around the cell content. Have you tried without? Oct 9, 2014 at 0:34
  • Yes I tried, these are there just to add some space between the text and the vertical lines, to allow the text to "breathe". Please note that the effect is the same for the last column, where there are no ~~. I updated the 1st post with the same numbers on all rows, so that the effect it more visible.
    – dim_tz
    Oct 9, 2014 at 0:36
  • I just edited your post so that the example is reproducible (so that we can copy/paste it and compile it directly) -- this should appear shortly. Oct 9, 2014 at 0:42

1 Answer 1

3

You should get rid of the space between cellcolor and the cell value:

\cellcolor[gray]{0.8}~~feat1~~ & {} & \cellcolor[gray]{0.8}{$1.49$} \\ \hhline{-|~|-}

This works.

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.