TikZ:使用 label= 时如何使节点的标签文本居中?

TikZ:使用 label= 时如何使节点的标签文本居中?

假设我有一个节点:

\node [rectangle,label=above:$\text{rect…}$] (r0) at (0,0) {};

我怎样才能使文本水平居中直角…在我的矩形上方?

提前感谢您的回答。

亲切地,

国家标准

编辑(MWE):

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}[x=01.00mm,y=00.50mm,node distance=10.00mm and 10.00mm,> = stealth',bend angle = 45,style = {font=\normalsize},every node/.style = {transform shape}]

\tikzset{tickh/.style={rectangle,text width=0,text height=0,fill=black,align=center,minimum width=4,minimum height=20,outer sep=0,inner sep=0,scale=0.5}}
\tikzset{rct/.style={trapezium angle=90,minimum width=20,fill=gray!100!black}}

\node [tickh,label={below:$\textit{aa}_i$}] (x1) at (0,0) {};
\node [rct,label={above:$\textit{bb}_j$},xshift=-03.50mm] (t0) [above=+02.00 of x1.center,anchor=south east] {};

\end{tikzpicture}
\end{document}

输出

编辑#2:

看来是这行代码style = {font=\normalsize}导致了问题。它可能与我使用的某个包发生冲突,因为它在最小示例上不会引起任何问题。如果我找到是哪一个,我会发布答案。

答案1

我猜您正在寻找以下内容:

在此处输入图片描述

above由或定位的标签below始终分别相对于节点的锚点北或南水平居中(如果不使用 移动它们xshift)。

你的 mwe 有错误,并且包含一些不合逻辑的地方。我纠正它们并在代码中添加解释作为注释:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows,
                positioning,
                shapes,
                shapes.geometric}
\begin{document}
\begin{tikzpicture}[
    x=01.00mm,y=00.50mm,        % tikz is not enginering tool, suficient is "x=1mm, y=0.5mm"
    node distance=0mm and 2mm,  % set to desired distances between nodes
    > = stealth',               % it need package `arrows
    bend angle = 45,            % not used in this mwe
%   style = {font=\normalsize}, % this is superfluous. default font size is normal size
                                % if for some reason like to change font size used in tikz picture, 
                                % than write for example only  "font=\small" without 
                                % use of "style={....}" . it is wrong syntax
    every node/.style = {transform shape}   % where you need this?
                    ]
\tikzset{tickh/.style={
    rectangle, fill=black,
    text width=0,text height=0, % what you like to achieve with this?
    align=center,               % for given text width this is pointless
    minimum height=20, minimum width=4,
    outer sep=0, inner sep=0,
    scale=0.5
                        },
            rct/.style={        % if you not define shape,
                                % tikz consider rectangle for node's shape
    trapezium angle=90,         % rectangle hasn't this option
    minimum width=20,
    fill=gray}
            }
\node [tickh,
       label=below:$\mathit{aa}_i$] 
                           (x1) {}; % label is centered regarding to node below of node
\node [rct,  
       label=above:$\mathit{bb}_j$, % label is centered regarding to node below of node
       above right=of x1] (t0) {};  % for positioning is used only "node distance"
                                    % in case that placement of this node is exception from
                                    % prescribed distances, than instead of `xshift` and `yshift` rather use
                                    % "node disance=<vertical distance> and <horizontal distance> of <anchor>".
                                    % if <anchor> is node, than distances are between borders of the adjacent nodes
\end{tikzpicture}
\end{document}

相关内容