4

I am designing memory layout of primitive data types such as int showing 8 bits. Below is the code snippet-

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix, positioning, calc}

\begin{document}
\begin{tikzpicture}[node distance=0mm,every node/.style={inner sep=1mm, font=\tiny}]  
    \node [draw, matrix] (a)
    {
        \foreach \x in {0,1,...,7} {\fill[black] (\x mm, 0.0) circle (1pt);}\\
    };

    \node [draw, matrix,right=of a](b)
    {
        \foreach \x in {0,1,...,7} {\fill[black] (\x mm, 0.0) circle (1pt);}\\
    };

    \node [draw, matrix,right=of b](c)
    {
        \foreach \x in {0,1,...,7} {\fill[black] (\x mm, 0.0) circle (1pt);}\\
    };

    \node [draw, matrix,right=of c](d)
    {
        \foreach \x in {0,1,...,7} {\fill[black] (\x mm, 0.0) circle (1pt);}\\
    };

    \node[below=of $(a)!0.5!(d)$, yshift=-1mm] (plus) {label goes here};
\end{tikzpicture}
\end{document}

This is how generated diagram looks- enter image description here

Is it possible to shorten the code? For example, just by encapsulating few nodes, I think it can be done. Any leads, please?

ravi
  • 1,618

3 Answers3

7

enter image description here

in the case, that all block with dots are equal you can repeat blocks defined as \pic:

\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{fit,
                positioning}

\begin{document}
    \begin{tikzpicture}[
node distance = 1mm and 0mm,
   dot/.style = {circle, fill, inner sep=1pt, right=1mm},
  memory/.pic = {\foreach \x in {1,...,8} {\node (n\x) [dot] at (\x/5,0) {};}
                 \node (f) [draw,
                            inner xsep=3mm, inner ysep=2mm,
                            outer sep=0mm, fit=(n1) (n8)] {};
                 \coordinate (-e) at (f.east);
                 \coordinate (-s) at (f.south);
                 }
                        ]
\pic (a) {memory};
\pic[right=of a-e] (b) {memory};
\pic[right=of b-e] (c) {memory};
\pic[right=of c-e] (d) {memory};
%
\node[below=of b-s -| b-e] (plus) {label goes here};
    \end{tikzpicture}
\end{document}
Zarko
  • 296,517
  • Awesome! I wasn't aware of \pic. It seems a nice way to utilize \pic. Thank you very much! – ravi May 09 '18 at 03:22
6

Just for fun, one example from the manual (text effects along path) that is slightly adapted here :

\documentclass[tikz,border=7pt]{standalone}
\usetikzlibrary{decorations.text}
\begin{document}
  \begin{tikzpicture}
    \path [
      decoration={
        text effects along path,
        text={~~~~~~~~ ~~~~~~~~ ~~~~~~~~},
        text effects/.cd,
        path from text,
        every letter/.style={shape=rectangle, fill=blue!20, draw=blue!40,
          minimum size=7mm, label={center:$\bullet$}}
      },
      decorate,
      local bounding box=memory
    ] (0,0);
    \node[below,scale=2] at (memory.south) {label goes here};
  \end{tikzpicture}
\end{document}

enter image description here

Kpym
  • 23,002
  • 3
    Are you sure you want to do \def~{$\bullet$}? ~ is after all used for non-breaking spaces. An alternative could be to remove that definition, and then add label={[circle,fill=black,inner sep=0pt,minimum size=4pt]center:} to the every letter style. – Torbjørn T. May 08 '18 at 09:08
  • @TorbjørnT. You are right : I replaced the \def by label. – Kpym May 08 '18 at 09:47
  • Well, this is why I love tikz. Every time, I learn something new about it. Thanks a lot. – ravi May 09 '18 at 03:20
6

This one uses only one node and its corresponding label.

The node is a rectangle split node (see shapes.multipart library). Each part contents is built with \mydots command. This command has an optional parameter with 8 as default value. This parameter fixes the amount of bullets. As \bullet is a binary operator and this provoques an spacing problem with an even number of bullets, it's converted to ordinary operator with \mathop. Finally the label for the node is drawn with label option.

\documentclass[tikz,border=2mm]{standalone} 
\usetikzlibrary{shapes.multipart}

\newcommand{\mydots}[1][8]{$\foreach\i in {1,...,#1}{\mathop{\bullet}}$}

\begin{document}
\begin{tikzpicture}
\node[rectangle split,
    rectangle split horizontal, draw, font=\small,
    inner xsep=2mm,
    label=below:label goes here]{%
    \mydots\nodepart{two}\mydots
    \nodepart{three}\mydots\nodepart{four}\mydots};
\end{tikzpicture}
\end{document}

enter image description here

Ignasi
  • 136,588
  • Interestingly this is the shortest code! I am still having a hard time to get it completely. Could you please, describe it by editing your answer? I sincerely acknowledge your efforts. – ravi May 09 '18 at 03:24
  • @RaviJoshi Done! – Ignasi May 09 '18 at 08:38