Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volkancirik/7275ade8e0007f158445206af1d541c9 to your computer and use it in GitHub Desktop.
Save volkancirik/7275ade8e0007f158445206af1d541c9 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}$$
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment