Add text within tikz rectangle node

The name of the pictureThe name of the pictureThe name of the pictureClash 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


enter image description here







share|improve this question
























    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


    enter image description here







    share|improve this question






















      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


      enter image description here







      share|improve this question












      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


      enter image description here









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 28 at 15:04









      user1880405

      1625




      1625




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          6
          down vote



          accepted










          You just have to add a node at x/2:




          enter image description here




          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





          share|improve this answer



























            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:



            rectangle-midway



            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:



            rectangle-midway






            share|improve this answer





























              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


              enter image description here






              share|improve this answer



























                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


                enter image description here






                share|improve this answer






















                  Your Answer







                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "85"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  convertImagesToLinks: false,
                  noModals: false,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: null,
                  bindNavPrevention: true,
                  postfix: "",
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                   

                  draft saved


                  draft discarded


















                  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






























                  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:




                  enter image description here




                  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





                  share|improve this answer
























                    up vote
                    6
                    down vote



                    accepted










                    You just have to add a node at x/2:




                    enter image description here




                    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





                    share|improve this answer






















                      up vote
                      6
                      down vote



                      accepted







                      up vote
                      6
                      down vote



                      accepted






                      You just have to add a node at x/2:




                      enter image description here




                      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





                      share|improve this answer












                      You just have to add a node at x/2:




                      enter image description here




                      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






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Aug 28 at 15:10









                      Phelype Oleinik

                      16.2k33466




                      16.2k33466




















                          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:



                          rectangle-midway



                          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:



                          rectangle-midway






                          share|improve this answer


























                            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:



                            rectangle-midway



                            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:



                            rectangle-midway






                            share|improve this answer
























                              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:



                              rectangle-midway



                              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:



                              rectangle-midway






                              share|improve this answer














                              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:



                              rectangle-midway



                              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:



                              rectangle-midway







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Aug 29 at 7:45

























                              answered Aug 28 at 15:58









                              AndréC

                              3,029728




                              3,029728




















                                  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


                                  enter image description here






                                  share|improve this answer
























                                    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


                                    enter image description here






                                    share|improve this answer






















                                      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


                                      enter image description here






                                      share|improve this answer












                                      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


                                      enter image description here







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Aug 28 at 15:20









                                      flav

                                      3,3271625




                                      3,3271625




















                                          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


                                          enter image description here






                                          share|improve this answer


























                                            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


                                            enter image description here






                                            share|improve this answer
























                                              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


                                              enter image description here






                                              share|improve this answer














                                              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


                                              enter image description here







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Aug 28 at 16:18

























                                              answered Aug 28 at 15:28









                                              Zarko

                                              112k861150




                                              112k861150



























                                                   

                                                  draft saved


                                                  draft discarded















































                                                   


                                                  draft saved


                                                  draft discarded














                                                  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













































































                                                  這個網誌中的熱門文章

                                                  How to combine Bézier curves to a surface?

                                                  Why am i infinitely getting the same tweet with the Twitter Search API?

                                                  Is there any way to eliminate the singular point to solve this integral by hand or by approximations?