0

I am trying to color cells of a table with 3 rows of 2 cm width each using tabularx and cellcolor. I want to split an arbitrary number of cells of this table into "sub-cells" with different colors, but I can't get the spacing right even with one split-up cell:

\documentclass{article}

\usepackage{tabularx}
\usepackage{xcolor}
\usepackage{colortbl}

\begin{document}

\begin{tabularx}{6cm}{|X|>{\hsize=.2\hsize}X@{}>{\hsize=.2\hsize}X@{}>{\hsize=.2\hsize}X@{}>{\hsize=.2\hsize}X@{}>{\hsize=.2\hsize}X|X|}
  \cellcolor{gray} &
      \cellcolor{red} & \cellcolor{green} & \cellcolor{yellow} & \cellcolor{orange} & \cellcolor{blue} &
  \cellcolor{gray} \\
  1 & \multicolumn{5}{X|}{2} & 3\\
\end{tabularx}

\end{document}

As can be seen below, the blue "sub-cell" is far bigger than the red, green, yellow and orange ones, which are too small. In total, the width seems to be o.k., though.

Example with incorrect sizes

I found some tikz-based solutions here, but they use overlays which I am trying to avoid because I will likely be using a large number of tables and "sub-cells" for illustrating a document that I am currently working on. Is there any way to split each cell into "sub-cells" of equal width?

  • Why are you using tabularx? It (at least in your example) make things only complicated. – Ulrike Fischer Feb 14 '17 at 13:14
  • @UlrikeFischer: A table would have been fine as well. I just found it more convenient since I could specify a total length. – Andreas Unterweger Feb 15 '17 at 8:14
2

For that kind of figure/table I would really consider Tikz:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
  \fill[gray](0,0) rectangle (15,1.5);
  \foreach \x/\col in {5/red,6/green,7/yellow,8/orange,9/blue}{
    \fill[\col] (\x,0) rectangle +(1,1.5);
  }
  \foreach \x/\label in {0/1,5/2,10/3,15/}{
    \draw[thin] (\x,-1.5) -- +(0,3) node[pos=0.5,anchor=north west]{\Huge\label};
  }
\end{tikzpicture}
\end{document}

enter image description here

  • That's a lot simpler than the other solutions I found on SO. I'll use that. Thank you. – Andreas Unterweger Feb 15 '17 at 8:15

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.