Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Draw boxes for matrices in equation
%Make sure to have \usepackage{tikz}
%https://tex.stackexchange.com/a/45815/140440 - for grid
%https://tex.stackexchange.com/a/381175/140440 - for alignment in equation
% This function draws a matrix.
\newcommand{\mat}[2]{% cols, rows
\vcenter{\hbox{ %Vertical alignment
\begin{tikzpicture}[scale=0.3, align=center]
\filldraw[step=1,black] (0.0, 0.0) grid (#1, #2);
\end{tikzpicture}
}}}
% This function draws a matrix with a label above it.
\newcommand{\labmat}[3]{% cols, rows, label
\stackrel{#3}{
\vcenter{\hbox{ %Vertical alignment
\begin{tikzpicture}[scale=0.3, align=center]
\filldraw[step=1,black] (0.0, 0.0) grid (#1, #2);
\end{tikzpicture}
}}}}
% This function allows for shading
% cols, rows, cols_shade_start, rows_shade_start, cols_shade_end, rows_shade_end, label
\newcommand{\colormat}[7]{
\stackrel{#7}{
\vcenter{\hbox{ %Vertical alignment
\begin{tikzpicture}[scale=0.3, align=center]
\filldraw[step=1] (0.0, 0.0) grid (#1, #2);
\filldraw[shift={(#4, #3)}, fill=gray] (0, 0) rectangle (#6, #5);
\end{tikzpicture}
}}}}
% Here are some examples:
$$\mat{2}{3} \cdot \exp(\mat{3}{2})$$
%And, with labels:
$$\labmat{2}{3}{X} \cdot \exp(\labmat{3}{2}{Y})$$
%Shaded:
$$\colormat{2}{3}{1}{0}{1}{1}{X}$$
@MilesCranmer

This comment has been minimized.

Copy link
Owner Author

@MilesCranmer MilesCranmer commented Oct 4, 2020

Output:
Screen Shot 2020-10-04 at 10 12 17 AM

@MilesCranmer

This comment has been minimized.

Copy link
Owner Author

@MilesCranmer MilesCranmer commented Oct 4, 2020

With shading example:
Screen Shot 2020-10-04 at 10 41 16 AM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.