2

I want to draw multiple lines caption that is connected to a line. I observe that line may overwrite to second line.

Base figure example would be right hand side of the following figure (taken from https://en.wikipedia.org/wiki/Introduction_to_Algorithms second edition, page 223):

enter image description here


my code: (it generates an error but able to compile)

\documentclass[tikz, margin=3.14mm]{standalone}
\usepackage{siunitx}
\usepackage{tikzpeople}
\usetikzlibrary{positioning,calc,arrows.meta,arrows,matrix}
\usetikzlibrary{decorations.pathreplacing}

\tikzset{ mymat/.style={ matrix of math nodes, minimum width=1cm, minimum height=0.5cm, text height=2.5ex, text depth=0.75ex, text width=3.25ex, align=center, row sep=-\pgflinewidth, outer sep=0pt, inner sep=0pt }, mymat3/.style={ matrix of math nodes, minimum width=0.5cm, minimum height=0.5cm, text height=2.5ex, text depth=0.75ex, text width=3.25ex, align=center, row sep=-\pgflinewidth, outer sep=0pt, inner sep=0pt } } \begin{document}

\begin{figure} \centering

\begin{tikzpicture}
  \matrix[mymat,anchor=south west,style={nodes=draw}]
  at (5*\x+0.3,-3.6)
  (mat1)
  {
    400\\
    100\\
  };

  \draw (mat1-1-1.north west) --++ (0,0.8);
  \draw (mat1-1-1.north east) --++ (0,0.8);

  \matrix[mymat3,right=25mm of mat1-1-1.east,anchor=east,style={nodes={draw,fill=gray!30}}]
  (mat2)
  {
    1 & 2 & \dots\\
  };

  \matrix[mymat3,below=0.5 mm of mat2,style={nodes={draw,fill=gray!30}}]
  (mat5)
  {
    1 & 2 & \dots\\
  };

  \path[->, -Latex]
  (mat1-2-1.east) edge[] node [left] {} (mat5-1-1.west);
  \path[->, -Latex]
  (mat1-1-1.east) edge[] node [left] {} (mat2-1-1.west);

  \foreach \i [count=\l from 0] in {2,...,1} \node[left= 0.1mm of mat1-\i-1] {\l};
  \draw (mat2-1-1.north) --++ (0,0.5) node[align=center] {\tiny available\\\tiny life};
  \draw (mat2-1-2.north) --++ (0,0.5) node[align=center] {\tiny core\\\tiny number};
\end{tikzpicture}
\caption{M1}\label{fig:M1}

\end{figure} \end{document}

Output:

enter image description here

Here, I have done draw (mat2-1-1.north) --++ (0,0.5) node[align=center] {\tiny available\\\tiny life}; to link caption along with a line but the line is linked to the first sentences where it overwrites to text after the new line (life and number strings in the example). And also there is a gap in between the lines that I couldn't remove.

Desired picuture could be, or any variaty that captions won't collide :

enter image description here

Related answers:

alper
  • 1,389

1 Answers1

4

Improvements had to be made here and there. You still need to learn some TikZ basics to produce a straight code. You're on the good way ;) Hope that helps.

matrices of nodes with captions

\documentclass[12pt]{article}
\usepackage{tikz}
\usepackage{siunitx}
\usepackage{tikzpeople}
\usetikzlibrary{positioning,calc,arrows.meta,arrows,matrix}
\usetikzlibrary{decorations.pathreplacing}

\tikzset{ mymat/.style={ matrix of math nodes, minimum width=1cm, minimum height=0.5cm, text height=2.5ex, text depth=0.75ex, text width=3.25ex, align=center, row sep=-\pgflinewidth, outer sep=0pt, inner sep=0pt }, mymat3/.style={ matrix of math nodes, minimum width=0.5cm, minimum height=0.5cm, text height=2.5ex, text depth=0.75ex, text width=3.25ex, align=center, row sep=-\pgflinewidth, outer sep=0pt, inner sep=0pt } } \begin{document}

\begin{figure} \centering

\begin{tikzpicture}
  \matrix[mymat,anchor=south west,style={nodes=draw}]
  (mat1) at (0.3,-3.6)
    {
    400\\
    100\\
  };

  \draw (mat1-1-1.north west) --++ (0,0.8);
  \draw (mat1-1-1.north east) --++ (0,0.8);

  \matrix[mymat3,right=8mm of mat1-2-1.south east,anchor=south west,style={nodes={draw,fill=gray!30}}]
  (mat2)
  {
    1 & 2 & \dots\\
    1 & 2 & \dots\\
  };


  \draw[-Latex]
  (mat1-1-1.east) -- (mat2-1-1.west);
  \draw[-Latex]
  (mat1-2-1.east) -- (mat2-2-1.west);

  \foreach \i [count=\l from 0] in {2,...,1} \node[left= 0.1mm of mat1-\i-1] {\l};

  \tikzset{
          lblnode/.style={
            text width=1cm,
            align=center,
            anchor=south,
            inner sep=1pt,
            execute at begin node=\setlength{\baselineskip}{3pt}
            }
           }
  \draw (mat2-1-1.north) --++ (-0.25,0.5) node[lblnode] {\tiny available life};
  \draw (mat2-1-2.north) --++ (0.25,0.5) node[lblnode] {\tiny core number};
\end{tikzpicture}
\caption{M1}\label{fig:M1}

\end{figure} \end{document}

EDIT

Following OP's question about row and column sep, here are some examples of how to modify them.

1. No gap, no bold line

row sep=-\pgflinewidth,
column sep=-\pgflinewidth,

no gap, no bold lines

2. Separated rows

row sep=2pt,
column sep=-\pgflinewidth,

separated rows

3. Separated columns

row sep=-\pgflinewidth,
column sep=2pt,

separated columns

SebGlav
  • 19,186
  • Sorry for asking too many questions, and appreciated your help :) // Is it normal that middle line in between the boxes is double bolded (ex: line in between 1 and 2)? is it possible to make the line normal bold line ? – alper Apr 10 '21 at 22:06
  • Could there be a small space in between the vertical gray arrays? I have modified minimum height but it didn't have any affect – alper Apr 10 '21 at 22:22
  • Just add column sep=-\pgflinewidth, to your matrix declaration, like you did for the rows, and change row sep to something positive. I'll edit my post to let you choose. – SebGlav Apr 11 '21 at 09:32
  • Thanks, I was also able to fix it via changing heighs for mat3: minimum height=0.3cm, and text height=2.1ex, but not sure would it be a proper solution – alper Apr 11 '21 at 15:07
  • Where should I add row sep=-\pgflinewidth, column sep=-\pgflinewidth, at which line in your original answer code? that would be helpful – alper Apr 11 '21 at 15:09
  • You should do that in your declaration of mymat3/.style. Here, you only have row sep=-\pgflinewidth. Play here with those settings. – SebGlav Apr 11 '21 at 15:27