4

Is there some way to color only part of a table cell? For example, the top third, or bottom half of a cell?

For example:

\renewcommand{\arraystretch}{3.0}
\begin{tabularx}{10cm}{|X|X|}
\hline
Row 1 & \cellcolor{blue!25} 1/3 colored \\
\hline
Row 2 & uncolored \\
\hline
\end{tabularx}

\renewcommand{\arraystretch}{1.0}
\begin{tabularx}{10cm}{|X|X|}
\hline
 &\cellcolor{blue!25} \\
 & \\
 & \\
\hline
 & \\
 & \\
 & \\
\hline
\end{tabularx}

\renewcommand{\arraystretch}{1.0}
\begin{tabularx}{10cm}{|X|X|}
\hline
Row 1 &\cellcolor{blue!25} 1/3 colored\\
 & \\
 & \\
\hline
Row 2 & uncolored \\
 & \\
 & \\
\hline
\end{tabularx}

enter image description here

The first case shows how the text should appear, but clearly the cell is fully colored. In the second case, I split the two rows into six rows so that one third of the original cell can be individually colored. But when adding the text, it is no longer center in the middle of the true cells because it is put in the subcells.

What is the best way to do this?

1
  • 1
    Welcome to TeX.SX! Can you please expand the code snippets that you have posted to a full minimal working example. It is much easier to help you if we can start with some compilable code that illustrates your problem. A MWE should start with a \documentclass command, include any necessary packages and be as small as possible to demonstrate your problem. At the moment we have to guess what packages etc you are using before we can compile your code.
    – user30471
    Nov 12, 2018 at 22:10

1 Answer 1

6

If you want to have a lot of control over shading of the cells then I recommend using a matrix of nodes from the tikz package --- see chapter 57.1 of the very comprehensive manual. For example, you can produce:

enter image description here

using the code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,calc}

\begin{document}

  \begin{tikzpicture}
    \matrix (M)[matrix of nodes]{
      Row 1 & 1/3 colored \\
      Row 2 & uncolored \\
      Row 3 & uncolored \\
      Row 4 & uncolored \\
      Row 5 & uncolored \\
      Row 6 & uncolored \\
     };
     % cell (1,2) with bluish background
     \draw[opacity=0.2,fill=blue!50](M-1-2.north west) rectangle (M-1-2.south east);
     % bottom third of cell (2,2) with bluish background
     \draw[opacity=0.2,fill=blue!50] ($ (M-2-2.north west)!0.67!(M-2-2.south west) $)
                            rectangle(M-2-2.south east);
     % triangular shading of cell (4,2)
     \draw[opacity=0.5, fill=green!50,draw=yellow, thick]
        (M-4-2.north west)--(M-4-2.south west)--(M-4-2.north east)--cycle;
    % crossing out cells in rows 5 and 6
    \draw[thick, red](M-5-1.north west)--(M-6-2.south east);
    \draw[thick, red](M-6-1.south west)--(M-5-2.north east);
  \end{tikzpicture}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .