1

I have the following sample matrix implementation. I want to have the text centered in between the space.

I was able to get the text but at the corner like this: enter image description here

What is the best way to center the text? Also is there a better way to align the arrow instead of defining arbitary coordinates as I did in the following reproducible example?:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
               backgrounds,
               fit,
               matrix,
               positioning}
\usepackage{xcolor}

\begin{document}

\tikzset{
  arr/.style = {-{Triangle[scale=0.8]}, 
                draw=gray, ultra thick,
                shorten >=1mm, shorten <=1mm}, 
   en/.style = {draw=none, fill=none, text=black},
every label/.append style = {name=LN, anchor=south,     % <---
                font=\bfseries, text=black, 
                text width=10em, align=flush center,   % <---
                yshift=1ex},                           % <---
  FIT/.style = {draw=gray, fill=gray!30, rounded corners, 
                inner sep=1ex,
                fit=#1, node contents={}},
neuron/.style = {matrix of nodes,
                nodes={draw=pink, rounded corners, fill=gray!50, 
                       text height=2ex, minimum width=2em, 
                       inner sep=1mm,
                       text=#1},
                row sep=2mm,
                outer sep=0mm},
       }
 \begin{tikzpicture}[
node distance = 0mm and 1mm                            % <---
                       ]
%%%

\matrix (N2) [neuron=white,
             ]             % <---
{
|[label=Title]| a  \\
b    \\
d   \\
c     \\
};
%%%%
\matrix (N5) [neuron=white, right=-2.8cm of N2.east]
{
|[label= Title]| a      \\        % <--- longer label
b        \\
d   \\
c          \\
};
          
   % <---

 \scoped[on background layer]
   \node (f4) [FIT={(N2-1-1) (N5-4-1)},draw=gray, fill=gray!30,rounded corners, inner xsep=1.5ex,inner ysep=1.5ex]{}; 
\scoped[on background layer]\node (f3) [FIT=(N5-1-1) (N5-4-1)]; 
\scoped[on background layer]\node (f2) [FIT=(N2-1-1) (N2-4-1)];

\coordinate[below left= 1.0 and 1.1 of N5.south] (aux2);    % <---
\draw[arr]  (f3.south) -- ++ (0,-.1) |- (aux2) node[midway,fill=white] {\scriptsize{Text}}  -- (f2.south);
% \path[clip]
%     (f4.south west) |- (f4.north east) |- (f4.south east);
% \node at (f4) {\includegraphics[scale=.18]{example-image-duck}};

\end{tikzpicture}

\end{document}

1 Answer 1

1

A simple approach would be:

\draw[arr] (f3.south) |- ++(0,.-1) -| (f2.south) node[near start, fill=white] {\scriptsize{Text}};

The option near start means the same as pos=0.25 and since we use a |- path operation, this denotes exactly the middle between the start of the path (which is .1 below of (f3.south)) and the corner where the path turns upwards. Note the explanation in the TikZ manual:

Another interesting case are the horizontal/vertical line-to operations |- and -|. For them, the position (or time) 0.5 is exactly the corner point.


For exact positioning, you can also use the calc library to calculate the center between two nodes (or coordinates) in TikZ using ($(nodeA)!.5!(nodeB)$). You can use this approach to define a coordinate below the center of f2.south and f3.south and draw the arrow through this coordinate using |- and -|. You can then easily add the text node.

Another way would be to simply place the text node at the calculated coordinate and then draw the arrows to and from this node.

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
               backgrounds,
               fit,
               matrix,
               positioning,
               calc}
\usepackage{xcolor}

\begin{document}

\tikzset{
    arr/.style = {-{Triangle[scale=0.8]}, 
                draw=gray, ultra thick,
                shorten >=1mm, shorten <=1mm}, 
    en/.style = {draw=none, fill=none, text=black},
    every label/.append style = {name=LN, anchor=south,     % <---
                font=\bfseries, text=black, 
                text width=10em, align=flush center,   % <---
                yshift=1ex},                           % <---
    FIT/.style = {draw=gray, fill=gray!30, rounded corners, 
                inner sep=1ex,
                fit=#1, node contents={}}, 
    neuron/.style = {matrix of nodes,
                nodes={draw=pink, rounded corners, fill=gray!50, 
                       text height=2ex, minimum width=2em, 
                       inner sep=1mm,
                       text=#1},
                row sep=2mm,
                outer sep=0mm},
    }
\begin{tikzpicture}[
    node distance = 0mm and 1mm                            % <---
    ]
%%%

\matrix (N2) [
    neuron=white,
    ]             % <---
{
|[label=Title]| a  \\
b    \\
d   \\
c     \\
};
%%%%
\matrix (N5) [neuron=white, right=-2.8cm of N2.east]
{
|[label= Title]| a      \\        % <--- longer label
b        \\
d   \\
c          \\
}; 
   % <---

\scoped[on background layer]
   \node (f4) [FIT={(N2-1-1) (N5-4-1)},draw=gray, fill=gray!30,rounded corners, inner xsep=1.5ex,inner ysep=1.5ex]{}; 
\scoped[on background layer]\node (f3) [FIT=(N5-1-1) (N5-4-1)]; 
\scoped[on background layer]\node (f2) [FIT=(N2-1-1) (N2-4-1)];

\coordinate (aux) at ([yshift=-.5cm]$(f3.south)!.5!(f2.south)$);
\draw[arr]  (f3.south) |- (aux)
node[fill=white] {\scriptsize{Text}} -| (f2.south);

\end{tikzpicture}

\end{document}

enter image description here

1
  • This works perfect! Thanks for the explanation!
    – user0193
    Jul 19 at 13:13

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.