如何设置矩形分割成相同的大小(特别是宽度)?

如何设置矩形分割成相同的大小(特别是宽度)?

我正在使用矩形分割来绘制链接节点:

在此处输入图片描述

如何确保每个矩形都具有相同的大小,特别是相同的宽度!无论它是空的还是一位数字或两位数字(最多两位数字,或像“-1”)

MWE(我的代码):

\documentclass[border=10pt]{standalone}
\usepackage{tikz, tkz-graph}
\usetikzlibrary{arrows,positioning}
\thispagestyle{empty}
\usepackage{memorygraphs}
\tikzset{
    phead/.style={
           rectangle, draw, text height=3mm, text width=3mm,
       text centered, node distance=3cm, inner sep=0pt
    },
    block/.style={
            draw,
            %text height=height("I"),
            %text depth=depth("I"),
            anchor=mid,
            %inner sep=0pt,
            %minimum width=0.6cm,
            every block,
    },
    arity/.style={
        block,
        rectangle split,
        rectangle split parts=#1+1,
        rectangle split horizontal,
        rectangle split every empty part={},
        rectangle split empty part width=0.6cm,
        minimum size=0.6cm,
    }   
}
\begin{document}
\begin{tikzpicture}[memory graph]

\node[phead, label=above:head] (head) {};

\def\prevname{head}
\foreach \name/\labela/\val in {1/7/,2/7/,3//,4/12/,5/13/,6/0/-1}{
    \ifnum \name=1
        \node[block,arity=1,right=of \prevname] (a\name) {\labela};
        \draw[ref] (\prevname.center) -- (a\name);
    \else
        \node[block,arity=1,right=of \prevname.arg 1 east] (a\name) {\labela};
        \draw[ref] (\prevname.arg 1 center) -- (a\name);
    \fi
    
    
    \xdef\prevname{a\name}
}

\end{tikzpicture}
\end{document}

你可以看到我在上面的代码中覆盖了一些内存图形的 tikz 样式,block比如arity

包的一些代码(样式)memeorygraphicshttps://ctan.org/pkg/memorygraphs

\tikzset{
    memory graph/.style={
        node distance=1.5em,
    },
    every block/.style={},
    block/.style={
        draw,
        text height=height("I"),
        text depth=depth("I"),
        anchor=mid,
        every block,
    },
    block/.append code={%
        \let\arg\memorygraphs@arg%
        \gdef\memorygraphs@arity{0}%
    },
    block mark north east/.style={
        append after command={
            \pgfextra
                \pgfinterruptpath
                \draw[#1]
                    ($(\tikzlastnode.head north east)-(\ifnum\memorygraphs@arity=0 .5\pgflinewidth\else0\fi,.5\pgflinewidth)$)
                    -- +(0,-\memorygraphs@marklength)
                    -- ($(\tikzlastnode.head north east)-(\ifnum\memorygraphs@arity=0 .5\pgflinewidth\else0\fi+\memorygraphs@marklength,.5\pgflinewidth)$)
                    -- cycle;
                \endpgfinterruptpath
            \endpgfextra
        },
    },
    block mark south east/.style={
        append after command={
            \pgfextra
                \pgfinterruptpath
                \draw[#1]
                    ($(\tikzlastnode.head south east)+(\ifnum\memorygraphs@arity=0 -.5\pgflinewidth\else0\fi,.5\pgflinewidth)$)
                    -- +(0,\memorygraphs@marklength)
                    -- ($(\tikzlastnode.head south east)+(\ifnum\memorygraphs@arity=0 -.5\pgflinewidth\else0\fi-\memorygraphs@marklength,.5\pgflinewidth)$)
                    -- cycle;
                \endpgfinterruptpath
            \endpgfextra
        },
    },
    block mark north west/.style={
        append after command={
            \pgfextra
                \pgfinterruptpath
                \draw[#1]
                    ($(\tikzlastnode.head north west)+(.5\pgflinewidth,-.5\pgflinewidth)$)
                    -- +(0,-\memorygraphs@marklength)
                    -- ($(\tikzlastnode.head north west)-(-.5\pgflinewidth-\memorygraphs@marklength,.5\pgflinewidth)$)
                    -- cycle;
                \endpgfinterruptpath
            \endpgfextra
        },
    },
    block mark south west/.style={
        append after command={
            \pgfextra
                \pgfinterruptpath
                \draw[#1]
                    ($(\tikzlastnode.head south west)+(.5\pgflinewidth,.5\pgflinewidth)$)
                    -- +(0,\memorygraphs@marklength)
                    -- ($(\tikzlastnode.head south west)+(.5\pgflinewidth+\memorygraphs@marklength,.5\pgflinewidth)$)
                    -- cycle;
                \endpgfinterruptpath
            \endpgfextra
        },
    },
    arity/.style={
        block,
        rectangle split,
        rectangle split parts=#1+1,
        rectangle split horizontal,
        rectangle split every empty part={},
        rectangle split empty part width=.2em,
    },
    arity/.append code={%
        \pgfmathsetcount\c@pgf@counta{\pgfkeysvalueof{/pgf/rectangle split parts}}%
        \ifnum\c@pgf@counta=0\else\advance\c@pgf@counta by-1 \fi
        \xdef\memorygraphs@arity{\the\c@pgf@counta}%
    },
  ref/.style={
        {Circle[length=3pt]}-Latex,
        shorten <=-1.5pt,
        rounded corners=.2em,
    },
}

答案1

与 @Peter Grill 的回答 (+1) 略有不同,使用了纯粹的tikz,即:不带memorygraphs包:

\documentclass[border=10pt]{standalone}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                chains,
                positioning,
                shapes.multipart}

\newlength\MinimumWidth
\setlength{\MinimumWidth}{\widthof{00}}
\tikzset{
arr/.style={{Circle[length=2pt 4, sep=0pt -4]}-Stealth},
arity/.style={
        rectangle split,
        rectangle split parts=#1+1,
        rectangle split horizontal,
        minimum height=10pt,
        rectangle split empty part width=\MinimumWidth-\pgfkeysvalueof{/pgf/inner xsep},
        text width=\MinimumWidth, align=center,
        execute at end node=\vphantom{fg},
        draw,
        on chain},
box/.style = {draw, text width=#1, minimum size=#1, inner sep=0pt},
box/.default = 4mm
        }
        
\begin{document}
    \begin{tikzpicture}[
node distance = 8mm and 7mm,
  start chain = on chain,
                        ]
\node[arity=0, box, label=head] (a0) {};
\foreach \labela/\val [count=\i from 0, count=\j] in {7/,7/,/,12/,13/,0/}%
{
\node[arity=1] (a\j) {\labela};
\draw[arr] (a\i.two north |- a\i.east) -- (a\j);
}

\node[arity=0, box, label=head,
      below=of a0] (b0) {};
\foreach \labela/\val [count=\i from 0, count=\j] in {17/,27/,/,2/,3/,0/}%
{
\node[arity=1] (b\j) {\labela};
\draw[arr] (b\i.two north |- b\i.east) -- (b\j);
}
\end{tikzpicture}
\end{document}

在此处输入图片描述

编辑: 删除了节点边框的颜色,现在所有节点的部分都有相同的宽度和高度。

答案2

定义所需宽度的长度。这里我将其设置为两位数的宽度00

\newlength\MinimumWidth
\setlength{\MinimumWidth}{\widthof{00}}

然后申请

text width=\MinimumWidth, align=center,

两种不同宽度的文本产生相同宽度的矩形:

在此处输入图片描述

代码:

\documentclass[border=10pt]{standalone}
\usepackage{tikz, tkz-graph}
\usetikzlibrary{arrows,positioning}
\thispagestyle{empty}
\usepackage{memorygraphs}
\usepackage{calc}

\pagecolor{white}

\newlength\MinimumWidth
\setlength{\MinimumWidth}{\widthof{00}}
\tikzset{
    phead/.style={
           rectangle, draw, text height=3mm, text width=3mm,
       text centered, node distance=3cm, inner sep=0pt
    },
    block/.style={
            draw,
            %text height=height("I"),
            %text depth=depth("I"),
            anchor=mid,
            %inner sep=0pt,
            %minimum width=0.6cm,
            every block,
    },
    arity/.style={
        block,
        rectangle split,
        rectangle split parts=#1+1,
        rectangle split horizontal,
        rectangle split every empty part={},
        rectangle split empty part width=0.6cm,
        minimum height=0.6cm,
        text width=\MinimumWidth, align=center,% <--- NOTE
    }   
}
\begin{document}
\begin{tikzpicture}[memory graph]

\node[phead, label=above:head] (head) {};

\def\prevname{head}
\foreach \name/\labela/\val in {1/7/,2/7/,3//,4/12/,5/13/,6/0/-1}{
    \ifnum \name=1
        \node[block,arity=1,right=of \prevname, red] (a\name) {\labela};
        \draw[ref] (\prevname.center) -- (a\name);
    \else
        \node[block,arity=1,right=of \prevname.arg 1 east, blue] (a\name) {\labela};
        \draw[ref] (\prevname.arg 1 center) -- (a\name);
    \fi
    \xdef\prevname{a\name}
}

\end{tikzpicture}
\begin{tikzpicture}[memory graph]

\node[phead, label=above:head] (head) {};

\def\prevname{head}
\foreach \name/\labela/\val in {1/17/,2/27/,3//,4/2/,5/3/,6/0/-1}{
    \ifnum \name=1
        \node[block,arity=1,right=of \prevname, red] (a\name) {\labela};
        \draw[ref] (\prevname.center) -- (a\name);
    \else
        \node[block,arity=1,right=of \prevname.arg 1 east, blue] (a\name) {\labela};
        \draw[ref] (\prevname.arg 1 center) -- (a\name);
    \fi
    \xdef\prevname{a\name}
}

\end{tikzpicture}
\end{document}

相关内容