将小页面内容置于 Tikz 图形节点的中心

将小页面内容置于 Tikz 图形节点的中心

感谢回答这个问题,我有以下 Tikz 图的代码,每个节点都有列表:

\documentclass{article}

\usepackage{tikz}
\usepackage{enumitem}
\usetikzlibrary{positioning}

\begin{document}
    \definecolor{BulletsColor}{rgb}{0, 0, 0.9}
    \newlist{myBullets}{itemize}{1}
    
    \setlist[myBullets]{
      label=\textcolor{BulletsColor}{\textbullet},
      leftmargin=*,
      topsep=0ex,
      partopsep=0ex,
      parsep=0ex,
      itemsep=0ex,
      before={\color{BulletsColor}}
    }

    \begin{tikzpicture}
    
      \node[draw, rounded corners] (dataTypes)  {
        \begin{minipage}{4.5cm} Data types
          \begin{myBullets}
            \item a
            \item b
          \end{myBullets}
        \end{minipage}
      };
    
      \node[draw, rounded corners] (signalTypes) [above=of dataTypes] {
        \begin{minipage}{4.5cm} Signal types
            \begin{myBullets}
              \item x
              \item y
            \end{myBullets}
        \end{minipage}
        };
    
    \draw[->, thick] (signalTypes.south) --(dataTypes);
    
    \end{tikzpicture}
\end{document}

现在,我想将列表内容和每个节点的标题置于中心,但不幸的是我不知道如何进行。

答案1

将每个节点的标题居中很容易。但是,您无法(真正)将itemizeorenumerate环境居中。它们会创建列表,并且它们不打算居中。您可能希望在 stackoverflow 上查阅类似问题的答案以了解更多详细信息:https://stackoverflow.com/a/59551912/14169367

您可以实现以下目标:

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,fit}

\begin{document}
    \begin{tikzpicture}
       [node distance=.5cm,
       typelist/.style={anchor=north, text width=3.5cm, yshift=.5cm, blue, font=\small}
       ]
        \node[] (st) at (0,0) {Signal types};
        \node[below= of st,typelist,align=center] (stlist) {
            $\circ$ x\\
            $\circ$ y
        };
        
        \node[below= of stlist,yshift=-1cm] (dt) {Data types};
        \node[below= of dt,typelist,align=center] (dtlist) {
            $\circ$ x\\
            $\circ$ y
        };
        
        \node[fit=(st) (stlist),rounded corners,draw,thick](stbox) {};
        \node[fit=(dt) (dtlist),rounded corners,draw,thick](dtbox) {};
        \draw[thick,->,>=stealth] (stbox) -- (dtbox);
    \end{tikzpicture}
\end{document}

看起来像这样:

在此处输入图片描述

编辑:为了确保较长的文本正确对齐,您可以node为每个元素创建一个。顺便说一句:这不是一个非常优雅的解决方案,但我想它足以作为一个起点。

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,fit}

\begin{document}
    \begin{tikzpicture}
        [node distance=.5cm,
        typelist/.style={anchor=north, text width=3.5cm, blue, font=\small}
        ]
        \node[] (st) at (0,0) {Signal types};
        \begin{scope}[node distance=0cm]
            \node[below= of st,typelist,align=center] (stlist1) {$\circ$ x};
            \node[below= of stlist1,typelist,align=center] (stlist2) {$\circ$ Some very long text too check the alignment};
        \end{scope}
        \node[fit=(st) (stlist2),rounded corners,draw,thick](stbox) {};
        
        \node[below= of stbox,yshift=-1cm] (dt) {Data types};
        \begin{scope}[node distance=0cm]
            \node[below= of dt,typelist,align=center] (dtlist1) {$\bullet$ x};
            \node[below= of dtlist1,typelist,align=center] (dtlist2) {$\bullet$ Some very long text too check the alignment};
        \end{scope}
        \node[fit=(dt) (dtlist2),rounded corners,draw,thick](dtbox) {};
        \draw[thick,->,>=stealth] (stbox) -- (dtbox);
    \end{tikzpicture}
\end{document}

此代码产生如下结果:

在此处输入图片描述

编辑2:如果您希望项目在左侧对齐,那么我猜您又回到原点了itemize。我draw在顶部框中添加了一个命令,以可视化此环境实际上是居中的,即使乍一看它看起来并不像这样。您可以使用环境稍微改善此行为varwidth

\documentclass[tikz]{standalone}
\usetikzlibrary{positioning,fit}
\usepackage{enumitem}
\usepackage{varwidth}

\begin{document}
    \begin{tikzpicture}
        [node distance=.5cm,
        typelist/.style={anchor=north, text width=3.5cm, blue, font=\small,yshift=.5cm}
        ]
        \node[] (st) at (0,0) {Signal types};
            \node[below= of st,typelist,align=center,draw] (stlist) {
                \begin{varwidth}{\textwidth}
                    \begin{itemize}[leftmargin=*]
                        \item x
                        \item Some very long text to check the alignment
                    \end{itemize}
                \end{varwidth}
            };
        \node[fit=(st) (stlist),rounded corners,draw,thick](stbox) {};
        
        \node[below= of stbox,yshift=-.5cm] (dt) {Data types};
            \node[below= of dt,typelist,align=center] (dtlist) {
                \begin{varwidth}{\textwidth}
                    \begin{itemize}[leftmargin=*]
                        \item x
                        \item Some very long text to check the alignment
                    \end{itemize}
                \end{varwidth}
            };
        \node[fit=(dt) (dtlist),rounded corners,draw,thick](dtbox) {};
        \draw[thick,->,>=stealth] (stbox) -- (dtbox);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容