是否有专门的库或包可以在 Tikz 中绘制分块/分区/分裂矩阵?

是否有专门的库或包可以在 Tikz 中绘制分块/分区/分裂矩阵?

我需要编写矩阵分裂和因式分解运算(用于我的线性代数课程)。直接使用 Latex 很难完成这些操作。

我可以使用 Visio 轻松绘制这些,但我现在更喜欢使用 Tikz,因为我使用 Latex 来绘制报告。

我想展示一个我通常需要的小例子,以及一个粗略的 tikz 实现来说明我需要什么,询问是否存在一个库或者通用方法来执行此 Tikz

这是我想要排版的典型示例:

在此处输入图片描述

这是 tikz 绘图的结果

在此处输入图片描述

以下是代码

\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}

矩阵本身可以是如图所示的矩形,不需要花哨的括号等。这个想法只是为了突出矩阵的结构(对角线、上、下、带状等...)

更新于 2013 年 7 月 23 日 晚上 9:40

有没有办法让尺寸在align环境中保持不变?

晚上10:40更新 感谢 Qrrbrbirlbel 的回复。保持尺寸不变的正确方法是

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

在此处输入图片描述

Latex 不是很棒吗?尝试在 Microsoft Word 中执行此操作 :)

答案1

代码

\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}

输出

在此处输入图片描述

相关内容