如何使定义的 \tikzset 标签居中显示?

如何使定义的 \tikzset 标签居中显示?

我设法从该论坛的另一个问题中获得了一些代码(我忘了在哪里,但这里是代码):

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

当我尝试在预定义节点上标记某些内容时,标签会按预期出现在顶部并居中。但是,对于创建的 tikzset(饱和度和速率限制),标签不会按预期居中。有没有办法在 LaTeX 中实现标签命令(尤其是使标签位于框的顶部并居中),以便它能像对预定义的 tikzstyles 一样适用于定义的 tikzset?下面是它的外观图片,如下所示: 在此处输入图片描述

非常感谢!

答案1

问题是你有两次

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

但为了使节点居中,你需要

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

此外,我摆脱了弃用的\tikzstyles库和你不使用库来arrows定位事物的方式,从而positioning

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

在此处输入图片描述

人们可以进一步调整代码,但以上是主要问题。

这是使用 s 得到的相同结果label

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

相关内容