Skip to content

Instantly share code, notes, and snippets.

@MilesCranmer
Last active February 9, 2021 10:49
Show Gist options
  • Save MilesCranmer/ea65307357556621ef49c482f1b4e8f0 to your computer and use it in GitHub Desktop.
Save MilesCranmer/ea65307357556621ef49c482f1b4e8f0 to your computer and use it in GitHub Desktop.
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
Copy link
Author

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