# Enforcing That \array Stays Square When Drawing Matrix

I'm trying to draw a block matrix in LaTeX, and this is what I have so far:

\begin{array}{|c|c|}
\hline
I_{r \times r} & 0 \\
\hline
0 & 0 \\
\hline
\end{array}


Here's a picture of what this looks like when rendered:

How would I enforce that the matrix be square when rendered? Right now, it's clearly wider than it is tall because of the I_{r \times r} placed in top-left element. Also, the intersections of the lines at the corners of the matrix are not being rendered properly. Do you have any suggestions as to how I could fix this as well?

-

the width/height of a cell is a parameter of the columntype C

\documentclass{article}
\usepackage{array}
\newcolumntype{C}[1]{@{}>{\rule[0.5\dimexpr-#1+1.2ex]{0pt}{#1}\hfil$}p{#1}<{$\hfil}@{}}
\begin{document}
\Huge
$\begin{array}{|C{2cm} | C{2cm} |}\hline I_{r \times r} & 0 \\\hline 0 & 0 \\\hline \end{array}$
$\begin{array}{|C{2.5cm} | C{2.5cm} |}\hline I_{r \times r} & 0 \\\hline 0 & 0 \\\hline \end{array}$
\end{document}


-
Fine idea! why \rule[0.5\dimexpr-#1+1.2ex? –  Alain Matthes Feb 13 '12 at 9:23
The question is why +1.2ex? –  Alain Matthes Feb 13 '12 at 9:31
it is 0.6ex which is nearly the half of the current line height –  Herbert Feb 13 '12 at 9:44
This automatically centers itself in equation mode, which is really convenient! Also, is there some predefined macro that contains the current line height? In case this changes, it would be nice if the matrix would also scale accordingly. –  void-pointer Feb 13 '12 at 16:54
what do you mean with "scale accordingly"? Should the matrix calculate the width/height of the biggest cell itself? –  Herbert Feb 13 '12 at 17:05

With tikz (There are others methods with tikz like matrix etc.)

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\foreach \x in {1,2}
\foreach \y in {1,2}
{
\draw (\x,\y) +(-.5,-.5) rectangle ++(.5,.5);
\draw (\x,\y);
}
\node at (1,2) {$I_{r \times r}$} ;
\node at (1,1) {0} ; \node at (2,1) {0} ;\node at (2,2) {0} ;
\end{tikzpicture}

\end{document}


or you can do something like that but I'm not sure if all blank spaces are removed.

 \def\vh{\vrule height 0.6cm depth 0.4cm width 0 cm}%
\newcommand{\sq}[2][1cm]{\hbox to #1{\hfil\vh#2\hfil}}%
$\begin{array}{@{}|@{}c@{}|@{}c@{}|@{}} \hline \sq{$I_{r \times r}$}&\sq{0}\\ \hline \sq{0} & \sq{0}\\ \hline \end{array}$

-
Thanks! The matrix is a lot easier to draw using TikZ, but I was having trouble getting it centered with the equals sign in equation mode. When I do $A = \begin{tikzpicture} ... \end{tikzpicture}$, the entire matrix is placed above the equals sign. I tried using \raisebox, but I would still need to iteratively search for the correct displacement value. –  void-pointer Feb 13 '12 at 16:49
\$A=\begin{tikzpicture}[baseline=(current bounding box.center)] ... –  Alain Matthes Feb 13 '12 at 20:44
That was immensely helpful, thanks! I'm being a little nitpicky here, but if you zoom in to the intersection of the second horizontal line from the top with the leftmost vertical line, you can see that they're not joining properly. I'm trying out some examples with \matrix so see if I can ameliorate this. tikzpicture and \matrix are certainly a lot more versatile than \array. –  void-pointer Feb 13 '12 at 22:20
I don't see a problem but I work with pgf 2.1 cvs. Perhaps it would be useful to sk another question with your problem –  Alain Matthes Feb 13 '12 at 23:02