1

I have written a small tikz snippet that displays the diagram as I expect

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{positioning}
\begin{document}

\begin{tikzpicture}[node distance=.15cm,>=latex]

\tikzstyle{myarr} = [draw, inner sep=5pt, fill=black!20, single arrow,single arrow head extend=0.1cm]
\tikzstyle{mylbl} = [inner sep=0pt, color=blue!70, text centered, text width=2cm]
\tikzstyle{mybox} = [draw, inner sep=5pt, text centered]

\node[mybox, text depth=2cm, text width=2cm] (corpus) at (0,0) {\textbf{Corpus}};
\node[myarr, single arrow head extend=0cm, single arrow tip angle=130, right=-1cm of corpus] (transformation) {Transformation};

% Is possible to resize the node to fit the whole content?
\node[mybox, text depth=1.75cm, text width=8.5cm, right=-1cm of transformation]  (embedding)  {\textbf{Embedding method}};

% UGLY HACK 1
\node[myarr ,single arrow head extend=0cm,single arrow tip angle=130, rotate=0, right=-1cm of corpus] (transformationhack) {Transformation};

\node[mylbl, right=of transformation] (sentence) {``I think ??? I am''};
\node[myarr, right=of sentence] (arrowa) {};
\node[mybox, minimum height=7ex, right=of arrowa] (cbow) {\textbf{CBOW}};
\node[myarr, right=of cbow] (arrowb) {};
\node[mylbl, right=of arrowb] (predict) {``therefore''};

% Why do I need a larger spacing here?
\node[myarr, rotate=-90, below=.35cm of embedding] (arrowc) {};

% east?
\node[mybox, text width=4cm, text depth=2.5cm, below=of arrowc.east] (embeddings) {\textbf{Word embeddings}};

% How to center this at center of (embeddings) without hand picking.
\node[] at (6.3,-3.80) (matrix) {%
$\begin{bmatrix}
0.12 & 0.59 & \cdots & 0.33 \\
0.48 & 0.02 & \cdots & 0.29 \\
\vdots & \vdots & \ddots & \vdots \\
0.32 & 0.74 & \cdots & 0.61
\end{bmatrix}$
};

\end{tikzpicture}
\end{document}

As shown in image below

diagram

But I'm still concerned about some "techniques" I have used to achieve these results. Hence I came here looking for ideas in how to improve the code (and design) quality.

First, since I want the "transform" arrow over the boxes I draw it twice, first time to work as a label to the second box (embedding) and the second to draw over the boxes. I didn't feel confortable with this approach. I labeled as UGLY HACK.

Second, I tried to break the node (Sentence) after the word ``think'' using the \\, but didn't work, to "fix" that, I have forced a text width of given size.

Third, If I add spacing, change text, or anything in the contents "inside" the (embedding) node, I need resize it. I know it makes sense, but I wish to know if there is a way to say: look, these objects are inside you, enlarge to fill, respect the inner separator, be elegant.

Fourth, why do I need a larger spacing in (arrowc), why it wont respect the global .15cm distance?

Fifth, why the (embeddings) [I know two box with very similar names, sorry] box needs to be is anchored to (arrowc.east) to correct placement, why not (arrowc.south)?

And lastly, how can I anchor my (matrix) node to the "center=of embeddings.center", instead to manually choose the position?

Best regards

PS: Of course, any overall improvements are always welcome.

1
  • would you like to accept and upvote the answer if it met your requirements
    – js bibra
    Jun 12, 2021 at 9:26

1 Answer 1

2

For the ugly hack --simply draw your arrow after both boxes are drawn -- that way the arrow will cover the border of both boxes

enter image description here

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{positioning}
\begin{document}
    
    \begin{tikzpicture}[node distance=.15cm,>=latex]
        
        \tikzstyle{myarr} = [draw, inner sep=5pt, fill=black!20, single arrow,single arrow head extend=0.1cm]
        \tikzstyle{mylbl} = [inner sep=0pt, color=blue!70, text centered, text width=2cm]
        \tikzstyle{mybox} = [draw, inner sep=5pt, text centered]
        
        \node[mybox, text depth=2cm, text width=2cm] (corpus) at (0,0) {\textbf{Corpus}};
        
        % Is possible to resize the node to fit the whole content?
        \node[mybox, text depth=1.75cm, text width=8.5cm, right=1cm of corpus]  (embedding)  {\textbf{Embedding method}};
        \node[myarr, single arrow head extend=0cm, single arrow tip angle=130, right=-1cm of corpus] (transformation) {Transformation};
        
        % UGLY HACK 1
%       \node[myarr ,single arrow head extend=0cm,single arrow tip angle=130, rotate=0, right=-1cm of corpus] (transformationhack) {Transformation};
        
        \node[mylbl, right=of transformation] (sentence) {``I think ??? I am''};
        \node[myarr, right=of sentence] (arrowa) {};
        \node[mybox, minimum height=7ex, right=of arrowa] (cbow) {\textbf{CBOW}};
        \node[myarr, right=of cbow] (arrowb) {};
        \node[mylbl, right=of arrowb] (predict) {``therefore''};
        
        % Why do I need a larger spacing here?
        \node[myarr, rotate=-90, below=.35cm of embedding] (arrowc) {};
        
        % east?
        \node[mybox, text width=4cm, text depth=2.5cm, below=of arrowc.east] (embeddings) {\textbf{Word embeddings}};
        
        % How to center this at center of (embeddings) without hand picking.
        \node[] at (6.3,-3.80) (matrix) {%
            $\begin{bmatrix}
                0.12 & 0.59 & \cdots & 0.33 \\
                0.48 & 0.02 & \cdots & 0.29 \\
                \vdots & \vdots & \ddots & \vdots \\
                0.32 & 0.74 & \cdots & 0.61
            \end{bmatrix}$
        };
        
    \end{tikzpicture}
\end{document}

EDIT --for the sentence break

Simply use align=left in the definition of label box as below

    \tikzstyle{mylbl} = [inner sep=0pt, color=blue!70, align=left ] 

Now you can use \\ to break the sentence-- off course the alignment will be off since there is a " in the first line but not in the second line so you can add a phantom " in the second line which will not be printed but will improve the alignment

    \node[mylbl, right=of transformation] (sentence) {``I think \\ \phantom{"}I am''};

enter image description here

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{positioning}
\begin{document}
    
    \begin{tikzpicture}[node distance=.15cm,>=latex]
        
        \tikzstyle{myarr} = [draw, inner sep=5pt, fill=black!20, single arrow,single arrow head extend=0.1cm]
        \tikzstyle{mylbl} = [inner sep=0pt, color=blue!70, align=left ]
        \tikzstyle{mybox} = [draw, inner sep=5pt, text centered]
        
        \node[mybox, text depth=2cm, text width=2cm] (corpus) at (0,0) {\textbf{Corpus}};
        
        % Is possible to resize the node to fit the whole content?
        \node[mybox, text depth=1.75cm, text width=8.5cm, right=1cm of corpus]  (embedding)  {\textbf{Embedding method}};
        \node[myarr, single arrow head extend=0cm, single arrow tip angle=130, right=-1cm of corpus] (transformation) {Transformation};
        
        % UGLY HACK 1
%       \node[myarr ,single arrow head extend=0cm,single arrow tip angle=130, rotate=0, right=-1cm of corpus] (transformationhack) {Transformation};
        
        \node[mylbl, right=of transformation] (sentence) {``I think \\ \phantom{"}I am''};
        \node[myarr, right=of sentence] (arrowa) {};
        \node[mybox, minimum height=7ex, right=of arrowa] (cbow) {\textbf{CBOW}};
        \node[myarr, right=of cbow] (arrowb) {};
        \node[mylbl, right=of arrowb] (predict) {``therefore''};
        
        % Why do I need a larger spacing here?
        \node[myarr, rotate=-90, below=.35cm of embedding] (arrowc) {};
        
        % east?
        \node[mybox, text width=4cm, text depth=2.5cm, below=of arrowc.east] (embeddings) {\textbf{Word embeddings}};
        
        % How to center this at center of (embeddings) without hand picking.
        \node[] at (6.3,-3.80) (matrix) {%
            $\begin{bmatrix}
                0.12 & 0.59 & \cdots & 0.33 \\
                0.48 & 0.02 & \cdots & 0.29 \\
                \vdots & \vdots & \ddots & \vdots \\
                0.32 & 0.74 & \cdots & 0.61
            \end{bmatrix}$
        };
        
    \end{tikzpicture}
\end{document}

EDIT2 -- for the enlarge node

Use the tikzlibrary{fit} as below

   \node[draw=teal, dashed,ultra thick,  fit=(sentence) (arrowa)(arrowb) (cbow)(predict)] {};

This will auto enlarge if you add anything in the nodes inside

enter image description here

EDIT -- for arrowc.east

Anchors also get rotated when you rotate the arrow so east takes the place of south anchor when rotated -90

For the placement of matrix -- I again suggest fit library so that it can expand with the matrix

enter image description here

Your problems are related to choosing nodes from where to position is all

Complete MWE

\documentclass{standalone}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{shapes.arrows}
\usetikzlibrary{positioning,fit}
\begin{document}
    
    \begin{tikzpicture}[node distance=.15cm,>=latex]
        
        \tikzstyle{myarr} = [draw, inner sep=5pt, fill=black!20, single arrow,single arrow head extend=0.1cm]
        \tikzstyle{mylbl} = [inner sep=0pt, color=blue!70, align=left ]
        \tikzstyle{mybox} = [draw, inner sep=5pt, text centered]
        
        \node[mybox, text depth=2cm, text width=2cm] (corpus) at (0,0) {\textbf{Corpus}};
        
        % Is possible to resize the node to fit the whole content?
        \node[mybox, draw=none, text depth=1.75cm, text width=8.5cm, right=1cm of corpus]  (embedding)  {\textbf{Embedding method}};
        \node[myarr, single arrow head extend=0cm, single arrow tip angle=130, right=-1cm of corpus] (transformation) {Transformation};
        
        % UGLY HACK 1
%       \node[myarr ,single arrow head extend=0cm,single arrow tip angle=130, rotate=0, right=-1cm of corpus] (transformationhack) {Transformation};
        
        \node[mylbl, right=of transformation] (sentence) {``I think \\ \phantom{"}I am''};
        \node[myarr, right=of sentence] (arrowa) {};
        \node[mybox, minimum height=7ex, right=of arrowa] (cbow) {\textbf{CBOW}};
        \node[myarr, right=of cbow] (arrowb) {};
        \node[mylbl, right=of arrowb] (predict) {``therefore''};
        
        % Why do I need a larger spacing here?
        \node[myarr, rotate=-90, below=of embedding] (arrowc) {};
        
        % east?
        \node[mybox, draw=none,  below=of arrowc.east] (embeddings) {\textbf{Word embeddings}};
        
        % How to center this at center of (embeddings) without hand picking.
        \node[below=2cm of embedding]  (matrix) {%
            $\begin{bmatrix}
                0.12 & 0.59 & \cdots & 0.33 \\
                0.48 & 0.02 & \cdots & 0.29 \\
                \vdots & \vdots & \ddots & \vdots \\
                0.32 & 0.74 & \cdots & 0.61
            \end{bmatrix}$
        };
        \node[draw=teal, dashed,ultra thick,  fit=(matrix)(embeddings)] {};
        \node[draw=teal, dashed,ultra thick,  fit=(sentence) (arrowa)(arrowb) (cbow)(predict)] {};
    \end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.