How to connect entries of a matrix with lines?

I am looking for a way, to draw lines in a given matrix, that connect two chosen entries. So, for example I would like to use

\begin{pmatrix} 1 & 2 \\ 3 & 4\end{pmatrix}


to initiate the 2x2-matrix with entries 1,2,3,4 and draw a line from 1 to 4 and a line from 3 to 2 within or rather over this matrix.

I would also be interested in drawing such lines in a matrix, whose entries simply are dots.

Thanks a lot!

2 Answers

Here is an example of what is easy to do with pstricks : define as nodes the elements in the matrix you want to link, then connect them by a node connection:

\documentclass[11pt]{article}
\usepackage{mathtools} %
\usepackage{pst-node}
\usepackage{auto-pst-pdf} % To compile with pdflatex --enablewrite18 (MiKTeX) or pdflatex --shell-escape (TeX Live, MacTeX)

\begin{document}

$\begin{pspicture} \begin{pmatrix} \Rnode{a}{1} & \Rnode{b}{2} \\ \Rnode{c}{3} & \Rnode{d}{4} \end{pmatrix} \psset{linewidth = 0.5pt, nodesep = 0.5pt} \ncline{b}{c}\ncline[border = 0.5pt]{a}{d} \end{pspicture}$

\end{document}


\documentclass{article}
\usepackage{tikz}
\usepackage{amsmath}
\newcommand\tikznode[2]{\tikz[remember picture,baseline=(#1.base)]{\node(#1)[inner sep=0pt]{#2};}}
\begin{document}
$M= \begin{pmatrix} \tikznode{1}{1} & \tikznode{2}{2} \\ \tikznode{3}{3} & \tikznode{4}{4}\end{pmatrix}$
\tikz[overlay,remember picture]{\draw[-](1)--(4);\draw[dotted,thick](2)--(3);}
\end{document}