1

I wish to typeset a math equation involving boxes of different sizes (which represent matrices of different sizes in my work).

What I have done so far is as follows:

\documentclass{article}
\usepackage{tikz}

\tikzstyle{mymatrix} = [rectangle, draw = black, anchor=base]
\newcommand{\largetikzmatrix}[1]{\tikz[baseline=(x.base)]{\node[mymatrix,minimum width=35pt,minimum height=35pt,inner sep=0pt] (x) {\makebox[0pt]{\ensuremath{#1}}};}}
\newcommand{\talltikzmatrix}[1]{\tikz[baseline=(x.base)]{\node[mymatrix,minimum width=12pt, minimum height=35pt,inner sep=0pt] (x) {\makebox[0pt]{\ensuremath{#1}}};}}
\newcommand{\smalltikzmatrix}[1]{\tikz[baseline=(x.base)]{\node[mymatrix,minimum width=12pt, minimum height=12pt,inner sep=0pt] (x) {\makebox[0pt]{\ensuremath{#1}}};}}

\begin{document}
\[
\largetikzmatrix{Z_k}
=
\talltikzmatrix{N_k} \smalltikzmatrix{M_k}^{-1}
\]
\end{document}

result

Note that I have used \makebox[0pt] to make sure that the text inside the nodes does not make them increase in size. I prefer a little clash between the subscript and the box, rather than increasing the node size (which would make things more difficult to understand).

The only thing I am not satisfied with is the lack of spacing between the $N_k$ node and the $M_k$ node. I would like to have the same spacing as there is normally between two variables in a math expression. I have looked for something like \mathvar to make sure that the pictures are interpreted as math variables (analogous to \mathop and \mathrel), but I don't see it, and frankly I am not even sure that this is really the issue here.

I know I can throw a \, in there artificially to make some space, but it would be good if I could understand why the TeX `normal' math spacing is not used here and how to fix this problem.

2
  • Do you have real matrices or symbolic ones (like above)? Are those boxes needed in real use case?
    – user11232
    Oct 15 '15 at 4:31
  • @HarishKumar I have edited for clarity -- I have boxes, not true matrices. Oct 15 '15 at 5:36
2

There is no space between variables in math expression:

\[ a b c \]

gives you

enter image description here

But you can add the spce you want with \, \quad, ... which also wotk with your code

\documentclass{article}
\usepackage{tikz}

\tikzstyle{mymatrix} = [rectangle, draw = black, anchor=base]
\newcommand{\largetikzmatrix}[1]{\tikz[baseline=(x.base)]{\node[mymatrix,minimum width=35pt,minimum height=35pt,inner sep=0pt] (x) {\makebox[0pt]{\ensuremath{#1}}};}}
\newcommand{\talltikzmatrix}[1]{\tikz[baseline=(x.base)]{\node[mymatrix,minimum width=12pt, minimum height=35pt,inner sep=0pt] (x) {\makebox[0pt]{\ensuremath{#1}}};}}
\newcommand{\smalltikzmatrix}[1]{\tikz[baseline=(x.base)]{\node[mymatrix,minimum width=12pt, minimum height=12pt,inner sep=0pt] (x) {\makebox[0pt]{\ensuremath{#1}}};}}

\begin{document}
\[
\largetikzmatrix{Z_k}
=
\talltikzmatrix{N_k}\ \smalltikzmatrix{M_k}^{-1}
\]
\end{document}

enter image description here

1
  • Thanks! This makes sense, it was my expectation that there had to be a spacing which was misguided. Oct 15 '15 at 8:52
1

Ignasi already answered the space issue, so this is just an improvement on tikz drawing.

\documentclass{article}
\usepackage{tikz,amsmath}

\tikzset{ mymatrix/.style = {rectangle, draw = black, anchor=base},}
\newcommand{\largetikzmatrix}[1]{\tikz[baseline=(x.base)]{\draw[path picture={
            \node[anchor=center] (x) at (path picture bounding box.center) {$#1$};
          }] (-17pt,-17pt) rectangle (17pt,17pt);}}
\newcommand{\talltikzmatrix}[1]{\tikz[baseline=(x.base)]{\draw[path picture={
            \node[anchor=center] (x) at (path picture bounding box.center) {$#1$};
          }] (-6pt,-17pt) rectangle (6pt,17pt);}}
\newcommand{\smalltikzmatrix}[1]{\tikz[baseline=(x.base)]{\draw[path picture={
            \node[anchor=center] (x) at (path picture bounding box.center) {$#1$};
          }] (-6pt,-6pt) rectangle (6pt,6pt);}}


\begin{document}
\[
\largetikzmatrix{Z_k}
= 
\talltikzmatrix{N_k}\ \smalltikzmatrix{M_k}^{-1}\]
\end{document}

enter image description here

2
  • If I see correctly, this clips the part of the text that goes outside the rectangular box, right? I guess I need to use two separate instructions to avoid it, a \node and a \draw rectangle. But I like your approach more than mine, thanks. Oct 15 '15 at 8:55
  • @FedericoPoloni: It is better to clip rather than having text protruding outside ;-). On a serious note, you can change the dimensions of the rectangle suitably.
    – user11232
    Oct 15 '15 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.