# How to make a defined \tikzset label centered above?

I managed to get some code from another question on this forum (I forgot where, but here is the code):

\tikzstyle{block} = [draw, rectangle,
minimum height=3em, minimum width=3em]
\tikzstyle{sum} = [draw, circle, node distance=1cm]
\tikzstyle{input} = [coordinate]
\tikzstyle{output} = [coordinate]
\tikzstyle{pinstyle} = [pin edge={to-,thin,black}]
\tikzset{%
saturation block/.style={%
draw,
path picture={
% Get the width and height of the path picture node
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
{\pgfpointanchor{path picture bounding box}{north east}}
\pgfgetlastxy\x\y
% Scale the x and y vectors so that the range
% -1 to 1 is slightly shorter than the size of the node
\tikzset{x=\x*.4, y=\y*.4}
%
% Draw annotation
\draw [very thin] (-1,0) -- (1,0) (0,-1) -- (0,1);
\draw [very thick] (-1,-.7) -- (-.7,-.7) -- (.7,.7) -- (1,.7);
},
append after command={\pgfextra{\let\mainnode=\tikzlastnode}
node[above right] at (\mainnode.north west) {#1}%
}
}
}

\tikzset{%
rateLimit block/.style={%
draw,
path picture={
% Get the width and height of the path picture node
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
{\pgfpointanchor{path picture bounding box}{north east}}
\pgfgetlastxy\x\y
% Scale the x and y vectors so that the range
% -1 to 1 is slightly shorter than the size of the node
\tikzset{x=\x*.4, y=\y*.4}
%
% Draw annotation
%\draw [very thin] (-1,-1) -- (-1,1) (-1,-1) -- (1,-1);
\draw [very thin] (-1,0) -- (1,0) (0,-1) -- (0,1);
\draw [very thick] (-1,-1) -- (1, 1);
},
append after command={\pgfextra{\let\mainnode=\tikzlastnode}
node[above right] at (\mainnode.north west) {#1}%
}
}
}
\begin{figure}[H]
\centering
% The block diagram code is probably more verbose than necessary
\begin{tikzpicture}[auto, node distance=2cm,>=latex']
% We start by placing the blocks
\node [input, name=input] {};
\node [sum, right of=input, node distance = 2cm, name = sum] {};
\node [block, right of=sum, node distance = 2cm, label = Hissssss] (ctrler) {PID};
\node [saturation block = {Saturation}, minimum height=3em, minimum width=3em, right of=ctrler, node distance = 3.5 cm] (sat){};
\node [rateLimit block = {Rate limit}, minimum height=3em, minimum width=3em, right of=sat, node distance = 3.5 cm] (rateLim) {};
\node [output, right of=rateLim, node distance = 2 cm] (output) {};

% Once the nodes are placed, connecting them is easy.
\draw [draw,->] (input) -- node[pos=0.92] {$+$} node {$R\left(s\right)$} (sum);
\draw [->] (sum) -- (ctrler);
\draw [->] (ctrler) -- node [name = commSig, pos = 0.5]{$U_c\left(s\right)$}(sat);
\draw [->] (sat) -- node [name = contSig, pos = 0.5]{$U\left(s\right)$} (rateLim);
\draw [->] (rateLim) -- node [name = out, pos = 0.8]{$X\left(s\right)$} (output);
\draw [->] (out) -- ++ (0, -1.5cm) -| node[pos=0.92] {$-$} (sum);
\end{tikzpicture}
\caption{Block diagram of the entire system.}
\label{blockdiag}
\end{figure}


When I try to label something on a predefined node, the label appears top and centered as expected. However, for the created tikzset (saturation and rate limit), the labels are not centered as expected. Is there a way to implement the label command in LaTeX (especially to make the label on top of the box and centered) such that it would work for defined tikzsets like it did for predefined tikzstyles? Here is the picture of what it looks like as shown below:

Thank you very much!

• Welcome! You have twice node[above right] at (\mainnode.north west) {#1} but seem to want node[above] at (\mainnode.north) {#1}, which centers the nodes. \tikzstyle is no longer supported. – user194703 Jan 21 at 3:38
• Hello, I managed to figure out that I have to remove right and west. But... how do I implement it with the label command? Also, how come \tikzstyle got deprecated? – Superman Jan 21 at 3:57
• Does the provided answer solve the problem? – Dr. Manuel Kuehner Mar 8 at 8:45

The issue is that you have twice

node[above right] at (\mainnode.north west) {#1}


but in order to center the nodes you need

node[above] at (\mainnode.north) {#1}


Further, I got rid of the deprecated \tikzstyles, arrows library and your way of positioning things without using the positioning library to arrive at

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\tikzset{block/.style={draw, rectangle,
minimum height=3em, minimum width=3em},
sum/.style={draw, circle, node distance=1cm},
input/.style={coordinate},
output/.style={coordinate},
pinstyle/.style={pin edge={to-,thin,black}},
saturation block/.style={%
draw,
path picture={
% Get the width and height of the path picture node
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
{\pgfpointanchor{path picture bounding box}{north east}}
\pgfgetlastxy\x\y
% Scale the x and y vectors so that the range
% -1 to 1 is slightly shorter than the size of the node
\tikzset{x=\x*.4, y=\y*.4}
%
% Draw annotation
\draw [very thin] (-1,0) -- (1,0) (0,-1) -- (0,1);
\draw [very thick] (-1,-.7) -- (-.7,-.7) -- (.7,.7) -- (1,.7);
},
append after command={\pgfextra{\let\mainnode=\tikzlastnode}
node[above] at (\mainnode.north) {#1}%
}
}
}

\tikzset{%
rateLimit block/.style={%
draw,
path picture={
% Get the width and height of the path picture node
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
{\pgfpointanchor{path picture bounding box}{north east}}
\pgfgetlastxy\x\y
% Scale the x and y vectors so that the range
% -1 to 1 is slightly shorter than the size of the node
\tikzset{x=\x*.4, y=\y*.4}
%
% Draw annotation
%\draw [very thin] (-1,-1) -- (-1,1) (-1,-1) -- (1,-1);
\draw [very thin] (-1,0) -- (1,0) (0,-1) -- (0,1);
\draw [very thick] (-1,-1) -- (1, 1);
},
append after command={\pgfextra{\let\mainnode=\tikzlastnode}
node[above] at (\mainnode.north) {#1}%
}
}
}
\begin{document}
\begin{figure}[htb]
\centering
% The block diagram code is probably more verbose than necessary
\begin{tikzpicture}[auto, node distance=1.5cm,>=Latex]
% We start by placing the blocks
\node [input, name=input] {};
\node [sum, right=1.75cm of input, name = sum] {};
\node [block, right=of sum, label = Hissssss] (ctrler) {PID};
\node [saturation block = {Saturation}, minimum height=3em, minimum
width=3em, right=1.8cm of ctrler] (sat){};
\node [rateLimit block = {Rate limit}, minimum height=3em, minimum
width=3em, right=1.8cm of sat] (rateLim) {};
\node [output, right=of rateLim, node distance = 2 cm] (output) {};

% Once the nodes are placed, connecting them is easy.
\draw [draw,->] (input) -- node[pos=0.92] {$+$} node {$R\left(s\right)$} (sum);
\draw [->] (sum) -- (ctrler);
\draw [->] (ctrler) -- node [name = commSig, pos = 0.5]{$U_c\left(s\right)$}(sat);
\draw [->] (sat) -- node [name = contSig, pos = 0.5]{$U\left(s\right)$} (rateLim);
\draw [->] (rateLim) -- node [name = out, pos = 0.8]{$X\left(s\right)$} (output);
\draw [->] (out) -- ++ (0, -1.5cm) -| node[pos=0.92] {$-$} (sum);
\end{tikzpicture}
\caption{Block diagram of the entire system.}
\label{blockdiag}
\end{figure}
\end{document}


One may further tune the code but the above are the main issues.

Here is the same result using labels.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\tikzset{block/.style={draw, rectangle,
minimum height=3em, minimum width=3em},
sum/.style={draw, circle, node distance=1cm},
input/.style={coordinate},
output/.style={coordinate},
pinstyle/.style={pin edge={to-,thin,black}},
saturation block/.style={%
draw,
path picture={
% Get the width and height of the path picture node
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
{\pgfpointanchor{path picture bounding box}{north east}}
\pgfgetlastxy\x\y
% Scale the x and y vectors so that the range
% -1 to 1 is slightly shorter than the size of the node
\tikzset{x=\x*.4, y=\y*.4}
%
% Draw annotation
\draw [very thin] (-1,0) -- (1,0) (0,-1) -- (0,1);
\draw [very thick] (-1,-.7) -- (-.7,-.7) -- (.7,.7) -- (1,.7);
},
}
}

\tikzset{%
rateLimit block/.style={%
draw,
path picture={
% Get the width and height of the path picture node
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
{\pgfpointanchor{path picture bounding box}{north east}}
\pgfgetlastxy\x\y
% Scale the x and y vectors so that the range
% -1 to 1 is slightly shorter than the size of the node
\tikzset{x=\x*.4, y=\y*.4}
%
% Draw annotation
%\draw [very thin] (-1,-1) -- (-1,1) (-1,-1) -- (1,-1);
\draw [very thin] (-1,0) -- (1,0) (0,-1) -- (0,1);
\draw [very thick] (-1,-1) -- (1, 1);
},
}
}
\begin{document}
\begin{figure}[htb]
\centering
% The block diagram code is probably more verbose than necessary
\begin{tikzpicture}[auto, node distance=1.5cm,>=Latex]
% We start by placing the blocks
\node [input, name=input] {};
\node [sum, right=1.75cm of input, name = sum] {};
\node [block, right=of sum, label = Hissssss] (ctrler) {PID};
\node [saturation block,label=above:{Saturation}, minimum height=3em, minimum
width=3em, right=1.8cm of ctrler] (sat){};
\node [rateLimit block,label=above:{Rate limit}, minimum height=3em, minimum
width=3em, right=1.8cm of sat] (rateLim) {};
\node [output, right=of rateLim, node distance = 2 cm] (output) {};

% Once the nodes are placed, connecting them is easy.
\draw [draw,->] (input) -- node[pos=0.92] {$+$} node {$R\left(s\right)$} (sum);
\draw [->] (sum) -- (ctrler);
\draw [->] (ctrler) -- node [name = commSig, pos = 0.5]{$U_c\left(s\right)$}(sat);
\draw [->] (sat) -- node [name = contSig, pos = 0.5]{$U\left(s\right)$} (rateLim);
\draw [->] (rateLim) -- node [name = out, pos = 0.8]{$X\left(s\right)$} (output);
\draw [->] (out) -- ++ (0, -1.5cm) -| node[pos=0.92] {$-$} (sum);
\end{tikzpicture}
\caption{Block diagram of the entire system.}
\label{blockdiag}
\end{figure}
\end{document}

• Out of curiosity, did you feel that the arrows library is also deprecated? If that's so, I wonder why. Also, how do you implement the label command when "making up your own sketch" (e.g. saturation and rate limit)? – Superman Jan 21 at 3:59
• @Superman Using labels is much simpler, I added a version. Why is arrows deprecated? Because it got superseded by arrows.meta, which is more powerful. Why is \tikzstyle deprecated? Because it does not offer anything that \tikzset{blabla/.style=...} does not already do, and cannot be placed in the options of tikzpictures. So it is unnecessary, and the maintainer(s) of TikZ do(es) no longer support it. – user194703 Jan 21 at 4:02
• Hmmmm interesting. What makes arrows.meta more powerful than arrows? – Superman Jan 21 at 4:05
• @Superman You can define any conceivable arrow with arrows.meta. This is not true for arrows. – user194703 Jan 21 at 4:05
• Oh, like a thick line with a "thin" arrow, and vice versa? For arrows, it's more uniform I suppose – Superman Jan 21 at 4:10