TransWikia.com

Colouring cells in a table

TeX - LaTeX Asked on February 25, 2021

I am trying to create a table with specific cells coloured either green or red. As a trial, I used the following code to colour one cell green

documentclass[a4paper,12pt]{extarticle}
%% Language and font encodings
usepackage[english]{babel}
usepackage[utf8x]{inputenc}
usepackage[T1]{fontenc}

%% Sets page size and margins
usepackage[a4paper,top=2.5cm,bottom=2.5cm,left=2.5cm,right=2.5cm,marginparwidth=1.75cm]{geometry}

usepackage{amsmath}
usepackage{amsfonts} 
usepackage{amssymb}
usepackage{amsthm}
usepackage{mathtools}
usepackage{optidef} 
usepackage[framed,numbered,autolinebreaks,useliterate]{mcode}
usepackage[table]{xcolor}

begin{document}
begin{table}
centering
begin{tabular}{ | l | l | l | } 
     hline
      A & B & C 
      hline
      D & E & cellcolor{green}F 
      hline
      G & H & I 
      hline
end{tabular}
end{table}
end{document}

Here I have used usepackage[table]{xcolor}. This did not work, and the output generated is shown below

enter image description here

Is there a way to create a table with specific cell colours

2 Answers

mcode is not in the standard distributions but you can see the same effect using xcolor

documentclass[a4paper,12pt]{extarticle}
%% Language and font encodings
usepackage[english]{babel}
usepackage[utf8x]{inputenc}
usepackage[T1]{fontenc}

%% Sets page size and margins
usepackage[a4paper,top=2.5cm,bottom=2.5cm,left=2.5cm,right=2.5cm,marginparwidth=1.75cm]{geometry}

usepackage{amsmath}
usepackage{amsfonts} 
usepackage{amssymb}
usepackage{amsthm}
usepackage{mathtools}
usepackage{optidef} 
usepackage{xcolor}
usepackage[table]{xcolor}

begin{document}
    begin{tabular}{ | l | l | l | }
      hline
      A & B & C 
      hline
      D & E & cellcolor{green}F 
      hline
      G & H & I 
      hline
    end{tabular}
end{document}

this produces the error

! LaTeX Error: Option clash for package xcolor.

and if you scroll past that the error

! Undefined control sequence.
l.24       D & E & cellcolor
                             {green}F 
? 

as cellcolor is not defined you get the output that you show.

Note that after any error the PDF output is not intended to be usable, it is at best, usable as a debugging aid. Never ignore error messages.

To fix the option clash, load xcolor earlier with the option, so simply swap the lines:

documentclass[a4paper,12pt]{extarticle}
%% Language and font encodings
usepackage[english]{babel}
%usepackage[utf8x]{inputenc}
usepackage[T1]{fontenc}

%% Sets page size and margins
usepackage[a4paper,top=2.5cm,bottom=2.5cm,left=2.5cm,right=2.5cm,marginparwidth=1.75cm]{geometry}

usepackage{amsmath}
usepackage{amsfonts} 
usepackage{amssymb}
usepackage{amsthm}
usepackage{mathtools}
usepackage{optidef} 
usepackage[table]{xcolor}
usepackage{xcolor}

begin{document}
    begin{tabular}{ | l | l | l | }
      hline
      A & B & C 
      hline
      D & E & cellcolor{green}F 
      hline
      G & H & I 
      hline
    end{tabular}
end{document}

Correct answer by David Carlisle on February 25, 2021

enter image description here

documentclass{article}
usepackage[table]{xcolor}% http://ctan.org/pkg/xcolor
begin{document}
begin{tabular}{l|c|r}
  hline
  Some & cellcolor{blue!25}coloured & contents 
  hline
end{tabular}

begin{tabular}{ | l | l | l | }
    hline
    A & B & C 
    hline
    D & E & cellcolor{green}F 
    hline
    G & H & I 
    hline
end{tabular}
end{document}

Answered by js bibra on February 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP