5

How could I draw with LaTeX such a matrix?

enter image description here

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

0

5 Answers 5

12

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

enter image description here

\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}
2
  • @marmot Very neat! Just changed it a little bit ;) Mar 28, 2018 at 0:17
  • 1
    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.)
    – user121799
    Mar 28, 2018 at 0:19
9

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}

enter image description here

1
  • 1
    That's what computers are for: computations! (+1 :) Mar 27, 2018 at 23:53
7

Here is a solution with {pNiceMatrix} of nicematrix. You switch very easily between the (continuous) dotted lines and the solid lines.

\documentclass{article}
\usepackage{nicematrix}
\usepackage{tikz}

\begin{document}
\[
\begin{pNiceMatrix}
 1      & \Cdots &   & 1      \\
 0      & \Ddots &   & \Vdots \\
 \Vdots & \Ddots \\
 0      & \Cdots & 0 & 1  \\
\end{pNiceMatrix}
\hspace{1cm}
\begin{pNiceMatrix}[xdots/line-style=solid]
 1      & \Cdots &   & 1      \\
 0      & \Ddots &   & \Vdots \\
 \Vdots & \Ddots \\
 0      & \Cdots & 0 & 1  \\
\end{pNiceMatrix}
\]
\end{document} 

You need several compilations (because nicematrix uses PGF/Tikz under the hood).

Output of the above code

1
  • Very nice solution, much easier than the other ones in my humble opinion !
    – Laurent90
    Nov 27, 2021 at 13:12
6

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} 

enter image description here

5

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

enter image description here

mscale=0.8

enter image description here

mscale=1.8

enter image description here

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)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .