在 Tikz 中对齐节点名称

在 Tikz 中对齐节点名称

我正在尝试将节点的名称与矩形的左上角对齐。 TikZ 中的左对齐(以及其他一些帖子)提到要改变参数text width,但是在下面的代码中使用时没有效果。

back group/.style={fill=yellow!20,rounded corners, draw=black!50, dashed, 
                   inner xsep=15pt, inner ysep=10pt, 
                   anchor=west, text width=8.8cm}

更多来自TIKZ-PGF:将两个文本节点左对齐 我尝试将下面代码的相关部分改为使用anchorright,但没有效果。

\node (bk1) [back group, anchor=west] [fit=(a) (b)] {Some words of some stuff};
\node (bk1) [back group, right] [fit=(a) (b)] {Some words of some stuff};  

请问该如何做呢?

(ps:我正在尝试在节点行周围添加一个边界框,然后添加标签描述符,如果有更好的方法我会很高兴。谢谢)。


我的代码

\documentclass[tikz,multi,border=10pt]{standalone}    
\usetikzlibrary{shadows,arrows.meta,positioning,backgrounds,fit}     

\tikzset{%
  line/.style={draw, thick, color=black!50, -LaTeX},
  back group/.style={fill=yellow!20,rounded corners, draw=black!50, 
                     dashed, inner xsep=15pt, inner ysep=10pt, 
                     anchor=west},
  block/.style = {rectangle, draw, fill=blue!20, text width=5em, 
                  text centered, rounded corners, minimum height=4em},
}

\begin{document}    
\begin{tikzpicture}[node distance = 3cm, auto]    
    \node [block] (a) {a};
    \node [block, right of=a] (b) {b};
    \path [line] (a) -- (b);

 \begin{scope}[on background layer]
     \node (bk1) [back group] [fit=(a) (b)] {Some words of some stuff};
  \end{scope}

\end{tikzpicture}
\end{document}

Some words of some stuff是我想要的位于左上角的名字。

答案1

inner ysep这是一个简短的建议(请注意,为了适合文本,我不得不增加一点。

\documentclass[tikz,multi,border=10pt]{standalone}    
\usetikzlibrary{shadows,arrows.meta,positioning,backgrounds,fit}     

\tikzset{%
  line/.style={draw, thick, color=black!50, -LaTeX},
  back group/.style={fill=yellow!20,rounded corners, draw=black!50, 
                     dashed, inner xsep=15pt, inner ysep=15pt, 
                     anchor=west},
  block/.style = {rectangle, draw, fill=blue!20, text width=5em, 
                  text centered, rounded corners, minimum height=4em},
}

\begin{document}    
\begin{tikzpicture}[node distance = 3cm, auto]    
    \node [block] (a) {a};
    \node [block, right of=a] (b) {b};
    \path [line] (a) -- (b);

 \begin{scope}[on background layer]
     \node (bk1) [back group] [fit=(a)
     (b)] {};
  \end{scope}
  \node[anchor=north west] at (bk1.north west) {Some words of some stuff};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容