tikz-pgf – How to Left and Right Align Inside One TikZ Rectangular Node?

tikz-pgf

I'd like to create a PID controller block diagram similar to the one here. At the moment my diagram looks as depicted below. What I still need is to right-and-left align the text inside the rectangular nodes, i.e. I'd like the letters P, I, D to be left and the equations right aligned. How do I accomplish that? Also, could you please tell me how to make the circle a little smaller?

enter image description here

\tikzstyle{int}=[draw, fill=blue!20, minimum size=2em, text width=2.5cm]
\tikzstyle{sum}=[draw, fill=blue!20, shape=circle, node distance=2.5cm]
\begin{figure}[htbp]
\centering
    \begin{tikzpicture}[node distance=1.5cm,auto,>=latex']
        \node [int] (kp) {P $k_p e(t)$};
        \node [int] (ki) [below of=kp] {I $k_i \int{e(t)dt}$};
        \node [int] (kd) [below of=ki] {D $k_d \frac{e(t)}{dt}$};
        \node [sum] (sum) [right of=ki] {$\sum$};
        \node [coordinate] (joint1) [left of=ki, node distance=2cm]{};
        \node [coordinate] (begin) [left of=joint1, node distance=2cm]{};
        \node [coordinate] (end) [right of=sum, node distance=2cm]{};
        \draw[-] (begin) -- node {$e(t)$} (joint1);
        \draw[->] (joint1) |- (kp.west);
        \draw[->] (joint1) -- (ki);
        \draw[->] (joint1) |- (kd);
        \draw[->] (kp.east) -| node [pos=0.95] {$+$} (sum.north);
        \draw[->] (ki.east) -- node [pos=0.8] {$+$} (sum.west);
        \draw[->] (kd.east) -| node [pos=0.95] {$+$} (sum.south);
        \draw[->] (sum.east) -- (end);
    \end{tikzpicture}   
\end{figure}

Best Answer

You can use \hfill between the letter and the equation to get simultaneous left and right adjustment. This only works if you specify the text width explicitly, though (as you've done in your code).

For decreasing the size of the circle, you'll have to decrease the inner sep (which is set to 2pt by default). If you want the node to be smaller still, you'll have to choose a smaller font size (using font=\small, for instance), or scale the node using scale=<value>.

Here's a minimal example (this is also the preferred form of posting code like yours in a question, since it saves people the trouble of guessing the required libraries/packages, and makes sure that the code is actually complete):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows}
\begin{document}
\tikzstyle{int}=[draw, fill=blue!20, minimum size=2em, text width=2.5cm]
\tikzstyle{sum}=[draw, fill=blue!20, shape=circle, inner sep=1pt, node distance=2.5cm]
\begin{figure}[htbp]
\centering
    \begin{tikzpicture}[node distance=1.5cm,auto,>=latex']
        \node [int] (kp) {P\hfill$k_p e(t)$};
        \node [int] (ki) [below of=kp] {I\hfill$k_i \int{e(t)dt}$};
        \node [int] (kd) [below of=ki] {D\hfill$k_d \frac{e(t)}{dt}$};
        \node [sum] (sum) [right of=ki] {$\sum$};
        \node [coordinate] (joint1) [left of=ki, node distance=2cm]{};
        \node [coordinate] (begin) [left of=joint1, node distance=2cm]{};
        \node [coordinate] (end) [right of=sum, node distance=2cm]{};
        \draw[-] (begin) -- node {$e(t)$} (joint1);
        \draw[->] (joint1) |- (kp.west);
        \draw[->] (joint1) -- (ki);
        \draw[->] (joint1) |- (kd);
        \draw[->] (kp.east) -| node [pos=0.95] {$+$} (sum.north);
        \draw[->] (ki.east) -- node [pos=0.8] {$+$} (sum.west);
        \draw[->] (kd.east) -| node [pos=0.95] {$+$} (sum.south);
        \draw[->] (sum.east) -- (end);
    \end{tikzpicture}   
\end{figure}

\end{document}