Add text within tikz rectangle node
Clash Royale CLAN TAG#URR8PPP
up vote
8
down vote
favorite
I am trying to add a centrally aligned text in the middle of each horizontal bar, but can't seem to figure out how. How can I best do that?
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6/high, B/4/med, C/2/low
node[left] at (0,y) l;
draw[fill=c] (0,y-.4) rectangle (x,y+.4);
endtikzpicture
endfigure
enddocument
tikz-pgf
add a comment |Â
up vote
8
down vote
favorite
I am trying to add a centrally aligned text in the middle of each horizontal bar, but can't seem to figure out how. How can I best do that?
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6/high, B/4/med, C/2/low
node[left] at (0,y) l;
draw[fill=c] (0,y-.4) rectangle (x,y+.4);
endtikzpicture
endfigure
enddocument
tikz-pgf
add a comment |Â
up vote
8
down vote
favorite
up vote
8
down vote
favorite
I am trying to add a centrally aligned text in the middle of each horizontal bar, but can't seem to figure out how. How can I best do that?
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6/high, B/4/med, C/2/low
node[left] at (0,y) l;
draw[fill=c] (0,y-.4) rectangle (x,y+.4);
endtikzpicture
endfigure
enddocument
tikz-pgf
I am trying to add a centrally aligned text in the middle of each horizontal bar, but can't seem to figure out how. How can I best do that?
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6/high, B/4/med, C/2/low
node[left] at (0,y) l;
draw[fill=c] (0,y-.4) rectangle (x,y+.4);
endtikzpicture
endfigure
enddocument
tikz-pgf
asked Aug 28 at 15:04
user1880405
1625
1625
add a comment |Â
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
accepted
You just have to add a node at x/2
:
documentclassarticle
% usepackagexcolor% TikZ already loads xcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/stuff[count=y] in %
A/6/high/text for everyone!,
B/4/med/more text,
C/2/low/some text
%
node [left] at (0,y) l;
draw [fill=c] (0,y-.4) rectangle (x,y+.4);
node [anchor=center] at (x/2,y) stuff;
endtikzpicture
endfigure
enddocument
add a comment |Â
up vote
6
down vote
Tikz
defines a rectangle by two vertices of one of its diagonals, just place the node midway
.
By adding a node in the middle of the path that traces the rectangle, we find exactly its center:
documentclass[tikz]standalone
begindocument
begintikzpicture
draw(0,0)rectangle(4,2)node[midway,blue]S;
draw[densely dotted, red](0,0)--(4,2);
draw[densely dotted,red](0,2)--(4,0);
endtikzpicture
enddocument
Your code becomes:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6/high, B/4/med, C/2/low
node[left] at (0,y) l;
draw[fill=c] (0,y-.4) rectangle (x,y+.4)node[midway]c;
endtikzpicture
endfigure
enddocument
the result is:
add a comment |Â
up vote
5
down vote
Another way :
documentclassarticle
usepackagexcolor
usepackagetikz
usetikzlibrarypositioning
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6cm/high, B/4cm/med, C/2cm/low
node[name=left-l] at (0,y) l;
node[fill=c, minimum width=x, right=(.2cm of left-l)] at (0,y) c;
endtikzpicture
endfigure
enddocument
add a comment |Â
up vote
4
down vote
similarly to flav answer:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/t [count=y] in A/6/high/tikz, B/4/med/love, C/2/low/I
node[draw,
fill=c,
label=left:l,
text width=x cm,
align=center, inner xsep=0pt, minimum height=8mm, right]
at (0,y) t;
endtikzpicture
endfigure
enddocument
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
You just have to add a node at x/2
:
documentclassarticle
% usepackagexcolor% TikZ already loads xcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/stuff[count=y] in %
A/6/high/text for everyone!,
B/4/med/more text,
C/2/low/some text
%
node [left] at (0,y) l;
draw [fill=c] (0,y-.4) rectangle (x,y+.4);
node [anchor=center] at (x/2,y) stuff;
endtikzpicture
endfigure
enddocument
add a comment |Â
up vote
6
down vote
accepted
You just have to add a node at x/2
:
documentclassarticle
% usepackagexcolor% TikZ already loads xcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/stuff[count=y] in %
A/6/high/text for everyone!,
B/4/med/more text,
C/2/low/some text
%
node [left] at (0,y) l;
draw [fill=c] (0,y-.4) rectangle (x,y+.4);
node [anchor=center] at (x/2,y) stuff;
endtikzpicture
endfigure
enddocument
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
You just have to add a node at x/2
:
documentclassarticle
% usepackagexcolor% TikZ already loads xcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/stuff[count=y] in %
A/6/high/text for everyone!,
B/4/med/more text,
C/2/low/some text
%
node [left] at (0,y) l;
draw [fill=c] (0,y-.4) rectangle (x,y+.4);
node [anchor=center] at (x/2,y) stuff;
endtikzpicture
endfigure
enddocument
You just have to add a node at x/2
:
documentclassarticle
% usepackagexcolor% TikZ already loads xcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/stuff[count=y] in %
A/6/high/text for everyone!,
B/4/med/more text,
C/2/low/some text
%
node [left] at (0,y) l;
draw [fill=c] (0,y-.4) rectangle (x,y+.4);
node [anchor=center] at (x/2,y) stuff;
endtikzpicture
endfigure
enddocument
answered Aug 28 at 15:10
Phelype Oleinik
16.2k33466
16.2k33466
add a comment |Â
add a comment |Â
up vote
6
down vote
Tikz
defines a rectangle by two vertices of one of its diagonals, just place the node midway
.
By adding a node in the middle of the path that traces the rectangle, we find exactly its center:
documentclass[tikz]standalone
begindocument
begintikzpicture
draw(0,0)rectangle(4,2)node[midway,blue]S;
draw[densely dotted, red](0,0)--(4,2);
draw[densely dotted,red](0,2)--(4,0);
endtikzpicture
enddocument
Your code becomes:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6/high, B/4/med, C/2/low
node[left] at (0,y) l;
draw[fill=c] (0,y-.4) rectangle (x,y+.4)node[midway]c;
endtikzpicture
endfigure
enddocument
the result is:
add a comment |Â
up vote
6
down vote
Tikz
defines a rectangle by two vertices of one of its diagonals, just place the node midway
.
By adding a node in the middle of the path that traces the rectangle, we find exactly its center:
documentclass[tikz]standalone
begindocument
begintikzpicture
draw(0,0)rectangle(4,2)node[midway,blue]S;
draw[densely dotted, red](0,0)--(4,2);
draw[densely dotted,red](0,2)--(4,0);
endtikzpicture
enddocument
Your code becomes:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6/high, B/4/med, C/2/low
node[left] at (0,y) l;
draw[fill=c] (0,y-.4) rectangle (x,y+.4)node[midway]c;
endtikzpicture
endfigure
enddocument
the result is:
add a comment |Â
up vote
6
down vote
up vote
6
down vote
Tikz
defines a rectangle by two vertices of one of its diagonals, just place the node midway
.
By adding a node in the middle of the path that traces the rectangle, we find exactly its center:
documentclass[tikz]standalone
begindocument
begintikzpicture
draw(0,0)rectangle(4,2)node[midway,blue]S;
draw[densely dotted, red](0,0)--(4,2);
draw[densely dotted,red](0,2)--(4,0);
endtikzpicture
enddocument
Your code becomes:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6/high, B/4/med, C/2/low
node[left] at (0,y) l;
draw[fill=c] (0,y-.4) rectangle (x,y+.4)node[midway]c;
endtikzpicture
endfigure
enddocument
the result is:
Tikz
defines a rectangle by two vertices of one of its diagonals, just place the node midway
.
By adding a node in the middle of the path that traces the rectangle, we find exactly its center:
documentclass[tikz]standalone
begindocument
begintikzpicture
draw(0,0)rectangle(4,2)node[midway,blue]S;
draw[densely dotted, red](0,0)--(4,2);
draw[densely dotted,red](0,2)--(4,0);
endtikzpicture
enddocument
Your code becomes:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6/high, B/4/med, C/2/low
node[left] at (0,y) l;
draw[fill=c] (0,y-.4) rectangle (x,y+.4)node[midway]c;
endtikzpicture
endfigure
enddocument
the result is:
edited Aug 29 at 7:45
answered Aug 28 at 15:58
AndréC
3,029728
3,029728
add a comment |Â
add a comment |Â
up vote
5
down vote
Another way :
documentclassarticle
usepackagexcolor
usepackagetikz
usetikzlibrarypositioning
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6cm/high, B/4cm/med, C/2cm/low
node[name=left-l] at (0,y) l;
node[fill=c, minimum width=x, right=(.2cm of left-l)] at (0,y) c;
endtikzpicture
endfigure
enddocument
add a comment |Â
up vote
5
down vote
Another way :
documentclassarticle
usepackagexcolor
usepackagetikz
usetikzlibrarypositioning
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6cm/high, B/4cm/med, C/2cm/low
node[name=left-l] at (0,y) l;
node[fill=c, minimum width=x, right=(.2cm of left-l)] at (0,y) c;
endtikzpicture
endfigure
enddocument
add a comment |Â
up vote
5
down vote
up vote
5
down vote
Another way :
documentclassarticle
usepackagexcolor
usepackagetikz
usetikzlibrarypositioning
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6cm/high, B/4cm/med, C/2cm/low
node[name=left-l] at (0,y) l;
node[fill=c, minimum width=x, right=(.2cm of left-l)] at (0,y) c;
endtikzpicture
endfigure
enddocument
Another way :
documentclassarticle
usepackagexcolor
usepackagetikz
usetikzlibrarypositioning
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c[count=y] in A/6cm/high, B/4cm/med, C/2cm/low
node[name=left-l] at (0,y) l;
node[fill=c, minimum width=x, right=(.2cm of left-l)] at (0,y) c;
endtikzpicture
endfigure
enddocument
answered Aug 28 at 15:20
flav
3,3271625
3,3271625
add a comment |Â
add a comment |Â
up vote
4
down vote
similarly to flav answer:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/t [count=y] in A/6/high/tikz, B/4/med/love, C/2/low/I
node[draw,
fill=c,
label=left:l,
text width=x cm,
align=center, inner xsep=0pt, minimum height=8mm, right]
at (0,y) t;
endtikzpicture
endfigure
enddocument
add a comment |Â
up vote
4
down vote
similarly to flav answer:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/t [count=y] in A/6/high/tikz, B/4/med/love, C/2/low/I
node[draw,
fill=c,
label=left:l,
text width=x cm,
align=center, inner xsep=0pt, minimum height=8mm, right]
at (0,y) t;
endtikzpicture
endfigure
enddocument
add a comment |Â
up vote
4
down vote
up vote
4
down vote
similarly to flav answer:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/t [count=y] in A/6/high/tikz, B/4/med/love, C/2/low/I
node[draw,
fill=c,
label=left:l,
text width=x cm,
align=center, inner xsep=0pt, minimum height=8mm, right]
at (0,y) t;
endtikzpicture
endfigure
enddocument
similarly to flav answer:
documentclassarticle
usepackagexcolor
usepackagetikz
definecolorhighrgb0.89, 0.26, 0.2
definecolormedrgb1.0, 0.65, 0.0
definecolorlowrgb0.98, 0.93, 0.36
begindocument
beginfigure
centering
begintikzpicture
foreach l/x/c/t [count=y] in A/6/high/tikz, B/4/med/love, C/2/low/I
node[draw,
fill=c,
label=left:l,
text width=x cm,
align=center, inner xsep=0pt, minimum height=8mm, right]
at (0,y) t;
endtikzpicture
endfigure
enddocument
edited Aug 28 at 16:18
answered Aug 28 at 15:28
Zarko
112k861150
112k861150
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f448164%2fadd-text-within-tikz-rectangle-node%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password