5

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
    Commented Nov 12, 2018 at 22:10

2 Answers 2

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}
0

The environment {NiceTabular} of nicematrix is similar to the classical environment {tabular} but creates PGF/TikZ nodes under the cells, rows and columns. Then, it's possible to use these nodes with TikZ to draw whatever you want before or after the construction of the main array.

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\renewcommand{\arraystretch}{3.0}
\begin{NiceTabularX}{10cm}{XX}[hvlines]
\CodeBefore
  \tikz \fill [blue!25] (1-|2) rectangle ($(1-|3)!0.3!(2-|3)$) ; 
\Body
Row 1 & 1/3 colored \\
Row 2 & uncolored \\
\end{NiceTabularX}

\end{document}

Output of the above code

It's also possible to write a command \Third which, when used within a cell, will color a part of that cell.

\documentclass{article}
\usepackage{xcolor}
\usepackage{nicematrix}
\usepackage{tikz}
\usetikzlibrary{calc}

\ExplSyntaxOn

\NewDocumentCommand{\Third}{}
  {
    \tl_gput_right:Nx \g_nicematrix_code_before_tl 
      { \__ganter:nn { \arabic{iRow} } { \arabic{jCol } } }
  }

\cs_new_protected:Nn \__ganter:nn 
  { 
    \tikz \fill [blue!25] 
       (#1-|#2) 
       rectangle 
       ($(#1-|\int_eval:n{#2+1})!0.3!(\int_eval:n{#1+1}-|\int_eval:n{#2+1})$) ; 
  }

\ExplSyntaxOff

\begin{document}

\renewcommand{\arraystretch}{3.0}
\begin{NiceTabularX}{10cm}{XX}[hvlines]
Row 1 & \Third 1/3 colored \\
\Third Row 2 & uncolored \\
\end{NiceTabularX}

\end{document}

Output of the second code

You must log in to answer this question.

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