7

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.)

  • Is it OK if we suggest a tikz alternative? – user11232 Jan 16 '15 at 10:38
  • Sure, I don't care whether or not the solution uses tikz. – Psychonaut Jan 16 '15 at 10:48
7

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

  • This is very nice! What does \vadjust pre do? What is the pre? – cfr Oct 29 '15 at 18:06
  • 1
    @cfr It is a pdfTeX extension. From the manual: “The \vadjust implementation of pdfTeX adds an optional qualifier ⟨pre spec⟩ (which is the string pre) to the original TeX primitive with the same name. As long as there is no pre given, \vadjust behaves exactly as the original (see the TeXbook, p. 281); it appends an adjustment item created from ⟨vertical mode material⟩ to the current list after the line in which \vadjust appears. However with the qualifier pre , the adjustment item is put before the line in which \vadjust pre appears.” – Henri Menke Oct 30 '15 at 7:46
  • Thank you very much! I was not sure where to find information about the pre as I didn't realise it was a pdfTeX enhancement, so I really appreciate your taking the trouble to comment. Your code is proving most useful, but I like to understand what I'm doing ;). – cfr Oct 30 '15 at 12:44
  • @cfr Thanks for the compliment, but actually this is not my code. I just adapted it from a solution by Heiko to a question of mine. – Henri Menke Oct 30 '15 at 13:21
  • @HenriMenke is it possible to color a cell that is crossed out with your code? I tried it with \cellcolor an \rowcolor, but the cell which is crossed out stays always half white... – Katharina May 24 '17 at 12:20
-1

Modifying this example

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{makecell}
\newcolumntype{x}[1]{>{\centering\arraybackslash}p{#1}}

\usepackage{tikz}
\newcommand\diag[4]{%
  \multicolumn{1}{p{#2}|}{\hskip-\tabcolsep
  $\vcenter{\begin{tikzpicture}[baseline=0,anchor=south west,inner sep=#1]
  \path[use as bounding box] (0,0) rectangle (#2+2\tabcolsep,\baselineskip);
  \node[minimum width={#2+2\tabcolsep},minimum height=\baselineskip+\extrarowheight] (box) {};
  \draw (box.south west) -- (box.north east);
  \node[anchor=south west] at (box.south west) {#3};
  \node[anchor=north east] at (box.north east) {#4};
 \end{tikzpicture}}$\hskip-\tabcolsep}}

\begin{document}
\setlength{\extrarowheight}{0.1cm}
\begin{tabular}{|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|x{0.5cm}|}\hline
&&&&20\\ \hline
&&&&30\\ \hline
&&&&45\\ \hline
15&12&18&50&\diag{.1em}{.5cm}{$a_i$}{$b_j$}\\ \hline
\end{tabular}
\end{document}

enter image description here

  • 1
    This doesn't appear to answer my question. This code uses a fixed cell size, and splits cell into two areas of separate content. – Psychonaut Jan 16 '15 at 14:48

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.