8

I am making some graphical models with tikz, and I have added a rectangle (plate./style) around three nodes. As you can see in the picture the text "N" is vertically/horizontally center aligned. I want to align the text "N" to the bottom right of the "plate" i.e. I want "N" at the bottom right corner of the rounded rectangle. Since I will be doing this repeatedly, I want this setting to be made inside 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}

enter image description here

3 Answers 3

5

Use an argument for the plate style and use it to place the label using the south.east anchor:

\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}

enter image description here

0
3

This solution is similar to Gonzalo's answer but instead of label uses append after command option to place the node. It also uses positioning library although it's not strictly necessary.

\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}

enter image description here

1

Does this qualify as "inside tikz"? I introduce a macro \shft{} that takes its argument and moves it down 43pt (actually 43pt plus its own height) and over 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}

enter image description here

2
  • This is brilliant! Thanks. Can we somehow move \shft{} inside tikzset?
    – ahmadh
    Jan 30, 2014 at 14:54
  • @ahmadh I'm sorry, but I don't know enough about tikz to answer your follow-up query. Best wishes. Jan 30, 2014 at 15:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .