[Tex/LaTex] A matrix with vertical, horizontal and diagonal lines matrices How could I draw with LaTeX such a matrix? I've looked around but didn't find any solution. Best Answer 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} Related SolutionsTikZ – Horizontal Row Separation Line in TikZ Matrix You can use a style that uses execute at end cell to draw a horizontal line at the top or bottom edge of selected cells. By applying this style to complete using row <number>/.style, you can get an effect similar to \hline: \documentclass{article} \usepackage{tikz} \usetikzlibrary{matrix} \begin{document} \tikzset{toprule/.style={% execute at end cell={% \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north east);% } }, bottomrule/.style={% execute at end cell={% \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east);% } } } \begin{tikzpicture} \matrix [matrix of nodes, row sep=-\pgflinewidth, column sep=-\pgflinewidth, nodes={rectangle,minimum width=3em,outer sep=0pt}, row 1/.style={toprule=thick,bottomrule}, row 3/.style={bottomrule=thick}] { 0 & 6 & 5\\ 1 & 3 & 7\\ 21 & 22 & 23\\ }; \end{tikzpicture} \end{document} Matrices – How to Typeset a Matrix with Crossing Arrows in LaTeX I once worked out a similar thing with pgf/tikZ (surprise, surprise!) as a solution to a question where else. \documentclass{minimal} \usepackage{tikz} \usetikzlibrary{calc,matrix} \begin{document} \begin{tikzpicture}[>=stealth] \matrix [% matrix of math nodes, column sep=1em, row sep=1em ] (sarrus) {% a_{11} & a_{12} & a_{13} & a_{11} & a_{12} \\ a_{21} & a_{22} & a_{23} & a_{21} & a_{22} \\ a_{31} & a_{32} & a_{33} & a_{31} & a_{32} \\ }; \path ($(sarrus-1-1.north west)-(0.5em,0)$) edge ($(sarrus-3-1.south west)-(0.5em,0)$) ($(sarrus-1-3.north east)+(0.5em,0)$) edge ($(sarrus-3-3.south east)+(0.5em,0)$) (sarrus-1-1) edge (sarrus-2-2) (sarrus-2-2) edge[->] (sarrus-3-3) (sarrus-1-2) edge (sarrus-2-3) (sarrus-2-3) edge[->] (sarrus-3-4) (sarrus-1-3) edge (sarrus-2-4) (sarrus-2-4) edge[->] (sarrus-3-5) (sarrus-3-1) edge[dashed] (sarrus-2-2) (sarrus-2-2) edge[->,dashed] (sarrus-1-3) (sarrus-3-2) edge[dashed] (sarrus-2-3) (sarrus-2-3) edge[->,dashed] (sarrus-1-4) (sarrus-3-3) edge[dashed] (sarrus-2-4) (sarrus-2-4) edge[->,dashed] (sarrus-1-5); \foreach \c in {1,2,3} {\node[anchor=south] at (sarrus-1-\c.north) {$+$};}; \foreach \c in {1,2,3} {\node[anchor=north] at (sarrus-3-\c.south) {$-$};}; \end{tikzpicture} \end{document} If you need help with further adaptations, feel free to ask. Related Question[Tex/LaTex] Matrix with arrows and labelsDiagonal Line in Matrices – How to Draw and CustomizeTikZ – Drawing Vertical Lines in MatrixTikZ-PGF – Adding Colored Dashed Lines Inside Matrix with External Braces
Best Answer
Doing the matrix with
amsmath
'spmatrix
and the lines withtikz
andtikzmark
:Or, greatly simplified by Mr. marmot: