使用 tikzpicture 进行矩阵绘制

使用 tikzpicture 进行矩阵绘制

我有这个代码

 \tikzstyle{matrici}=[
 matrix of math nodes, nodes in empty cells,
 nodes={draw=gray, align=center, inner sep=0pt, text width=1cm, minimum 
 height=1cm}]

 \begin{tikzpicture} 

 \matrix[matrici, 
 ]  (X)  {&&\\};
 \matrix[  matrici, at=(X), yshift=-1.5cm,
 ]  (Y)  {&&\\};
 \matrix[  matrici, at=(Y), yshift=-1.5cm,
 ]  (A)  {&&\\};
 \matrix[  matrici, at=(A), yshift=-1.5cm, xshift=-1cm,
 ]  (B)  {&&\\};
 \matrix[  matrici, at=(B), yshift=-1.5cm, xshift=-1cm,
 ]  (C)  {&&\\};
 \matrix[  matrici, at=(C), yshift=-1.5cm, xshift=+0.5cm 
 ]  (D) {&&&&&\\}; 


 \node[at=(X-1-3), xshift=1cm ] {$ \times $}; 
 \node[at=(Y-1-3), xshift=1cm ] {$ = $}; 

 \end{tikzpicture}  

和这个输出

在此处输入图片描述

但是我怎样才能实现这个输出呢?当然我可以画很多直线来测量所有测量值,但这太愚蠢了。我怎样才能在最后一个矩阵中将第一个矩阵节点划掉呢?

在此处输入图片描述

答案1

matrix您也可以使用矩形作为替代方案multipart

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning, shapes.multipart, arrows.meta}

\begin{document}
\begin{tikzpicture}[
    mybox/.style={
        rectangle split, rectangle split horizontal, rectangle split parts=#1, draw, anchor=center},
    mybox/.default=3,
    node distance = 2mm]
        \node[mybox] (a) {};
        \node[right=of a] (x) {$\times$};
        \node[mybox, below=of a] (b) {};
        \node[right=of b] (eq) {$=$};
        \node[mybox, below=of b] (c) {};
        \node[mybox, below=of c.two split south, anchor=north east] (d) {};
        \node[mybox, below=of d.two split south, anchor=north east] (e) {};
        \node[mybox=6, below=of e.south west, anchor=one split north] (f) {};
        \draw ([shift={(-3mm,1mm)}]f.north west) coordinate(aux1)--([shift={(8mm,1mm)}]f.north east) coordinate (aux2);

        \draw (aux1|-c.north)+(0,1mm) coordinate(aux3)--(aux3-|aux2);
        \draw[Bar-Bar] ([xshift=3mm]c.north east) coordinate (aux4) -- (aux4|-e.south) node[right, midway]{$n$};
        \draw[Bar-Bar] ([yshift=-2mm]f.south west) -- ([yshift=-2mm]f.south east) node[below, midway]{$2n$};
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我会做一个独特的matrix,这样可以更简单地定位您需要的各种绿线。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix, fit}
\tikzset{
    matriciona/.style={
        matrix of math nodes,
        nodes in empty cells,
        row sep=14pt,
        column sep=-\pgflinewidth,% to avoid double borders in contiguous cells
        nodes={
             align=center, inner sep=0pt, text 
                width=1cm, minimum height=1cm,
                anchor=center
        }
    },
    my/.style={
        draw=gray
    },
    trattini/.style={
        draw, dashed
    },
    linee/.style={
        green!50!black, very thick
    },
    descr/.style={
        green!50!black, font=\large
    },
} 

\begin{document}
    \begin{center}
        \begin{tikzpicture} 
            \matrix[matriciona] (M) {
                & & & |[my]| & |[my]| & |[my]| & \times \\ 
                & & & |[my]| & |[my]| & |[my]| & = \\[6pt]% a little more space between this row and the following 
                & & & |[my]| & |[my]| & |[my]| & \\ 
                & & |[my]| & |[my]| & |[my]| & & \\ 
                & |[my]| & |[my]| & |[my]| & & & \\[6pt]% a little more space between this row and the following  
               |[trattini]| & |[my]| & |[my]| & |[my]| & |[my]| & |[my]| & \\ 
                };
            \node[fit=(M-3-1)(M-5-6), inner ysep=10pt, inner xsep=1cm] (finto) {};
            \draw[linee] (finto.north west) -- (finto.north east);
            \draw[linee] (finto.south west) -- (finto.south east);
            \draw[linee, |-|] (M-5-7.south) --  node[descr, xshift=10pt] {$n$} (M-3-7.north);
            \draw[linee, |-|] ([yshift=-10pt]M-6-1.south west) --  node[descr, yshift=-10pt] {$2n$} ([yshift=-10pt]M-6-6.south east);
      \end{tikzpicture}      
  \end{center}
\end{document} 

在此处输入图片描述

答案3

谢谢 CarLaTeX 和 Ignasi,你们教会了我很多东西。我更喜欢这个解决方案:

\tikzset{
s/.style 2 args={insert path={+(#1:{0.5*#2})--+(180+#1:{0.5*#2})}}
}

\tikzstyle{matrici}=[
matrix of math nodes, 
nodes in empty cells,
column sep=-\pgflinewidth, % to avoid double borders in contiguous cells
nodes={
       draw, 
       align=center, 
       inner sep=0pt, 
       text width=1cm, 
       minimum height=1cm
    }
    ]

 \begin{center}
 \begin{tikzpicture} 
 \matrix[matrici, 
 ]  (X)  {&&\\};
 \matrix[  matrici, at=(X), yshift=-1.5cm,
 ]  (Y)  {&&\\};
 \matrix[  matrici, at=(Y), yshift=-1.5cm,
 ]  (A)  {&&\\};
 \matrix[  matrici, at=(A), yshift=-1.5cm, xshift=-1cm,
 ]  (B)  {&&\\};
 \matrix[  matrici, at=(B), yshift=-1.5cm, xshift=-1cm,
 ]  (C)  {&&\\};
 \matrix[  matrici, at=(C), yshift=-1.5cm, xshift=+0.5cm 
 ]  (D) {|[dashed]|&&&&&\\}; 


 \node[at=(X-1-3), xshift=1cm ] {$ \times $}; 
 \node[at=(Y-1-3), xshift=1cm ] {$ = $}; 


 \draw ([yshift=0.75cm]D)  [s={0}{8}]; 
 \draw ([yshift=5.25cm]D)  [s={0}{8}]; 

 \draw[|-|] ([yshift=-0.75cm]D)  [s={00}{6}] node[below, midway]{$2n$}; 
 \draw[|-|] ([xshift=+3cm]B)     [s={90}{4}] node[right, midway]{$n$}; 
 \end{tikzpicture}     
 \end{center}

输出:

在此处输入图片描述

在我的解决方案中,我使用了数十亿次“cm”!没有办法将“cm”设置为默认值\tikzset?例如,为了使用xshift=+3

相关内容