Graphics, Figures & Tableshighlighting cells in table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
suzanne hugo
Posts: 7
Joined: Wed Jan 28, 2009 11:25 am

highlighting cells in table

Post by suzanne hugo »

I am trying to highlight individual cells in a table. This is all fine, but the first cell I want to highlight is the top left cell of the table. When I highlight it, the border lines of the table on the top and left of the cell are no longer visible. How do I ensure that the cell colouring lies inside the table border? Thanks.

This is an example of the basic code I have so far:

Code: Select all

\begin{tabularx}{.7\textwidth}{K K|K K K K|}
  \multicolumn{2}{c}{} & \textrm{Piano} & \textrm{Trumpet} & \textrm{Flute} & \multicolumn{1}{c}{\textrm{Viola}}\\
  \multicolumn{2}{c}{}& \textbf{pno} & \textbf{tpt} & \textbf{flt} & \multicolumn{1}{c}{\textbf{vla}}\\ \cline{3-6} 
  \textrm{Piano} & \textbf{pno} & 5.9 \cellcolor[gray]{.8}& 2.5 & 2.5 & 7.5\\
  \textrm{Trumpet} & \textbf{tpt} & 2.0 & 7.1 \cellcolor[gray]{.8}& 0.25 & 6.5\\
  \textrm{Flute} & \textbf{flt} & 1.2 & 4.4 & 10.3 \cellcolor[gray]{.8} & 5.7\\
  \textrm{Viola} & \textbf{vla} & 3.4& 2.8 & 9.4 & 6.3 \cellcolor[gray]{.8}\\ \cline{3-6}
\end{tabularx}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

fatra2
Posts: 126
Joined: Fri May 01, 2009 1:43 pm

Re: highlighting cells in table

Post by fatra2 »

Are you sure that this problem persist when you print your document. I have seen often that the "problem" come from the display and not from the code.

Cheers
suzanne hugo
Posts: 7
Joined: Wed Jan 28, 2009 11:25 am

Re: highlighting cells in table

Post by suzanne hugo »

Yup, quite sure...the grey cell colour replaces the table border on the top when printed
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

highlighting cells in table

Post by gmedina »

Hi suzanne hugo,

please post a complete, compilable minimal example that allows us to reproduce the odd behaviour mentioned.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
suzanne hugo
Posts: 7
Joined: Wed Jan 28, 2009 11:25 am

highlighting cells in table

Post by suzanne hugo »

here is a basic compilable version of the code i would like to implement:

Code: Select all

\documentclass[a4paper]{article} 
\usepackage{tabularx,colortbl}

\begin{document}
\begin{tabularx}{.7\textwidth}{c|c c c c|}
 \multicolumn{1}{c}{}& \textbf{pno} & \textbf{tpt} & \textbf{flt} &      \multicolumn{1}{c}{\textbf{vla}}\\ \cline{2-5} 
  \textbf{pno} & 5.9 \cellcolor[gray]{.8}& 2.5 & 2.5 & 7.5\\
  \textbf{tpt} & 2.0 & 7.1 \cellcolor[gray]{.8}& 0.25 & 6.5\\
  \textbf{flt} & 1.2 & 4.4 & 10.3 \cellcolor[gray]{.8} & 5.7\\
  \textbf{vla} & 3.4& 2.8 & 9.4 & 6.3 \cellcolor[gray]{.8}\\ 
  \cline{2-5}
\end{tabularx}
\end{document}
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

highlighting cells in table

Post by gmedina »

Hi,

sometimes the \cellcolor command causes some problems with \cline; in these cases it's necessary to use \hhline (from the hhline package) instead. The following variation of your code shows OK (in my system, at least):

Code: Select all

\documentclass{article}
\usepackage{tabularx}
\usepackage{colortbl}
\usepackage{hhline}

\begin{document}

\begin{tabularx}{.7\textwidth}{>{\bfseries}c|c c c c|}
  \multicolumn{1}{c}{}& \textbf{pno} & \textbf{tpt} & \textbf{flt} & \multicolumn{1}{c}{\textbf{vla}}\\
  \hhline{~----}
  pno & 5.9 \cellcolor[gray]{.8}& 2.5 & 2.5 & 7.5\\
  tpt & 2.0 & 7.1 \cellcolor[gray]{.8}& 0.25 & 6.5\\
  flt & 1.2 & 4.4 & 10.3 \cellcolor[gray]{.8} & 5.7\\
  vla & 3.4& 2.8 & 9.4 & 6.3 \cellcolor[gray]{.8}\\
  \hhline{~----}
\end{tabularx}

\end{document}
Refer to the documentation of hhline for further information. I also used the >{declaration} construct in the format of the first column; an explanation can be found in the documentation of the array package.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
suzanne hugo
Posts: 7
Joined: Wed Jan 28, 2009 11:25 am

Re: highlighting cells in table

Post by suzanne hugo »

thank you :) it works perfectly. i'll be sure to check out the documentation for more efficient table construction!
paularmand
Posts: 1
Joined: Wed Jul 13, 2011 12:30 pm

highlighting cells in table

Post by paularmand »

Please note that hhline ~ will not NOT print a horizontal line, but will add a white horizontal line. Hence, we used with colored multirow cells, a white separator will show. To prevent this, check the following example:

Code: Select all

 \begin{table}
	\centering
    \begin{threeparttable}[t]
	\caption{Schedule and setup of the test experiment}
	\label{tab:experiment_timeline}
      \begin{tabular}{|c|c|c|c|c|c|}
	\hline
	Group A & Group B & Group C  & Group D  & Time &  \\
	\cline{1-5} 
	\multicolumn{4}{|c|}{Introduction to the experiment} & & \\
	\cline{1-5} 
	\multicolumn{4}{|c|}{Personal questionnaire} & & \\
	\cline{1-5} 
	\multicolumn{4}{|c|}{PAnDA tool explanation} & & \\
	\cline{1-5} 
	\multicolumn{4}{|c|}{Design Problem DP1} & & \\
	\hhline{|-----|~|}
	\cellcolor[gray]{0.9} N & \cellcolor[gray]{0.9} N & \cellcolor[gray]{0.9} N & \cellcolor[gray]{0.9} N & 15 min. & \\
	\cline{1-5} 
	\multicolumn{4}{|c|}{Questionnaire DP1} & & \\

	\cline{1-5} 
	\multicolumn{4}{|c|}{Design Problem DP2} & & \\
	\hhline{|-----|~|}
	& W & \cellcolor[gray]{0.9} N & \cellcolor[gray]{0.9} & 10 min. & \\
	\hhline{|~|-->{\arrayrulecolor[gray]{0.9}}->{\arrayrulecolor{black}}|-~|}
	\multirow{-2}{*}{W} & \cellcolor[gray]{0.9} N & W & \multirow{-2}{*}{\cellcolor[gray]{0.9} N} & 10 min. & \\
	\cline{1-5}
      \end{tabular}
    \end{threeparttable}
  \end{table}
which results in:
table_hhline_illustration
table_hhline_illustration
latex_table_hhline_example.png (72.07 KiB) Viewed 32249 times
Post Reply