14

I need to write matrix splitting and factorization operations (for my linear algebra course). It is hard to do these is direct Latex.

I can use Visio to draw these easily, but I'd prefer to use Tikz now since I use Latex for my report.

I'd like to show a small example of what I typically need, and a crude implementation in tikz to illustrate what I need and ask if there exist a library or general approach to do this Tikz.

Here is a typical example of what I'd like to typeset:

enter image description here

Here is the result of the tikz drawing

enter image description here

Here is the code

\documentclass{article}
\usepackage{tikz}
\begin{document}
\[
A=L+D+U
\]
where

\begin{tikzpicture}
    \def\x{0}
   \def\y{0}
   \def\L{2}
   \def\s{.25}
   \def\z{.35}  

    \draw [very thin,fill=lightgray] (\x,\y) rectangle (\x+\L,\y+\L);
    \node at (\x+\L+\s,\y+\L/2) {$=$};
    \node[font=\Large] at (\x+0.5*\L,\y-\z) {$A$};

    \pgfmathsetmacro\x{\x+\L+2*\s}

    \draw [very thin] (\x,\y) rectangle (\x+\L,\y+\L);
    \draw [fill=lightgray] (\x,\y)--(\x+.9*\L,\y)--(\x,\y+.9*\L);
    \node[font=\Large] at (\x+0.65*\L,\y+0.65*\L) {$0$};
    \node[font=\Large] at (\x+0.5*\L,\y-\z) {$L$};

    \node at (\x+\L+\s,\y+\L/2) {$+$};

    \pgfmathsetmacro\x{\x+\L+2*\s}

    \draw [very thin] (\x,\y) rectangle (\x+\L,\y+\L);
     \draw [fill=lightgray]  (\x+0.9*\L,\y)--(\x+\L,\y)--(\x+\L,\y+0.1*\L)--(\x+0.1*\L,\y+\L)--(\x,\y+\L)--(\x,\y+0.9*\L)--(\x+0.9*\L,\y);
     \node[font=\Large] at (\x+0.25*\L,\y+0.25*\L) {$0$};
     \node[font=\Large] at (\x+0.75*\L,\y+0.75*\L) {$0$};
     \node[font=\Large] at (\x+0.5*\L,\y-\z) {$D$};

     \node at (\x+\L+\s,\y+\L/2) {$+$};

    \pgfmathsetmacro\x{\x+\L+2*\s}

    \draw [very thin] (\x,\y) rectangle (\x+\L,\y+\L);
    \draw [fill=lightgray] (\x+\L,\y+0.1)--(\x+\L,\y+\L)--(\x+0.1,\y+\L);
    \node[font=\Large] at (\x+0.35*\L,\y+0.35*\L) {$0$};
    \node[font=\Large] at (\x+0.5*\L,\y-\z) {$U$};

\end{tikzpicture}
\end{document}

The matrix itself can be a rectangle as shown, no fancy parenthesis needed and such. The idea is just to highlight the structure of the matrices (diagonal, upper, lower, banded, etc...)

update 7/23/13 9:40 pm

Is there a way to make the sizes remain the same in the align env?

update 10:40 pm Thanks for Qrrbrbirlbel reply. The correct way to keep the size the same is

{\tikzset{set ALDU=small}
\begin{align*}
  A        &= L + D + U  \\
  \ALDU{A} &= \ALDU{L} + \ALDU{D} + \ALDU{U}
\end{align*}
}

enter image description here

isn't Latex nice? try to do this in Microsoft word :)

11

Code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\def\ppbb{path picture bounding box}
\tikzset{
  common ALDU/.style={
    shape=rectangle,
    label={below:{$#1$}},
    draw=black
  },
  set ALDU/.is choice,
  set ALDU/small/.style={
    ALDU/.style={common ALDU=#1,minimum size=2em},
    zero node/.style={minimum size=+0pt,font=\scriptsize}},
  set ALDU/big/.style={
    ALDU/.style={common ALDU=#1,minimum size=3em},
    zero node/.style={minimum size=+0pt}},
  set ALDU=big,
  A/.style={ALDU=A,#1},
  L/.style={ALDU=L,
    path picture={
      \fill[l] ($(\ppbb.north west)!.2!(\ppbb.south west)$) --
               ($(\ppbb.south east)!.2!(\ppbb.south west)$) --
               (\ppbb.south west)                           -- cycle;
      \node[zero node] at ($(\ppbb.south west)!.7!(\ppbb.north east)$) {$0$};
    }},
  U/.style={ALDU=U,
    path picture={
      \fill[u] ($(\ppbb.north west)!.2!(\ppbb.north east)$) --
               ($(\ppbb.south east)!.2!(\ppbb.north east)$) --
               (\ppbb.north east)                           -- cycle;
      \node[zero node] at ($(\ppbb.south west)!.3!(\ppbb.north east)$) {$0$};
    }},
  D/.style={ALDU=D,
    path picture={
      \fill[d] ($(\ppbb.north west)!.2!(\ppbb.north east)$) coordinate (@aux) --
               ($(\ppbb.south east)!.2!(\ppbb.north east)$)                   |-
               ($(\ppbb.south east)!.2!(\ppbb.south west)$)                   --
               ($(\ppbb.north west)!.2!(\ppbb.south west)$)         |- (@aux) -- cycle;
      \path[nodes=zero node] node at ($(\ppbb.south west)!.225!(\ppbb.north east)$) {$0$}
                            node at ($(\ppbb.south west)!.775!(\ppbb.north east)$) {$0$};
  }},
  aldu/.style={draw=black,fill=lightgray},
  A/.default={aldu},
  l/.style={aldu},
  d/.style={aldu},
  u/.style={aldu}
}
\newcommand*{\ALDU}[2][]{\tikz[baseline=-.5ex,#1]\node[#2] {};}
\begin{document}
\[ \ALDU{A} = \ALDU{L} + \ALDU{D} + \ALDU{U} \]
\tikzset{set ALDU=small}
\[ \ALDU{A} = \ALDU{L} + \ALDU{D} + \ALDU{U} \]
\end{document}

Output

enter image description here

  • Thank you for a nice implementation. Much better than my code :) I am trying to learn your code and hopefully add more shapes (there should be package of such shapes since they are very useful). But meanwhile, would you please know why the size of the A matrix becomes smaller than the other matrices in the RHS when I put everything inside an \align environment? Please see example of what I mean added to end of my question. May be this is a latex thing. I do not know. – Nasser Jul 24 '13 at 2:41
  • 1
    @Nasser The align environments put each cell in their own group, thus the \tikzset only affects this cell. You need to use \tikzset outside of the align* environment. If it should only affect this equation, you need to group it again, say {\tikzset{set ALDU=small}\begin{align*} … & … \end{align*}}; or use it twice (if it should only affect one of many lines in align). — What other shapes and variations exist? One could define them much more general and with a better user-interface. One could also create new shapes but then you need to restrict yourself to, say, rectangles. – Qrrbrbirlbel Jul 24 '13 at 3:12

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.