TikZ:对齐两个 Tikz 图片并将括号与节点对齐

TikZ:对齐两个 Tikz 图片并将括号与节点对齐

我尝试使用 Tikz 绘制两个图形。如何将它们垂直对齐?我希望结点形成中心,这样所有六个节点都垂直对齐。此外,我想使用节点对齐花括号。但这也行不通。我希望括号从第一个节点的开头开始,到最后一个节点的结尾结束。我该怎么做?

这是我得到的:

enter image description here

这就是我想要的:

enter image description here

此代码生成了上面的图片:

\documentclass[a4paper]{article}
\usepackage{tikz}

\begin{document} 

\usetikzlibrary{chains, positioning, decorations.pathreplacing}

\def\ilsize{3}
\def\nodesize{6mm}

\tikzset{>=latex}

\begin{figure}[h]
\centering

\begin{tikzpicture}[shorten >=0pt, ->, draw=black!100]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{node}=[circle, draw, fill=black!100, minimum size=\nodesize,inner sep=0pt]
    \tikzstyle{input node}=[node, fill=black!0]

    % Nodes
    \foreach \name / \y in {1,...,\ilsize}
        \node[input node] (In-\name) at (0.0cm,-\y cm) {$w_{\y}$};

    % Labels %
    \draw[-, decoration={brace,raise=0pt, amplitude=3mm}, decorate, xshift=10mm, yshift=0mm]
    (In-1) -- (In-3) node[black,midway,right=3mm] {$g(v)$};
    \draw[-, decoration={brace, raise=0pt, amplitude=3mm, mirror}, decorate, xshift=10mm, yshift=0mm]
    (In-1) -- (In-3) node[black,midway,left=3mm] {$v$};
\end{tikzpicture}

\begin{tikzpicture}[shorten >=0pt, ->, draw=black!100]
    \tikzstyle{every pin edge}=[<-,shorten <=1pt]
    \tikzstyle{node}=[circle, draw, fill=black!100, minimum size=\nodesize,inner sep=0pt]
    \tikzstyle{input node}=[node, fill=black!0]

    % Nodes
    \foreach \name / \y in {1,...,\ilsize}
        \node[input node] (In-\name) at (0.0cm,-\y cm) {$u_{\y}$};

    % Labels %
    \draw[-, decoration={brace,raise=0pt, amplitude=3mm}, decorate, xshift=0mm, yshift=0mm]
 (In-1) -- (In-3) node[black,midway,right=3mm] {$g(v)$};
  %(Out-1) -- (Out-3) node[black,midway,xshift] {$f(x)$};
    \draw[-, decoration={brace, raise=0pt, amplitude=3mm, mirror}, decorate, xshift=0mm, yshift=0mm]
  (In-1) -- (In-3) node[black,midway,left=3mm] {$\mathcal{K}=U*V*W$};
\end{tikzpicture}
\end{figure}

\end{document}

答案1

正如我提到的,将机器人合并tikzpicture为一个可以更好地控制其元素的位置:

编辑: 根据新解释对括号位置进行了更正

\documentclass[a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing} % tikz library should be here, in preamble

\begin{document}
    \begin{figure}[htb]
    \centering
\def\ilsize{3}
\def\nodesize{6mm}
    \begin{tikzpicture}[
   B/.style = {% common "universal" style for all braces
               decoration={brace, amplitude=3mm,#1},% #1 is for mirroring, when necessary
               decorate},
 B/.default = ,  % default braces are not mirrored
circ/.style = {% style name, which is not used by tikz
               circle, draw, minimum size=\nodesize, inner sep=0pt},
                        ]
% Nodes
\foreach \name / \y in {1,...,\ilsize}
        \node[circ] (In-\name) at (0,-\y) {$w_{\y}$};
% Labels
\draw[B]
    (In-1.north -| In-1.east) -- node[right=3mm] {$g(v)$}               (In-3.south -| In-3.east);
\draw[B=mirror]
    (In-1.north -| In-1.west) -- node[left=3mm] {$v$}   (In-3.south -| In-3.west) ;

    \begin{scope}[yshift=-31mm]
% Nodes
\foreach \name / \y in {1,...,\ilsize}
        \node[circ] (In-\name) at (0,-\y)   {$w_{\y}$};
% Labels %
\draw[B]        % draw brace
    (In-1.north -| In-1.east) -- node[right=3mm] {$g(v)$}               (In-3.south -| In-3.east);
\draw[B=mirror] % draw mirrored brace
    (In-1.north -| In-1.west) -- node[left=3mm] {$\mathcal{K}=U*V*W$}   (In-3.south -| In-3.west) ;
    \end{scope}
\end{tikzpicture}
    \end{figure}
\end{document}

上述 mwe 包括:

  • 使用最近的语法来定义节点和其他元素样式(\tikzstyle已过时,您应该使用\tizset或作为 mwe 中的选项tikzpicture
  • 库应该在文档序言中加载
  • 永远不要使用名称node,它已经用作tikz
  • 定义括号的通用样式是明智的

结果是:

enter image description here

答案2

您可以使用两个matrix of math nodes括号分隔符和两个用于左文本和右文本的标签来绘制此方案:

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{positioning, matrix}

\begin{document}
\begin{tikzpicture}[%
    ball/.style={circle, minimum size=6mm, inner sep=0pt, draw},
    vector/.style={%
        matrix of math nodes, inner sep=0pt,
        row sep=2pt, nodes={ball, anchor=center}, 
        left delimiter=\{, right delimiter =\}}
]

\matrix[vector, 
    label={[label distance=3mm]left:{$v$}}, 
    label={[label distance=3mm]right:{$g(v)$}}] (v1) 
    {w_1\\w_2\\w_3\\};

\matrix[vector, 
    label={[label distance=3mm]left:{$\mathcal{K}=U*V*W$}}, 
    label={[label distance=3mm]right:{$g(v)$}}, below=3mm of v1] (v2) 
    {u_1\\u_2\\u_3\\};
\end{tikzpicture}
\end{document}

enter image description here

答案3

感谢您添加草图。我认为您想用 Ti 绘制括号Z 方法。除此之外,您拥有的看起来像一个方程式,您可以使用它align来对齐东西。我把它留在了图形环境中。

\documentclass[a4paper]{article}
\usepackage{tikz}
\usepackage{amsmath}
\usetikzlibrary{positioning, decorations.pathreplacing}
\tikzset{pics/.cd,
multidot/.style n args={2}{code={
\foreach \X in {1,...,#1}
{
\node[circle,draw,minimum size=\nodesize] (aux-\X) at (0,{(#1/2+1/2-\X)*9mm}){$#2_{\X}$};}
\draw[thick,decorate,decoration={brace,raise=5pt,amplitude=3mm}] (aux-1.north east) -- (aux-#1.south
east);
\draw[thick,decorate,decoration={brace,raise=5pt,amplitude=3mm}] (aux-#1.south west) -- (aux-1.north
west);
}}}
\begin{document} 


\def\nodesize{7mm}


\begin{figure}[h]
\begin{align*}
v&\,\vcenter{\hbox{\tikz{\pic{multidot={3}{w}}}}}\,g(v)\\
\mathcal{K}=U*V*W&\,\vcenter{\hbox{\tikz{\pic{multidot={3}{u}}}}}\,g(v)
\end{align*}
\end{figure}

\end{document}

enter image description here

答案4

我忍不住用两行代码来实现它:

\documentclass[a4paper]{article}
\usepackage{tikz,amsmath}
\begin{document} 

\newcommand{\wcir}[1]{\tikz{\node[draw,circle,minimum size=6mm,inner sep=0]{$#1$};}}

\begin{align*}
v &\begin{Bmatrix}\wcir{w_1}\\\wcir{w_2}\\\wcir{w_3}\end{Bmatrix} g(v) \\
\mathcal{K} = U*V*W &\begin{Bmatrix}\wcir{w_1}\\\wcir{w_2}\\\wcir{w_3}\end{Bmatrix} g(v)
\end{align*}

\end{document}

enter image description here

相关内容