7

I tried to draw two graphs with Tikz. How can I align them vertically to each other? I want the knots to form the center, such that all six nodes align vertically. Furthermore I want to align the curly brackets using the nodes. But that does not work either. I want the parentheses to start at the beginning of the first node and end at the end of the last node. How can I do that?

This is what I get:

enter image description here

This is what I would like to have:

enter image description here

This code produces the picture above:

\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}
6
  • why you not merge both images in one tikzpicture?
    – Zarko
    Dec 14, 2018 at 9:13
  • Could you perhaps add a sketch that illustrates what you want?
    – user121799
    Dec 14, 2018 at 9:25
  • @Zarko I tried that, but it made things worse. I am not an expert.
    – Gilfoyle
    Dec 14, 2018 at 9:28
  • @marmot I added a sketch.
    – Gilfoyle
    Dec 14, 2018 at 9:35
  • @Zarko I added a sketch to show how I want to align the curly brackets using the nodes.
    – Gilfoyle
    Dec 14, 2018 at 9:41

5 Answers 5

9

as i mentioned, merging bot tikzpictures in one enable better controlling their elements positions:

edit: position of braces are corrected acoording to new explanation in question

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

in above mwe are:

  • used recent syntax for defining nodes and other elements style (\tikzstyle is obsolete, you should use \tizset or as is used in mwe as options of tikzpicture)
  • libraries should be loaded in document preamble
  • never use names as node , which is already used as one of key element of the tikz
  • it is sensible to define the common style for braces

result is:

enter image description here

2
  • Are your four remarks already considered in your example? Since I'm not an expert, I find it hard to see that for myself.
    – Gilfoyle
    Dec 14, 2018 at 17:39
  • @Samuel, yes. i have considered. compare your mwe and mine. you will observe difference. anyway, i will add comments to my code, which will emphasize those remarks (asap).
    – Zarko
    Dec 14, 2018 at 17:45
6

You can draw this scheme with two matrix of math nodes delimited by braces and two labels for left and right text:

\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

4

Thanks for adding a sketch. I take that you want to draw the braces with TikZ methods. Other than that, what you have looks like an equation, where you can use align to aliggn stuff. I left it in a figure environment.

\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

3

Edit: Some corrections after the clarification of the OP.

\documentclass{article}
\usepackage{tikz}

\usetikzlibrary{chains, positioning, decorations.pathreplacing}
\def\ilsize{3}
\def\nodesize{6mm}
\tikzset{>=latex}

\begin{document} 
\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=6pt,amplitude=3mm}, decorate]
    (In-1.north) -- (In-3.south) node[black,midway,right=3mm, xshift=5] {$g(v)$};
    \draw[-, decoration={brace,raise=6pt,amplitude=3mm, mirror}, decorate]
    (In-1.north) -- (In-3.south) node[black,midway,left=3mm, xshift=-5] {$v$};

    \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}
    \pgfmathparse{\y+3}
        \node[input node] (In-\name) at (0.0cm,-\pgfmathresult cm) {$u_{\y}$};

    % Labels %
    \draw[-, decoration={brace,raise=6pt,amplitude=3mm}, decorate]
    (In-1.north) -- (In-3.south) node[black,midway,right=3mm, xshift=5] {$g(v)$};

    \draw[-, decoration={brace, raise=6pt, amplitude=3mm, mirror}, decorate, xshift=0mm, yshift=0mm]
    (In-1.north) -- (In-3.south) node[black,midway,left=3mm, xshift=-5] {$\mathcal{K}=U*V*W$};
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

2
  • Much better! I added a sketch that shows what I meant with aligned brackets. Maybe you can add that too?
    – Gilfoyle
    Dec 14, 2018 at 9:37
  • @Samuel you already got better answers, but nevertheless I wanted to correct the code to suit the given task.
    – Sito
    Dec 14, 2018 at 11:43
1

I couldn't resist doing it in two lines of code:

\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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .