# How can I draw this simple diagram? I unsuccesfully tried with table

Is there a way to precisely draw this simple diagram? Using tables the outcome is ugly and not as readable as it should be. Or maybe I'm not so good with tables :) Thanks in advance for your help! • "Or maybe I'm not so good with tables" glad you've said that. – CroCo Dec 28 '17 at 21:58
• – Cragfelt Dec 29 '17 at 9:09
• Is there any answer that solve your problem? – Cragfelt Jan 14 '18 at 17:21

\documentclass{article}
\begin{document}
$\begin{array}{ccccc} 1 & 2 & 3 & 4 & \\ \cline{1-4} \multicolumn{1}{|c|}{a} & \multicolumn{1}{|c|}{b} & \multicolumn{1}{|c|}{x} &\multicolumn{1}{|c|}{a} & 0\\ \cline{1-4} & \multicolumn{1}{|c|}{a} & \multicolumn{1}{|c|}{x} &\multicolumn{1}{|c|}{a} & 1\\ \cline{2-4} & & \multicolumn{1}{|c|}{x} &\multicolumn{1}{|c|}{b} & 2\\ \cline{3-4} & & &\multicolumn{1}{|c|}{x} & 3\\ \cline{4-4} \end{array}$
\end{document} A semantically cleaner solution with blkarray:

\documentclass{article}
\usepackage{blkarray}

\begin{document}

$\setlength{\BAextrarowheight}{2pt} \makeatletter\setlength{\BA@colsep}{5pt}\makeatother \begin{blockarray}{*{5}{c}} 1 & 2 & 3 & 4 \\ \cline{1-4}% \begin{block}{*{4}{|c}|c} a & b & x & a & 0 \\ \BAhhline{----|~} \end{block} \begin{block}{c*{3}{|c}|c} & a & x & a & 1\\ \BAhhline{~|---|~}% \end{block} \begin{block}{cc*{2}{|c}|c} & & x & b & 2 \\ \BAhhline{~~|--|~}% \end{block} \begin{block}{*{3}{c}|c|c} & & & x & 3 \\ \BAhhline{~~~|-|~}% \end{block} \end{blockarray}$

\end{document} You can use only \hbox, \vbox, \hrule and \vrule primitives:

\newdimen\bxwidth  \bxwidth=1.2em

\def\p #1 #2 #3 #4 {\par\hbox{\bp{#1}\bp{#2}\bp{#3}\bp{#4}}}
\def\r #1 {\smallskip\hrule width\dimexpr 4\bxwidth+.4pt\relax}
\def\x #1 #2 #3 #4 #5 {\hbox{\bx{#1}\bx{#2}\bx{#3}\bx{#4}\vrule\bp{#5}}\nointerlineskip}
\def\bp#1{\hbox to\bxwidth{\hss#1\hss}}%
\def\bx#1{\ifx.#1\bp{}%
\else \lower\dimexpr.2\bxwidth+.4pt\vbox{
\bp{\unskip\vrule height.8\bxwidth depth.2\bxwidth\hfil $#1$}\hrule}%
\fi
}

\p 1 2 3 4
\r -------
\x a b x a 0
\x . a x a 1
\x . . x b 2
\x . . . x 3

\bye


You can use ytableau:

\documentclass{article}
\usepackage{ytableau}

\begin{document}

\begin{ytableau}
\none & \none & \none & \none \\
a        & b        & x        & a        & \none \\
\none    & a        & x        & a        & \none \\
\none    & \none    & x        & b        & \none \\
\none    & \none    & \none    & x        & \none
\end{ytableau}

\end{document} • (+1) For showing us that there is always a simpler way to do the things (and for Sportmanship). – Cragfelt Dec 31 '17 at 8:33

## UPDATE

This is a version using a matrix of TikZ, with perfect squared boxes. \documentclass[border=4mm,tikz]{standalone}
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}

\tikzset{square matrix/.style = {
matrix of math nodes,
row sep = -\pgflinewidth,
column sep = -\pgflinewidth,
nodes = {draw,
minimum height = 2em,
text width = 2em,
anchor = center,
align = center,
inner sep = 0pt
},
},
square matrix/.default = 1cm
}
\matrix (m) [square matrix]
{
&   &   &   &            \\
a & b & x & a & |[white]|. \\
& a & x & a & |[white]|. \\
&   & x & b & |[white]|. \\
&   &   & b & |[white]|. \\
};
\foreach \i in {2,...,5}
{
\pgfmathtruncatemacro{\y}{\i - 2}
\node at (m-\i-5) {$\y$};
\pgfmathtruncatemacro{\x}{\i - 1}
\node[above, inner sep = 3mm] at (m-2-\x.north) {$\x$};
}
\draw (m-2-5.north west)--(m-5-5.south west);

\end{tikzpicture}
\end{document}


And just for fun... here is an odd solution with skyline puzzle of LogicPuzzle package. \documentclass[border=4mm]{standalone}
\usepackage{logicpuzzle}

\begin{document}

\skylinesetup{rows=4, columns=4}
\begin{skyline}
\skylineT{1,2,3,4}
\skylineR{3,2,1,0}
\setrow{4}{$a$,$b$,$x$,$a$}
\setrow{3}{{},$a$,$x$,$a$}
\setrow{2}{{},{},$x$,$b$}
\setrow{1}{{},{},{},$x$}
\begin{puzzlebackground}
\fillarea{blue!20}{%
(4,1)--(5,1)--(5,5)--(1,5)--(1,4)--(2,4)--(2,3)--(3,3)--(3,2)--(4,2)--(4,1)}
%\fillarea{white}{(1,1)--(4,1)--(4,2)--(3,2)--(3,3)--(2,3)--(2,4)--(1,4)--(1,1)}
\end{puzzlebackground}
\end{skyline}

\end{document}


## Notes

1. The package only works with rectangular grids, so there is no possibility to make the exact figure as the question requires to in this case.

2. The commented \fillarea{white}{...} line is for coloring and enframe the other part of the grid. Colors can be changed at will.

• I have to say, my perfect squared boxes was corrected and pointed out by CarLaTeX with minimum height = 2em, text width = 2em, inner sep = 0ptinstructions. She deleted the comments. – Cragfelt Dec 31 '17 at 9:19

A tabular solution:

\documentclass{article}

\def\multl#1{\multicolumn{1}{|c}{#1}}
\def\multc#1{\multicolumn{1}{|c}{#1}}
\def\multr#1{\multicolumn{1}{c|}{#1}}
\def\multcr#1{\multicolumn{1}{|c|}{#1}}
\begin{document}

\begin{tabular}{*{5}{c}}
1&2&3&4&\\\cline{1-4}
\multl{a}&\multc{b}&\multc{x}&\multcr{a}&0\\\cline{1-4}
&&\multl{x}&\multcr{b}&1\\\cline{3-4}
&&&\multcr{x}&2\\\cline{4-4}
&&&x&3
\end{tabular}

\end{document} (PS: The line missing before "a" is fixed in the code )