# Drawing dashed border matrix with tikz

I need to draw a matrix such that there is dashed line between each column and row. This is my code just to start, but the result is not good. As you see the second horizontal line is not direct and the vertical line does not go to end

\begin{tikzpicture}
\matrix (mymatrix) [matrix of math nodes,left delimiter={(},right
delimiter={)}]
{ A(1)  &  B_{1,2} & B_{1,3}&  \cdots  &  B_{1,r-1} & B_{1,r} \\
B_{1,2}^T  &  A(2)  &  0 & \cdots &  0  &  0 \\
B_{1,3}^T &  0  &  A(3)  &  \cdots  &  0 & 0\\
\vdots & 0 &  0  &   \ddots  &  0  &  0\\
B_{1,r-1}^T  &  0  &  0  &  0  &  0  &  0\\
B_{1,r}^T  &  0  &  0  &  0  &  0  &  0\\
};
\draw[black,dashed] (mymatrix-1-1.south west) -- (mymatrix-1-6.south east);
\draw[black,dashed] (mymatrix-2-1.south west) -- (mymatrix-2-6.south east);
\draw[black,dashed] (mymatrix-1-4.north east) -- (mymatrix-6-6.north east -| mymatrix-1-4.south east);
\end{tikzpicture}


An MWE starts with \documentclass and ends with \end{document}, like this one:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{matrix,calc}
\begin{document}
\begin{tikzpicture}
\matrix (mymatrix) [matrix of math nodes,left delimiter={(},right
delimiter={)}]
{ A(1)  &  B_{1,2} & B_{1,3}&  \cdots  &  B_{1,r-1} & B_{1,r} \\
B_{1,2}^T  &  A(2)  &  0 & \cdots &  0  &  0 \\
B_{1,3}^T &  0  &  A(3)  &  \cdots  &  0 & 0\\
\vdots & 0 &  0  &   \ddots  &  0  &  0\\
B_{1,r-1}^T  &  0  &  0  &  0  &  0  &  0\\
B_{1,r}^T  &  0  &  0  &  0  &  0  &  0\\
};
\foreach \X [evaluate=\X as \Y using {int(\X+1)}]in {1,...,2}
{\path ($(mymatrix-\X-1.south west)!0.5!(mymatrix-\Y-1.north west)$) coordinate
(aux\X);
\draw[black,dashed] (mymatrix.west |- aux\X) -- (mymatrix.east |- aux\X);
}
\foreach \X [evaluate=\X as \Y using {int(\X+1)}]in {1,...,5}
{\path ($(mymatrix-1-\X.east)!0.5!(mymatrix-1-\Y.west)$) coordinate
(auy\X);
\draw[black,dashed] (mymatrix.north -| auy\X) -- (mymatrix.south -| auy\X);
}
\end{tikzpicture}
\end{document}


• It is perfect. thanks. How do you draw vertical lines? – No one Dec 13 '18 at 10:58
• @Noone Sorry, I was too sloppy, but I added it now. – marmot Dec 13 '18 at 11:02
• I want the vertical for every column. not for a one. – No one Dec 13 '18 at 11:04
• @Noone Doesn't this contradict with you user name? (Just kidding! ;-) – marmot Dec 13 '18 at 11:11
• Thank you bro, you saved my day. LOL, Did you see Game of Thrones movie? I am that No one guy ;) – No one Dec 13 '18 at 11:14