2

Say I have a node:

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

How can I horizontally center the text rect… above my rectangle?

Thanks in advance for your answers.

Kindly,

NS

Edit (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}

output

EDIT #2:

It appears that the line style = {font=\normalsize} is causing the problem. It is probably a conflict with some package I'm using because it doesn't cause any problem on minimal examples. I'll post an answer if I find which one it is.

  • 1
    Hi ! Could you give us a MWE ? – flav Apr 11 '18 at 16:33
  • It is centered when you do that, can you show an example demonstrating that it isn't? Edit: for example with \documentclass{article} \usepackage{tikz} \begin{document} \tikz\node [rectangle,draw,label=above:rect] {}; \end{document} the label is centered above the rectangle. – Torbjørn T. Apr 11 '18 at 16:37
  • Tip: The button marked {} is for code markup, that is better than quote markup. – Torbjørn T. Apr 11 '18 at 16:47
  • I just got it, thank you. Sorry I'm still a newbie here. Does the example I provided work for you? – N. Short Apr 11 '18 at 16:50
  • You're missing \usetikzlibrary{shapes.geometric} from your example. But what output do you get? After adding draw to the rct style I get a rectangle with the label above it, centered horizontally. – Torbjørn T. Apr 11 '18 at 16:50
1

i guess, that you looking for the following:

enter image description here

labels positioned by above or below are always horizontal centered regarding to a node's anchor north or south respectively (if you not shift them with xshift).

your mwe has errors and contains some illogicality. i correct them and add explanations as comments in code:

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.