# A matrix with vertical, horizontal and diagonal lines

How could I draw with LaTeX such a matrix?

I've looked around but didn't find any solution.

Doing the matrix with amsmath's pmatrix and the lines with tikz and tikzmark:

\documentclass{article}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{tikzmark,calc}
\newdimen\numht
\newdimen\numwd

\begin{document}
\pagenumbering{gobble}
\settowidth{\numwd}{0}
\settoheight{\numht}{(}

\begin{equation*}
\begin{pmatrix}
1\tikzmark{a} & & & \tikzmark{b}1\\
0\tikzmark{c} & \\
\\
0\tikzmark{d} & & \tikzmark{e}0 & \tikzmark{f}1\\
\end{pmatrix}
\end{equation*}

\begin{tikzpicture}[remember picture, overlay]
\draw ($(pic cs:a)+(0,0.5\numht)$) -- ($(pic cs:b)+(0,0.5\numht)$);
\draw ($(pic cs:a)+(0,0.0\numht)$) -- ($(pic cs:f)+(0,1 \numht)$);
\draw ($(pic cs:c)+(0,0.0\numht)$) -- ($(pic cs:e)+(0,1 \numht)$);
\draw ($(pic cs:c)+(-0.5\numwd,-1pt)$) -- ($(pic cs:d)+(-0.5\numwd,\numht)$);
\draw ($(pic cs:d)+(0,0.5\numht)$) -- ($(pic cs:e)+(0,0.5\numht)$);
\draw ($(pic cs:b)+(0.5\numwd,-1pt)$) -- ($(pic cs:f)+(0.5\numwd,\numht)$);
\end{tikzpicture}

\end{document}


Or, greatly simplified by Mr. marmot:

\documentclass{article}
\usepackage{amsmath}
\usepackage{tikz}
\newcommand{\tikznode}[2]{%
\ifmmode%
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {$#2$};%
\else
\tikz[remember picture,baseline=(#1.base),inner sep=0pt] \node (#1) {#2};%
\fi}
\begin{document}

\begin{equation*}
\begin{pmatrix}
\tikznode{a}{1} & & & \tikznode{b}{1}\\
\tikznode{c}{0} & \\
\\
\tikznode{d}{1} & & \tikznode{e}{0} & \tikznode{f}{1}\\
\end{pmatrix}
\end{equation*}

\begin{tikzpicture}[remember picture, overlay,shorten >=1pt,shorten <=1pt]
\draw (a) -- (b);
\draw (c) -- (e);
\draw (c) -- (d);
\draw (d) -- (e);
\draw (a) -- (f);
\draw (b) -- (f);
\end{tikzpicture}

\end{document}

• @marmot Very neat! Just changed it a little bit ;) – Phelype Oleinik Mar 28 '18 at 0:17
• It's not that great, the distances of the vertical lines are much better in your original solution. (But I think that shorten at the relevant places can cure this, I am just doing this now.) – marmot Mar 28 '18 at 0:19

Alternative to Phelype's nice answer with a bit less labeling and computations: draw the whole thing in TikZ.

\documentclass{article}
\usepackage{amsmath,tikz}
\usetikzlibrary{matrix}
\begin{document}

$\begin{tikzpicture}[% from https://tex.stackexchange.com/a/330411/121799 every left delimiter/.style={xshift=.75em}, every right delimiter/.style={xshift=-.75em}, ] \matrix[ matrix of math nodes, left delimiter=(, right delimiter=), nodes in empty cells ] (m) { 1 & ~~~ & & 1 \\ 0 & & & \\ & & & \\ 1 & & 0& 1\\ }; \draw (m-1-1) -- (m-1-4); \draw (m-1-1) -- (m-4-4); \draw (m-2-1) -- (m-4-1); \draw (m-2-1.-20) -- (m-4-3); \draw (m-4-1) -- (m-4-3); \draw (m-1-4) -- (m-4-4); \end{tikzpicture}$

\end{document}


• That's what computers are for: computations! (+1 :) – Phelype Oleinik Mar 27 '18 at 23:53

Another simple solution, with pstricks:

\documentclass{article}

\usepackage{amsmath}
\usepackage{pst-node}
\usepackage{auto-pst-pdf} % to compile with pdflatex --enable-write18 (MiKTeX) or pdflatex -shell-escape (TeX Live, MacTeX)

\begin{document}

\begin{equation*}
\begin{pspicture}
\begin{pmatrix}
\Rnode{A}{1} & & & \Rnode{B}{1}\\
\Rnode{C}{0} & \\
\\
\Rnode{D}{0}& & \Rnode{E}{0} & \Rnode{F}{1}\\
\end{pmatrix}
\foreach \S/\T in {A/B, A/F, B/F, C/D, C/E, D/E}{\ncline[linewidth=0.6pt, nodesep=1.5pt]{\S}{\T}}
\end{pspicture}
\end{equation*}

\end{document}


A solution with just tikz (no kind of matrix)... I just adding the solution to show some nice ideas about scaling tikzpictures and taking in account the font:

Play with the value of \mscale to see what I mean:

\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{calc}

\def\bo{$\boldsymbol{1}$}
\def\bz{$\boldsymbol{0}$}

\begin{document}
\section{Test}
Text here

\xdef\mscale{2.2}% \mscale \in [0.4-2.3] %PLAY WITH THE VALUES IN THIS AREA
\begin{tikzpicture}[scale =\mscale]
\pgfmathsetmacro\mysize{int(\mscale * 3.3)}
\pgfmathsetmacro\fixedpar{4.6*\mysize+1.6*\mysize*(0.8-\mscale)+.8*(\mysize*(1.3-\mscale)^3}
\foreach \mfont[count=\i from 1] in {\tiny,\footnotesize,\small,\normalsize,\large,\Large,\LARGE,\huge,\Huge,\HUGE}{\ifnum \mysize< \i \xdef\accsize{\expandafter\noexpand\mfont}\breakforeach\else\xdef\accsize{\expandafter\noexpand\mfont}\fi}
\tikzstyle{every node}=[font=\accsize]
\node (A)at (0,0) {\bo};
\node (B)at (0,-1) {\bz};
\node (C)at (3,0) {\bo};
\node (D)at (0,-3) {\bo};
\node (E)at (2,-3) {\bz};
\node (F)at (3,-3){\bo};
\draw[-] (A)--(F);
\draw[-] (C)--(F);
\draw[-] (B)--(D);
\draw[-] (B)--(E);
\draw[-] (A)--(C);
\draw[-] (D)--(E);
\node[yscale={\fixedpar},xscale={0.3*\fixedpar}] at (-0.5,-1.5){$($};
\node[yscale={\fixedpar},xscale={0.3*\fixedpar}] at (3.5,-1.5){$)$};
\end{tikzpicture}

\end{document}


Some results with changing just the scale:

mscale=0.4

mscale=0.8

mscale=1.8

Not really perfectly tuned, but it gives general idea. (quadratic function is a must for the corrections because of the non-linear increase of fontsizes)