1

I am trying to create a table which has all three traits, mentioned in the title, at once. I tried tabular with individual \color for each column, tabu with \rowfont, and custom column definitions from here and here. The last method was the only one which did not cause the double row height visible in the output, yet at the same time it made the frame lines incomplete at the right side.

Is there a method, that would give a complete frame to the table, and a proper row height at once?

Input

\usepackage{color}
\usepackage{colortbl}

\definecolor{colorh}{rgb}{0.5,0.5,0.5}   

\begin{document}
\begin{tabular}{ |p{2cm}|p{2cm}|p{2cm}|p{2cm}| } 
\hline
\rowcolor{colorh} {\color{white}a} & {\color{white}b} & {\color{white}b} & {\color{white}d} \\
\rowcolor{colorh} e & f & g & h \\                              
\hline
\end{tabular} 
\end{document}

Output

enter image description here

2
  • I don't really unserstand what you mean with the ‘proper’ row height?
    – Bernard
    Jan 29 '16 at 21:42
  • The one which is related to the height of the contents, as in the second row of the table in the example output. It has a proper height because its text has not been coloured. On the contrary, the first row has an extra space above the contents. It is not because it is the first row, as such an extra space would also be visible in any other row with coloured both background and text.
    – arataj
    Jan 29 '16 at 21:50
1

The problem disappears if you use the m{2cm} column type. If you want to use p{2cm}, a work around consists in using \textcolor{white}{cell contents} indtead of \color{white} cell contents:

\documentclass{article}
\usepackage[table, x11names]{xcolor}
\usepackage{hhline}
\definecolor{colorh}{rgb}{0.5,0.5,0.5}

\setlength\arrayrulewidth{0.25mm}
\newcolumntype{L}{>{\arraybackslash}p{2cm}}%

\begin{document}

\begin{tabular}{|L|L|L|L|}
  \hline
  \rowcolor{colorh}
  \textcolor{white}{a} & \textcolor{white}{b} & \textcolor{white}{c} & \textcolor{white}{d} \\
  \rowcolor{colorh}
  e & f & g & h \\
  \hline
\end{tabular}

\end{document} 

enter image description here

4
  • Is definition of your L column type complete? If it is, can you elaborate, what is its benefit of use \arraybackslash in it?
    – Zarko
    Jan 30 '16 at 0:33
  • I've been used to add it for a long time. If I remember well, it is there to say \` means \tabularnewline` in the last column (where it might mess up with other kinds of \`, for instance if there is a maths environment like cases` or align*.
    – Bernard
    Jan 30 '16 at 1:06
  • So far I understood, that \arraybackslah is necessary, if you change align of cell contents. For example \newcolumntype{L}{>{\raggedright\arraybackslash}p{2cm}}.
    – Zarko
    Jan 30 '16 at 1:12
  • Maybe you're right. In any case, I don't think it does any harm if used in places where it's not strictly necessary.
    – Bernard
    Jan 30 '16 at 1:30
1

With {NiceTabular} of nicematrix, you have directly the expected output.

\documentclass{article}
\usepackage{nicematrix}

\definecolor{colorh}{rgb}{0.5,0.5,0.5}   

\begin{document}

\begin{NiceTabular}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}[colortbl-like]
\hline
\RowStyle[color=white]{}
\rowcolor{colorh}a & b & b & d \\
\rowcolor{colorh} e & f & g & h \\                              
\hline
\end{NiceTabular} 

\end{document}

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

Output of the above code

I have used the key colortbl-like in order to be able to use commands to color the row with the syntax of colortbl (\rowcolor).

However, it's also possible to color the array with instructions before the array itself (in the \CodeBefore).

\documentclass{article}
\usepackage{nicematrix}

\begin{document}

\begin{NiceTabular}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
\CodeBefore
    \arraycolor[rgb]{0.5,0.5,0.5}   
\Body
    \hline
    \RowStyle[color=white]{}
    a & b & b & d \\
    e & f & g & h \\ 
    \hline
\end{NiceTabular} 

\end{document}

The output is the same.

0

I guess it is more of a workaround than a solution, but the last of the mentioned methods actually makes the rightmost line visible if it is specified twice, like |L|L|L|L||.

Input

\documentclass{article}
\usepackage{color}
\usepackage{colortbl}

\definecolor{colorh}{rgb}{0.5,0.5,0.5}

\setlength\arrayrulewidth{0.25mm}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{2cm}}

\begin{document}
\begin{tabular}{ |L|L|L|L|| }
\rowcolor{colorh} {\color{white}a} & {\color{white}b} & {\color{white}b} & {\color{white}d} \\
a & b & c & d \\
\rowcolor{colorh} e & f & g & h \\
\end{tabular} 
\end{document}

Output

enter image description here

0

With slightly changed your MWE in question, I cannot reproduce your image of your table. Also your finding in your answer is false. Try the following:

\documentclass[border=3mm]{standalone}

\usepackage[table]{xcolor}
\definecolor{colorh}{rgb}{0.5,0.5,0.5}

\begin{document}
\begin{tabular}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
    \hline
\rowcolor{colorh} 
\textcolor{white}{a} 
    & \textcolor{white}{b} 
        & \textcolor{white}{c} 
            & \textcolor{white}{d}      \\
    e & f & g & h \\
\rowcolor{colorh}
    e & f & g & h \\
    \hline
\end{tabular}
\end{document}   

enter image description here

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.