Graphics, Figures & TablesColored squares in tabular

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
bgeus
Posts: 2
Joined: Wed Apr 28, 2010 2:39 pm

Colored squares in tabular

Post by bgeus »

I need to create some sort of schedule with colored squares. I tried to do this using \cellcolor from colortbl. This works, but this fills the entire cell, which is not very appealing or print friendly.
So I tried \colorbox instead, which creates smaller squares, but not centered vertically and not proportional to the cell size.

The attached pdf / code shows what I tried.

Does anybody have a solution or idea to do this?

Code: Select all

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage{rotating}
\usepackage{longtable}
\usepackage{colortbl}

\begin{document}
\centering
\begin{tabular}{|c||c|c|}
\hline
 & \begin{sideways}abc\ \ \end{sideways} & \begin{sideways}abc\ \ \end{sideways} \\
\hline
\hline
 Mo & \cellcolor[rgb]{1,0.5,1} & \cellcolor[rgb]{1,0,0} \\
\hline
 Di & \cellcolor[rgb]{1,0.5,1} & \cellcolor[rgb]{1,0,0} \\
\hline
 Wo & \cellcolor[rgb]{1,0.5,1} & \cellcolor[rgb]{1,0,0} \\
\hline
\end{tabular} \\

\begin{tabular}{|c||c|c|}
\hline
 & \begin{sideways}abc\ \ \end{sideways} & \begin{sideways}abc\ \ \end{sideways} \\
\hline
 Mo & \colorbox[rgb]{1,0.5,1}{} & \colorbox[rgb]{1,0,0}{} \\
\hline
 Di & \colorbox[rgb]{1,0.5,1}{} & \colorbox[rgb]{1,0,0}{} \\
\hline
 Wo & \colorbox[rgb]{1,0.5,1}{} & \colorbox[rgb]{1,0,0}{} \\
\hline
\end{tabular}

\end{document}
Attachments
test.pdf
Example
(12.68 KiB) Downloaded 459 times

Recommended reading 2024:

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

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Colored squares in tabular

Post by gmedina »

Hi,

the following code could give you some ideas:

Code: Select all

\documentclass[a4paper]{article}
\usepackage{array}
\usepackage{graphicx}
\usepackage[tables]{xcolor}

\definecolor{mycolor}{rgb}{1,0.5,1}
\newcommand\ColorSquare[1]{{\color{#1}\rule{3mm}{3mm}}}

\begin{document}

\centering
\setlength\extrarowheight{3pt}
\begin{tabular}{*{3}{|>{\centering\arraybackslash}m{5mm}}|}
  \hline
  & \rotatebox{90}{abc } & \rotatebox{90}{abc }\\
  \hline
  Mo & \ColorSquare{mycolor} & \ColorSquare{red}\\
  \hline
  Di & \ColorSquare{mycolor} & \ColorSquare{red}\\
  \hline
  Wo & \ColorSquare{mycolor} & \ColorSquare{red}\\
  \hline
\end{tabular}

\end{document}
By changing the lengths for the \rule command and the value for \extrarowheight, you could achieve the desired layout.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
bgeus
Posts: 2
Joined: Wed Apr 28, 2010 2:39 pm

Re: Colored squares in tabular

Post by bgeus »

Thanks, you solved my problem :D
Post Reply