# A custom table using TikZ instead of tabular

How could I create a table like below using TikZ? I tried using matrix, but could not get rid of few table lines like below.

It might be easier in tabular LaTeX, but I want in TikZ because I could then have embedded output in jupyter notebook using tikzmagic.

Update:
Here is my MWE not working

\documentclass[table]{article}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots, pgfplotstable}
\usetikzlibrary{matrix, positioning}
\pgfplotsset{compat=1.15}
\usepackage{xcolor}

\begin{document}

\begin{tikzpicture}
\tikzset{%
square matrix/.style={
matrix of nodes,
column sep=-\pgflinewidth, row sep=-\pgflinewidth,
nodes in empty cells,
nodes={draw,
minimum size=#1,
anchor=center,
align=center,
inner sep=0pt
},
},
square matrix/.default=0.9cm
}
\newcommand{\tx}{|[fill=cyan!20]|}
\newcommand{\tb}{|[fill=yellow!20]|}
\newcommand{\tg}{|[fill=gray!20]|}

\matrix[square matrix] (A)
{
$p(x,y)$    &       & 0     & 100   & 200 \\
x           & 100   & 0.20  & 0.10  & 0.20 \\
};

\node[below=0.50cm,align=center,text width=5cm] at (0,-3)
{
A sample joint pmf
};

\end{tikzpicture}

\end{document}


Output I get:

Update:
Thanks to @marmot, I am able to achieve and also draw two more on right side, however having a feeling that the table lines on new two tables on right are not exactly straight.

Updated MWE:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\begin{document}
\begin{tikzpicture}
\matrix (table) [matrix of math nodes,nodes in empty cells]
{\rho(x,y) & & 0 & 100 & 200\\
& 100 & .20 & .10 & .20\\
& 250 & .05 & .15 & .30\\
};
\draw ($(table-2-1)!0.5!(table-3-1)$) coordinate (aux)
(aux.center -| table-1-1.west) node[anchor=west]{$x$};
\node[above=1pt of table-1-4] {$y$};
\draw ($(table-2-2.east)!0.5!(table-2-3.west)$) coordinate (aux2)
(table.north -| aux2) -- (table.south -| aux2);
\draw ($(table-1-1.south)!0.5!(table-2-2.north)$) coordinate (aux3)
(table.west |- aux3) -- (table.east |- aux3);

\matrix (table_x) [%
right=1cm of table.north east, anchor=north west,
matrix of math nodes,nodes in empty cells
]
{x & 100 & 250 \\
\rho_X(x) & .5 & .5 \\
};
\draw ($(table_x-2-1.east)!0!(table_x-1-1.west)$) coordinate (aux4) (table_x.north -| aux4) -- (table_x.south -| aux4);
\draw ($(table_x-1-1.south)!0.5!(table_x-2-2.north)$) coordinate (aux5) (table_x.west |- aux5) -- (table_x.east |- aux5);

\matrix (table_y) [%
right=1cm of table_x.north east, anchor=north west,
matrix of math nodes,nodes in empty cells
]
{y & 0 & 100 & 200 \\
\rho_Y(y) & .25 & .25 & .5 \\
};
\draw ($(table_y-2-1.east)!0!(table_y-1-1.west)$) coordinate (aux6) (table_y.north -| aux6) -- (table_y.south -| aux6);
\draw ($(table_y-1-1.south)!0.5!(table_y-2-2.north)$) coordinate (aux7) (table_y.west |- aux7) -- (table_y.east |- aux7);

\end{tikzpicture}
\end{document}


Output:

I do not have any problems in producing such tables with TikZ.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\begin{document}
\begin{tikzpicture}
\matrix (table) [matrix of math nodes,nodes in empty cells]
{\rho(x,y) & & 0 & 100 & 200\\
& 100 & .20 & .10 & .20\\
& 250 & .05 & .15 & .30\\
};
\path ($(table-2-1)!0.5!(table-3-1)$) coordinate (aux)
(aux.center -| table-1-1.west) node[anchor=west]{$x$};
\node[above=1pt of table-1-4] {$y$};
\draw ($(table-2-2.east)!0.5!(table-2-3.west)$) coordinate (aux2)
(table.north -| aux2) -- (table.south -| aux2);
\draw ($(table-1-1.south)!0.5!(table-2-2.north)$) coordinate (aux3)
(table.west |- aux3) -- (table.east |- aux3);
\end{tikzpicture}
\end{document}


Or with left-aligned nodes.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\begin{document}
\begin{tikzpicture}[set width/.style={column #1/.style={text width=6mm}}]
\matrix (table) [matrix of math nodes,nodes in empty cells,
set width/.list={2,...,5}]
{\rho(x,y) & & 0 & 100 & 200\\
& 100 & .20 & .10 & .20\\
& 250 & .05 & .15 & .30\\
};
\path ($(table-2-1)!0.5!(table-3-1)$) coordinate (aux)
(aux.center -| table-1-1.west) node[anchor=west]{$x$};
\node[above=1pt of table-1-4] {$y$};
\draw ($(table-2-2.east)!0.5!(table-2-3.west)$) coordinate (aux2)
(table.north -| aux2) -- (table.south -| aux2);
\draw ($(table-1-1.south)!0.5!(table-2-2.north)$) coordinate (aux3)
(table.west |- aux3) -- (table.east |- aux3);
\end{tikzpicture}
\end{document}


EDIT: Answer to your updated question. The horizontal red line is just for illustration, i.e. you don't want to keep it in your document.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix,positioning,calc}
\begin{document}
\begin{tikzpicture}[every node/.append style={text depth=.25ex,text height=1.5ex}]
\matrix (table) [matrix of math nodes,nodes in empty cells]
{\rho(x,y) & & 0 & 100 & 200\\
& 100 & .20 & .10 & .20\\
& 250 & .05 & .15 & .30\\
};
\draw ($(table-2-1)!0.5!(table-3-1)$) coordinate (aux)
(aux.center -| table-1-1.west) node[anchor=west]{$x$};
\node[above=1pt of table-1-4] {$y$};
\draw ($(table-2-2.east)!0.5!(table-2-3.west)$) coordinate (aux2)
(table.north -| aux2) -- (table.south -| aux2);
\draw ($(table-1-1.south)!0.5!(table-2-2.north)$) coordinate (aux3)
(table.west |- aux3) -- (table.east |- aux3);

\matrix (table_x) [%
right=1cm of table.north east, anchor=north west,
matrix of math nodes,nodes in empty cells
]
{x & 100 & 250 \\
\rho_X(x) & .5 & .5 \\
};
\draw ($(table_x-2-1.east)!0!(table_x-1-1.west)$) coordinate (aux4)
(table_x.north -| aux4) -- (table_x.south -| aux4);
\draw ($(table_x-1-1.south)!0.5!(table_x-2-2.north)$)
coordinate (aux5) (table_x.west |- aux5) -- (table_x.east |- aux5);

\matrix (table_y) [%
right=1cm of table_x.north east, anchor=north west,
matrix of math nodes,nodes in empty cells
]
{y & 0 & 100 & 200 \\
\rho_Y(y) & .25 & .25 & .5 \\
};
\draw ($(table_y-2-1.east)!0!(table_y-1-1.west)$)
coordinate (aux6) (table_y.north -| aux6) -- (table_y.south -| aux6);
\draw ($(table_y-1-1.south)!0.5!(table_y-2-2.north)$)
coordinate (aux7) (table_y.west |- aux7) -- (table_y.east |- aux7);

\end{tikzpicture}
\begin{tikzpicture}[overlay,remember picture]
\draw[red] ([yshift=-5.4cm] current page.north west)
-- ([yshift=-5.4cm] current page.north east);
\end{tikzpicture}
\end{document}


• I just updated with my MWE marmot. – Parthiban Rajendran Nov 3 '18 at 15:35
• there might be few unwanted lines in my MWE, this is because I quickly extracted that part from source document to create a MWE, so might not have isolated all unwanted parts. But I could not figure out which part was adamantly drawing lines around table. It was straightforward and easier using tabular but I want to stick to tikz for better readability in my jupyter notebook. – Parthiban Rajendran Nov 3 '18 at 15:37
• what does this line ($(table-2-1)!0.5!(table-3-1)$) coordinate (aux) mean? its defining the aux coordinate? if so what is !0.5!? I infer table-2-1 as 2nd row, 1st column? – Parthiban Rajendran Nov 3 '18 at 15:48
• @PaariVendhan The lines come from draw, i.e. each node is in a box. Since $p(x,y)$  is wider than the minimum size of 9mm, there are these gaps. They will disappear if you increase the minimum width of the first column to become wider than $p(x,y)$ . And yes, ($(table-2-1)!0.5!(table-3-1)$) coordinate (aux) defines an aux coordinate that is used later. – marmot Nov 3 '18 at 16:08
• oh reg lines I asked why they appeared in my MWE, because there was no draw command in my MWE. reg $p(x,y)$, thats quite a trick. Min size 9mm is default? and i can add more tables side ways? i was going to use qquad to get something like this – Parthiban Rajendran Nov 3 '18 at 16:16

a minimalistic solution:

\documentclass[tikz, margin=3.141592mm]{standalone}  % :)
\usetikzlibrary{matrix}

\begin{document}
\begin{tikzpicture}
\matrix (m) [matrix of math nodes,
nodes in empty cells,
nodes={minimum height=3ex, minimum width=2em,
inner sep=0pt, anchor=center},
column 1/.style = {nodes={minimum width=3.3em}},
]
{
\rho(x,y)   &       & 0     & 100 & 200\\
& 100   & .20   & .10 & .20\\
& 250   & .05   & .15 & .30\\
};
\draw (m-1-1.south west) -- (m-1-5.south east);
\draw (m-1-2.north east) -- (m-3-2.south east);
\node[above] at (m-1-4.north) {$y$};
\node[right] at (m-2-1.south west) {$x$};
\end{tikzpicture}
\end{document}


• Unfortunately the vertical line is not really centered between the cells. That is, why don't you draw it with \draw (m-1-3.north west) -- (m-3-3.south west);, or why is the location dependent on this choice? This is why I am using a "less minimalistic" code with calc.... – marmot Nov 4 '18 at 0:02
• @marmot, nodes are tight aligned, so the lines are almost on the middle between nodes. small difference can arise due to column/row sep value. so you are (as usual) right :-) . this (tiny?) discrepancy is the cost for my "minimalistic" solution ;-). your solution is deep study (some times i use the similar approach in similar cases), so +1 ... – Zarko Nov 4 '18 at 3:34
• @marmot, off-topic: do you wake up so early or still not went to slip? here is 04:36 :-( – Zarko Nov 4 '18 at 3:37
• +1 also. Here it is 8:37 pm ... almost time to go to bed. – marmot Nov 4 '18 at 3:38