使用 tikz math 时不要停留在环境中

使用 tikz math 时不要停留在环境中

我遇到了类似的问题,如图所示这里但是当我将矩阵放在tikz块内时,Example环境不会跳过一行并将所有内容放在同一行中。与类似问题一样,我想将矩阵放在一行下方并居中。

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{tikz}
\newtheorem{xmp}{Example}

\begin{document}
    \begin{xmp}
        \[ \mathbf{A}_{4\times 4}=
            \begin{tikzpicture}\(
                \begin{bmatrix*}[r]
                    -9 & -1 & 0 & -3 \\
                    -5 & -8 & -9 & 4 \\
                    7 & 3 & -1 & -7 \\
                    -8 & 1 & 2 & 7
                \end{bmatrix*} \)
            \end{tikzpicture}
        \]
    \end{xmp}

\end{document}

我将矩阵放在块内,因为如果我使用矩阵库,我就无法使用包提供的环境tikz右对齐数字。{bmatrix*}[r]mathtools

答案1

不要将bmatrix直接放在 中tikzpicture,而是将其放在 里面。顺便说一下,node您可以将 中的节点右对齐,设置。matrix of math nodesnodes={anchor=base east}

注意baseline添加到tikzpicture环境中的键,用于改善其垂直对齐。

在此处输入图片描述

\documentclass[10pt,a4paper]{article}
\usepackage[latin1]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{matrix}
\newtheorem{xmp}{Example}

\begin{document}
    \begin{xmp}
        \[ \mathbf{A}_{4\times 4}=
                \begin{bmatrix*}[r]
                    -9 & -1 & 0 & -3 \\
                    -5 & -8 & -9 & 4 \\
                    7 & 3 & -1 & -7 \\
                    -8 & 1 & 2 & 7
                \end{bmatrix*}
        \]
    \end{xmp}


    \begin{xmp}
        \[ \mathbf{A}_{4\times 4}=
                \begin{tikzpicture}[baseline]
                \node [inner sep=0pt, name=m] {
                    \(\begin{bmatrix*}[r]
                    -9 & -1 & 0 & -3 \\
                    -5 & -8 & -9 & 4 \\
                    7 & 3 & -1 & -7 \\
                    -8 & 1 & 2 & 7
                \end{bmatrix*}\)};
              \end{tikzpicture}
        \]
    \end{xmp}
    \begin{xmp}
        \[ \mathbf{A}_{4\times 4}=
                \begin{tikzpicture}[baseline]
                \matrix [name=m,
                         inner xsep=0,
                         inner ysep=2pt,
                         matrix of math nodes,
                         right delimiter={]},
                         left delimiter={[},
                         column sep=8pt,
                         row sep=5pt,
                         nodes={anchor=base east,
                                inner sep=0pt}]{
                    -9 & -1 & 0 & -3 \\
                    -5 & -8 & -9 & 4 \\
                    7 & 3 & -1 & -7 \\
                    -8 & 1 & 2 & 7 \\
                   };
              \end{tikzpicture}
        \]
     \end{xmp}
\end{document}

答案2

我不明白将矩阵放在节点内部有什么好处。如果你想将它作为图像,其中每个矩阵元素都有一个名称,那么你需要将矩阵写为tikz图片:

\documentclass[10pt,a4paper]{article}
%\usepackage[latin1]{inputenc} % default in article is utf8
\usepackage{amsmath}           % loaded by mathtools
%\usepackage{amsfonts}         % loaded by amssymb
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{backgrounds, fit, matrix}
\newtheorem{xmp}{Example}

\usepackage{url}

\begin{document}
\begin{xmp}
\[
\mathbf{A}_{4\times 4}=
    \begin{tikzpicture}[baseline]
    \matrix (m) [matrix of math nodes,%
                 nodes={inner sep=1pt, text width=3ex, align=right},
                 left delimiter={[},right delimiter={]},
                 column sep=1ex, row sep=1ex,
                 ]
    {
    -9  & -1  &  0  & -3    \\
    -5  & -8  & -9  &  4    \\
     7  &  3  & -1  & -7    \\
    -8  &  1  &  2  &  7    \\
    };
    \end{tikzpicture}
\]
\end{xmp}
\begin{xmp}
\[
\mathbf{A}_{4\times 4}=
    \begin{tikzpicture}[baseline]
    \matrix (m) [matrix of math nodes,%
                 nodes={inner sep=1pt, text width=3ex, align=right},
                 left delimiter={[},right delimiter={]},
                 column sep=1ex, row sep=1ex,
                 ]
    {
    -9  & -1  &  0  & -3    \\
    -5  & -8  & -9  &  4    \\
     7  &  3  & -1  & -7    \\
    -8  &  1  &  2  &  7    \\
    };
    \scoped[on background layer]
    \node[fill=red!30,inner sep=1pt,
          fit=(m-2-2) (m-4-4)]   {};
    \end{tikzpicture}
\]
\end{xmp}
an example of the representation of determinants calculation (taken from \url{http://tex.stackexchange.com/questions/257043/})
\[
\mathbf{A}_{3\times 3}=
\begin{tikzpicture}[baseline,
strip/.style = {
    draw=#1,%color
    line width=1em, opacity=0.2,
    line cap=round,% only if you like them ...
    shorten <=-1mm, shorten >=-1mm,
                },
                    ]
\matrix (m)     [matrix of math nodes,
                 column sep=1em,
                 nodes={text height=1ex,text width=2ex}
                 ]
{
|[red]|+
    & |[red]|+
          & \color{red}+\color{blue}-
                & |[blue]|-
                      & |[blue]|-   \\[3.3mm,between origins]
a_1 & b_1 & c_1 & a_1 & a_2         \\
a_2 & b_2 & c_2 & a_2 & b_2         \\
a_3 & b_3 & c_3 & a_3 & b_3         \\
};
\draw[thick] (m-2-1.north) -| (m-4-1.south west)
                           -- (m-4-1.south);
\draw[thick] (m-2-3.north) -| (m-4-3.south east)
                           -- (m-4-3.south);
\draw[strip=blue]%
    (m-4-1.center) edge (m-2-3.center)
    (m-4-2.center) edge (m-2-4.center)
    (m-4-3.center)  --  (m-2-5.center);
\draw[strip=red]
    (m-2-1.center) edge (m-4-3.center)
    (m-2-2.center) edge (m-4-4.center)
    (m-2-3.center)  --  (m-4-5.center);
\end{tikzpicture}
    \]
\[
\det A = (\textcolor{red} {a_1b_2c_3 + b_1c_2a_3 + c_1a_2b_3}) -
         (\textcolor{blue}{a_3b_2c_1 + b_3c_2a_1 + c_3a_2b_1})
\]
\end{document}

在此处输入图片描述

相关内容