[Tex/LaTex] Cross out arbitrary table cells with diagonal line

tables

I'm looking for a way to use a diagonal line to cross out a table cell which may or may not be empty, and without having to manually specify the cell's width and height. (It would also be great if the solution were compatible with tabu.)

That is, I want to find a definition of \strike such that the following code produces the following output:

\documentclass{article}

\usepackage{tabu}
\newcommand{\strike}[1]{#1} % Some LaTeX wizardry here

\begin{document}

\renewcommand{\arraystretch}{2}
\begin{tabu}{|[3pt]c|c|c|[3pt]}
\tabucline[3pt]{-}
foo & bar & baz \\
\hline
\strike{quux} & A & B \\
\hline
C & D & \strike{$\delta$} \\
\tabucline[3pt]{-}
\end{tabu}

\end{document}

Output showing crossed-out table cells

(I've found several previous Stack Exchange questions on diagonal lines through cells, but they all assume that the cell is of known dimensions, or that the cell is empty, or that the intention is to split the cell into two triangular cells with separate content. The "Strike out a table cell" question is probably closest to what I want, except that it's about striking out blank cells; it's not obvious to me how to adapt the solutions to striking out cells which already have content.)

Best Answer

Adapting from the answer you have linked I defined \strike. The command takes two arguments, which is due to the varying rules in your table.

\strike{<column spec>}{<content>}

where <column spec> is the column format expected by \multicolumn, e.g. |[3pt]c|. You may specify columns different from c, but this will have no effect due to \hspace{0pt plus 1filll} on both sides of the content.

\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{zref-savepos}
\usepackage{tabu}

\newcounter{NoTableEntry}
\renewcommand*{\theNoTableEntry}{NTE-\the\value{NoTableEntry}}

\newcommand*{\strike}[2]{%
  \multicolumn{1}{#1}{%
    \stepcounter{NoTableEntry}%
    \vadjust pre{\zsavepos{\theNoTableEntry t}}% top
    \vadjust{\zsavepos{\theNoTableEntry b}}% bottom
    \zsavepos{\theNoTableEntry l}% left
    \hspace{0pt plus 1filll}%
    #2% content
    \hspace{0pt plus 1filll}%
    \zsavepos{\theNoTableEntry r}% right
    \tikz[overlay]{%
      \draw
        let
          \n{llx}={\zposx{\theNoTableEntry l}sp-\zposx{\theNoTableEntry r}sp-\tabcolsep},
          \n{urx}={\tabcolsep},
          \n{lly}={\zposy{\theNoTableEntry b}sp-\zposy{\theNoTableEntry r}sp},
          \n{ury}={\zposy{\theNoTableEntry t}sp-\zposy{\theNoTableEntry r}sp}
        in
        (\n{llx}, \n{lly}) -- (\n{urx}, \n{ury})
      ;
    }% 
  }%
}

\begin{document}

\renewcommand{\arraystretch}{2}
\begin{tabu}{|[3pt]c|c|c|[3pt]}
  \tabucline[3pt]{-}
  foo & bar & baz \\
  \hline
  \strike{|[3pt]c|}{quux} & A & B \\
  \hline
  C & D & \strike{c|[3pt]}{$\delta$} \\
  \tabucline[3pt]{-}
\end{tabu}
\end{document}

enter image description here

Related Question