Tx · LaTeX & TikZ

How to Make a Colored Diagonal Table Cell in LaTeX

Updated 13 October 2026

At a glance

  1. 1. Choose cell dimensions

    Set the width and height of a TikZ picture to match the header cell you need.

  2. 2. Fill both triangles

    Draw two triangular paths that share the diagonal, and give each path its own fill color.

  3. 3. Draw the lines

    Add the rectangle border and the diagonal after the fills so the edges remain visible.

  4. 4. Position the labels

    Anchor one label in each triangle, then place the picture in a table cell.

A colored diagonal table cell is usually made by drawing a small graphic inside the cell rather than trying to color part of a normal table cell. TikZ lets you fill the two triangular regions independently, draw their shared diagonal, and place a label in each region.

The example below creates a two-color header cell with “Row” in the upper-left triangle and “Year” in the lower-right triangle. You can change the labels, colors, and dimensions for your own table.

How the diagonal cell is built

The picture is a rectangle with corners at (0,0)(0,0) and (2,1)(2,1), so its dimensions are 2 cm by 1 cm. A diagonal from the lower-left corner to the upper-right corner divides it into two triangles. Each triangle is a separate filled path; drawing the outline and diagonal afterward keeps the lines crisp.

The baseline option aligns the center of the TikZ picture with the surrounding text baseline. That helps the graphic sit neatly within the table row. The label nodes use anchors so their text stays near opposite corners without requiring manual spacing around each word.

Complete compilable example

This document uses the xcolor and tikz packages. It includes a small table to show how the macro works in context.

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}

\newcommand{\coloredcell}[2]{%
  \tikz[baseline=(current bounding box.center)]{%
    \path[fill=blue!20] (0,0) -- (2,0) -- (2,1) -- cycle;
    \path[fill=orange!25] (0,0) -- (0,1) -- (2,1) -- cycle;
    \draw (0,0) rectangle (2,1);
    \draw (0,0) -- (2,1);
    \node[anchor=north west, inner sep=2pt, font=\scriptsize]
      at (0.03,0.97) {#1};
    \node[anchor=south east, inner sep=2pt, font=\scriptsize]
      at (1.97,0.03) {#2};
  }%
}

\begin{document}

\begin{tabular}{|c|c|c|}
  \hline
  \multicolumn{1}{|@{}c@{}|}{\coloredcell{Row}{Year}}
    & 2023 & 2024 \\
  \hline
  North & 12 & 15 \\
  \hline
  South & 9 & 11 \\
  \hline
\end{tabular}

\end{document}

The first macro argument goes in the upper-left triangle; the second goes in the lower-right triangle. The \multicolumn specification removes the usual horizontal padding around that graphic, so its border sits next to the table’s vertical rules. If you prefer a little inset around the graphic, use an ordinary table cell instead.

Adjusting the appearance

Change the coordinates in the fill paths, border, and nodes together to resize the cell. For instance, changing every right-hand x-coordinate from 2 to 2.5 makes the picture wider; changing the top y-coordinate from 1 to 1.2 makes it taller. Keep the label coordinates just inside the new corners, or a label may extend beyond the colored area.

The color expressions blue!20 and orange!25 mean 20% blue and 25% orange mixed with white. Replace them with other named colors or mixtures such as green!18 and red!30. To reverse which region is filled by which color, swap the fill options on the two paths.

For longer labels, reduce \scriptsize to \tiny, shorten the wording, or enlarge the graphic. You can put math in a label by including math delimiters in the argument, for example \coloredcell{$i$}{Year}. A fixed-size graphic does not automatically grow to fit its text.

Common mistakes

  • Filling after drawing the lines: a later fill can cover the diagonal or border. Draw both filled paths first, then draw the lines.
  • Using the wrong triangle corners: the upper-left triangle is (0,0) -- (0,1) -- (2,1), while the lower-right one is (0,0) -- (2,0) -- (2,1). Each path ends with cycle to close its shape.
  • Forgetting table padding: standard c columns add horizontal padding, which can leave a gap between the graphic and the table rules. The example removes that padding just for the header using \multicolumn and @{}.
  • Expecting automatic resizing: the picture’s coordinates set its dimensions. Changing only the rectangle but not the fills or node positions can leave gaps or misplaced labels.

When a simpler package is enough

If you need a diagonal header but do not need separately colored triangles, the diagbox package provides diagonal box commands intended for table headers. For custom fills, label placement, or other shapes, TikZ gives you more control.

References

  1. The TikZ and PGF Packages: Manual
  2. The xcolor Package

Spotted an error? Send a correction. Read how we write and check articles in our editorial policy.