# Diagonal lines in table cell

I need this table:

(each row and column should have the same height and length, last cell should be divided by diagonal line). I tried the following code:

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{array}
\usepackage{makecell}
\newcolumntype{x}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\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&\diaghead(-3,2){\hskip \hsize}{$a_i$}{$b_j$}\\ \hline
\end{tabular}
\end{document}


but text in the last cell is displayed incorrectly and cells are not the same.

How can I change that?

-
Use slashbox instead. – Leo Liu May 8 '11 at 15:18
possible duplicate of Tables of numbers – Leo Liu May 8 '11 at 15:18
I tried 'slashbox' but it doesn't give a good result. – LiN May 8 '11 at 15:31
@leoliu: are you the same person as the author of diagbox, which seems to me to do the job noticeably better than slashbox? i know it's not "sexy" any more to be using pict2e graphics, but it does do this job pretty well... – wasteofspace Nov 21 '12 at 9:48
@LeoLiu Based on that comment, perhaps we could have an answer using diagbox (for completeness)? – Joseph Wright Jan 7 '13 at 16:19

Exact solution with TikZ:

\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.north west) -- (box.south 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}


Also it is a reimplementation of \diaghead.

-
The code is similar to slashbox and makecell. For a general macro \diag, the code \multicolumn{1}{p{#2}|} may be changed. – Leo Liu May 8 '11 at 16:38
it would be great if you made that into a package; this is a common requirement, and it would be nice to do the job in "some other" way – wasteofspace Nov 21 '12 at 9:45
There is a question discussing the issues all solutions that aren't all-TikZ seem to have tex.stackexchange.com/questions/89745/… Your solution was also highlighted so maybe you want to join :) – Christian Jan 8 '13 at 10:55

It might be possible to draw a diagonal line which fits exactly in a table cell, but it might be easier to draw the whole table as a picture.

Here my attempt using tikz. For large tables the need for nodes for each cell might be quite an effort, but it should be OK for smaller ones.

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[x=.75cm,y=.5cm]
\draw (0,0) grid [step=1] (5,4);
\node at (0.5,0.5) {15};
\node at (1.5,0.5) {12};
\node at (2.5,0.5) {18};
\node at (3.5,0.5) {50};
\node at (4.5,3.5) {20};
\node at (4.5,2.5) {30};
\node at (4.5,1.5) {45};
\draw (4,1) -- (5,0);
\node at (5.0,1.0) [below left,inner sep=1pt] {\small$a_i$};
\node at (4.0,0.0) [above right,inner sep=1pt] {\small$b_j$};
\end{tikzpicture}
\end{document}


-

Here's a solution using TikZ:

\documentclass[11pt]{article}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}%[thick]
\matrix (mat) [%
matrix of nodes,
nodes in empty cells,
text width=0.8cm,
text height=10pt,
text depth=2pt,
]
{%
& & & & 20 \\
& & & & 30  \\
& & & & 45  \\
15 & 12 & 18 & 50 & \raisebox{5pt}{$a_i$}\hspace{-15pt}\llap{\raisebox{-1pt}{$b_j$}}\\
};
% horizontal lines
\foreach \i in {1,2,3,4}
\draw (mat-\i-1.north west) -- (mat-\i-5.north east);
\draw (mat-4-1.south west) -- (mat-4-5.south east);
% vertical lines
\foreach \j in {1,2,3,4,5}
\draw (mat-1-\j.north west) -- (mat-4-\j.south west);
\draw (mat-1-5.north east) -- (mat-4-5.south east);
% diagonal line
\draw (mat-4-5.north west) -- (mat-4-5.south east);
\end{tikzpicture}

\end{document}


EDIT: in the solution I initially gave, the last cell contents was not what the OP wanted. I corrected it.

-

Here's another solution using Tikz and based on a given solution by Heiko Oberdiek.

\documentclass{article}
\pagestyle{empty}% for cropping
\usepackage{array}
\usepackage{makecell}
\newcolumntype{x}[1]{>{\centering\arraybackslash}p{#1}}

\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{zref-savepos}

\newcounter{DiagonalizedEntry}
\renewcommand*{\theDiagonalizedEntry}{NTE-\the\value{DiagonalizedEntry}}

\newcommand*{\diagonalize}[2]{%
\multicolumn{1}{@{}c@{}|}{%
\stepcounter{DiagonalizedEntry}%
\zsavepos{\theDiagonalizedEntry l}% left
\hspace{0pt plus 1filll}%
\zsavepos{\theDiagonalizedEntry r}% right
\tikz[overlay]{%
\draw[red]
let
\n{llx}={\zposx{\theDiagonalizedEntry l}sp-\zposx{\theDiagonalizedEntry r}sp}, % x left
\n{urx}={0}, % x right
\n{lly}={\zposy{\theDiagonalizedEntry b}sp-\zposy{\theDiagonalizedEntry r}sp}, % y bottom
\n{ury}={\zposy{\theDiagonalizedEntry t}sp-\zposy{\theDiagonalizedEntry r}sp} %  y top
in
(\n{llx}, \n{ury}) -- (\n{urx}, \n{lly})
node[anchor=south west] at (\n{llx}, \n{lly}) {#1}
node[anchor=north east] at (\n{urx}, \n{ury}) {#2}
;
}%
}%
}

\begin{document}
\renewcommand{\arraystretch}{2}
\renewcommand\tabcolsep{10pt}
\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&\diagonalize{$a_i$}{$b_j$} \\ \hline
\end{tabular}
\end{document}


-