TikZ:矩形中的右下角对齐文本

TikZ:矩形中的右下角对齐文本

我正在使用 tikz 制作一些图形模型,并在三个节点周围添加了一个矩形(plate./style)。如您在图片中看到的,文本“N”垂直/水平居中对齐。我想将文本“N”对齐到“plate”的右下角,即我希望“N”位于圆角矩形的右下角。由于我将重复执行此操作,因此我希望在 tikzset 内进行此设置。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\tikzset{
  latentnode/.style={draw, minimum width=5mm, shape=circle, ultra thick, black},
  dagconn/.style={arrows=->, black, thick},
  plate/.style={draw, shape=rectangle, rounded corners=0.5ex, thick,
    minimum width=3.1cm, text width=3.1cm, align=right, inner sep=10pt, innerysep=10pt}
}

\begin{document}

\begin{figure}[t!]
 \begin{tikzpicture}[scale=1]
   \node[latentnode] (1) [label=below left:$z_1$] at (1,3) {};
   \node[latentnode] (2) [label=below left:$z_2$] at (2.5,3) {};
   \node[latentnode] (3) [label=below left:$z_3$] at (4,3) {};
   \node[latentnode] (x1) [label=left:$x_1$] at (1,1.5) {};
   \node[latentnode] (x2) [label=left:$x_2$] at (2.5,1.5) {};
   \node[latentnode] (x3) [label=left:$x_3$] at (4,1.5) {};
   \node[plate, inner sep=20pt, fit=(1) (2) (3)] (plate1) {N};

   \draw[dagconn] (1) to (2);
   \draw[dagconn] (2) to (3);
   \draw[dagconn] (1) to (x1);
   \draw[dagconn] (2) to (x2);
   \draw[dagconn] (3) to (x3);
 \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案1

使用样式参数plate并使用它使用锚点放置标签south.east

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}

\tikzset{
  latentnode/.style={draw, minimum width=5mm, shape=circle, ultra thick, black},
  dagconn/.style={arrows=->, black, thick},
  plate/.style={draw, shape=rectangle, rounded corners=0.5ex, thick,
    minimum width=3.1cm, text width=3.1cm, align=right, inner sep=10pt, inner ysep=10pt,label={[xshift=-14pt,yshift=14pt]south east:#1}}
}

\begin{document}

\begin{figure}[t!]
 \begin{tikzpicture}[scale=1]
   \node[latentnode] (1) [label=below left:$z_1$] at (1,3) {};
   \node[latentnode] (2) [label=below left:$z_2$] at (2.5,3) {};
   \node[latentnode] (3) [label=below left:$z_3$] at (4,3) {};
   \node[latentnode] (x1) [label=left:$x_1$] at (1,1.5) {};
   \node[latentnode] (x2) [label=left:$x_2$] at (2.5,1.5) {};
   \node[latentnode] (x3) [label=left:$x_3$] at (4,1.5) {};
   \node[plate=N, inner sep=20pt, fit=(1) (2) (3)] (plate1) {};

   \draw[dagconn] (1) to (2);
   \draw[dagconn] (2) to (3);
   \draw[dagconn] (1) to (x1);
   \draw[dagconn] (2) to (x2);
   \draw[dagconn] (3) to (x3);
 \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案2

这个解决方案与 Gonzalo 的答案类似,但它不是label使用append after command选项来放置节点,而是使用positioning库,尽管这不是必需的。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit, positioning}

\tikzset{
  latentnode/.style={draw, minimum width=5mm, shape=circle, ultra thick, black},
  dagconn/.style={arrows=->, black, thick},
  plate/.style={draw, shape=rectangle, rounded corners=0.5ex, thick,
    minimum width=3.1cm, text width=3.1cm, align=right, inner sep=10pt, inner ysep=10pt, 
    append after command={node[above left= 3pt of \tikzlastnode.south east] {#1}}}
}

\begin{document}

\begin{figure}[t!]
 \begin{tikzpicture}[scale=1]
   \node[latentnode] (1) [label=below left:$z_1$] at (1,3) {};
   \node[latentnode] (2) [label=below left:$z_2$] at (2.5,3) {};
   \node[latentnode] (3) [label=below left:$z_3$] at (4,3) {};
   \node[latentnode] (x1) [label=left:$x_1$] at (1,1.5) {};
   \node[latentnode] (x2) [label=left:$x_2$] at (2.5,1.5) {};
   \node[latentnode] (x3) [label=left:$x_3$] at (4,1.5) {};
   \node[plate=N, inner sep=20pt, fit=(1) (2) (3)] (plate1) {};

   \draw[dagconn] (1) to (2);
   \draw[dagconn] (2) to (3);
   \draw[dagconn] (1) to (x1);
   \draw[dagconn] (2) to (x2);
   \draw[dagconn] (3) to (x3);
 \end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案3

这是否符合“tikz 内部”的条件?我引入了一个宏\shft{},它接受其参数并将其向下移动 43pt(实际上是 43pt 加上其自身的高度)并超过 112pt。

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\usepackage{stackengine}
\def\shft#1{\stackunder[43pt]{}{\kern112pt #1}}
\tikzset{
  latentnode/.style={draw, minimum width=5mm, shape=circle, ultra thick, black},
  dagconn/.style={arrows=->, black, thick},
  plate/.style={draw, shape=rectangle, rounded corners=0.5ex, thick,
    minimum width=3.1cm, text width=3.1cm, align=right, inner sep=10pt, %innerysep=10pt
}
}
\begin{document}
\begin{figure}[t!]
 \begin{tikzpicture}[scale=1]
   \node[latentnode] (1) [label=below left:$z_1$] at (1,3) {};
   \node[latentnode] (2) [label=below left:$z_2$] at (2.5,3) {};
   \node[latentnode] (3) [label=below left:$z_3$] at (4,3) {};
   \node[latentnode] (x1) [label=left:$x_1$] at (1,1.5) {};
   \node[latentnode] (x2) [label=left:$x_2$] at (2.5,1.5) {};
   \node[latentnode] (x3) [label=left:$x_3$] at (4,1.5) {};
   \node[plate, inner sep=20pt, fit=(1) (2) (3)] (plate1) {\shft{N}};

   \draw[dagconn] (1) to (2);
   \draw[dagconn] (2) to (3);
   \draw[dagconn] (1) to (x1);
   \draw[dagconn] (2) to (x2);
   \draw[dagconn] (3) to (x3);
 \end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容