Aligning legends in multiple columns with pgfplots

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












How can you align the text inside labels with pgfplots? In the example below, I would like to align both the k's and the m's.



documentclass[preview]standalone
usepackagepgfplots
begindocument
begintikzpicture
beginaxis[
title=$y=kx+m$,
ylabel=$y$,
xlabel=$x$,
legend cell align=left,
]
addplot[blue, domain=1:4, samples=4] 10*x - 5;
addlegendentry$k = 10$, $m = -5$
addplot[red, domain=1:4, samples=4] 2*x + 3;
addlegendentry$k = 2$, $m = 3$
endaxis
endtikzpicture
enddocument


mwe







share|improve this question
















  • 1




    have a look at tex.stackexchange.com/a/440121/36296
    – samcarter
    Aug 15 at 12:35














up vote
3
down vote

favorite












How can you align the text inside labels with pgfplots? In the example below, I would like to align both the k's and the m's.



documentclass[preview]standalone
usepackagepgfplots
begindocument
begintikzpicture
beginaxis[
title=$y=kx+m$,
ylabel=$y$,
xlabel=$x$,
legend cell align=left,
]
addplot[blue, domain=1:4, samples=4] 10*x - 5;
addlegendentry$k = 10$, $m = -5$
addplot[red, domain=1:4, samples=4] 2*x + 3;
addlegendentry$k = 2$, $m = 3$
endaxis
endtikzpicture
enddocument


mwe







share|improve this question
















  • 1




    have a look at tex.stackexchange.com/a/440121/36296
    – samcarter
    Aug 15 at 12:35












up vote
3
down vote

favorite









up vote
3
down vote

favorite











How can you align the text inside labels with pgfplots? In the example below, I would like to align both the k's and the m's.



documentclass[preview]standalone
usepackagepgfplots
begindocument
begintikzpicture
beginaxis[
title=$y=kx+m$,
ylabel=$y$,
xlabel=$x$,
legend cell align=left,
]
addplot[blue, domain=1:4, samples=4] 10*x - 5;
addlegendentry$k = 10$, $m = -5$
addplot[red, domain=1:4, samples=4] 2*x + 3;
addlegendentry$k = 2$, $m = 3$
endaxis
endtikzpicture
enddocument


mwe







share|improve this question












How can you align the text inside labels with pgfplots? In the example below, I would like to align both the k's and the m's.



documentclass[preview]standalone
usepackagepgfplots
begindocument
begintikzpicture
beginaxis[
title=$y=kx+m$,
ylabel=$y$,
xlabel=$x$,
legend cell align=left,
]
addplot[blue, domain=1:4, samples=4] 10*x - 5;
addlegendentry$k = 10$, $m = -5$
addplot[red, domain=1:4, samples=4] 2*x + 3;
addlegendentry$k = 2$, $m = 3$
endaxis
endtikzpicture
enddocument


mwe









share|improve this question











share|improve this question




share|improve this question










asked Aug 15 at 12:11









Ema

183




183







  • 1




    have a look at tex.stackexchange.com/a/440121/36296
    – samcarter
    Aug 15 at 12:35












  • 1




    have a look at tex.stackexchange.com/a/440121/36296
    – samcarter
    Aug 15 at 12:35







1




1




have a look at tex.stackexchange.com/a/440121/36296
– samcarter
Aug 15 at 12:35




have a look at tex.stackexchange.com/a/440121/36296
– samcarter
Aug 15 at 12:35










3 Answers
3






active

oldest

votes

















up vote
4
down vote



accepted










I don't think that there is general way, at the end you have two more or less independant nodes. How at best to "synchronize" their content depends a lot on the concrete case. And almost everything that works is ok.



In your example I would probably use eqparbox:



documentclass[preview]standalone
usepackagepgfplots
usepackageeqparbox
begindocument
begintikzpicture
beginaxis[
title=$y=kx+m$,
ylabel=$y$,
xlabel=$x$,
legend cell align=left,
%legend columns=2
]
addplot[blue, domain=1:4, samples=4] 10*x - 5;
addlegendentryeqmakebox[m1][l]$k = 10$, $m = -5$
addplot[red, domain=1:4, samples=4] 2*x + 3;
addlegendentryeqmakebox[m1][l]$k = 2$, $m = 3$
endaxis
endtikzpicture
enddocument


enter image description here






share|improve this answer



























    up vote
    3
    down vote













    Quick hack:



    documentclass[preview]standalone
    usepackagepgfplots
    begindocument
    begintikzpicture
    beginaxis[
    title=$y=kx+m$,
    ylabel=$y$,
    xlabel=$x$,
    legend cell align=left,
    ]
    addplot[blue, domain=1:4, samples=4] 10*x - 5;
    addlegendentry$k = 10$, $m = -5$
    addplot[red, domain=1:4, samples=4] 2*x + 3;
    addlegendentry$k = 2$,phantom0 $m = 3$
    endaxis
    endtikzpicture
    enddocument


    enter image description here






    share|improve this answer




















    • Yes, I could also do it by inserting a hfill with the appropriate length as well, but I was wondering if there was a general way.
      – Ema
      Aug 15 at 12:32

















    up vote
    1
    down vote













    This solution is similar to the linked one except that I use an array instead of align. Note that the array is actually overlaid over a bunch of struts. Not sure why one needs to tweak arraystretch for this.



    documentclass[preview]standalone
    usepackagepgfplots

    begindocument
    begintikzpicture
    beginaxis[
    title=$y=kx+m$,
    ylabel=$y$,
    xlabel=$x$,
    legend cell align=left,
    ]
    addplot[blue, domain=1:4, samples=4] 10*x - 5;
    addplot[red, domain=1:4, samples=4] 2*x + 3;
    legendsmashdefarraystretch1.15% align tabular to struts
    $beginarray[t]@k=nulll@$ $m=nulll@
    10,&-5\
    2,&3
    endarray$strut,strut
    endaxis
    endtikzpicture
    enddocument


    demo






    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%2f446131%2faligning-legends-in-multiple-columns-with-pgfplots%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      I don't think that there is general way, at the end you have two more or less independant nodes. How at best to "synchronize" their content depends a lot on the concrete case. And almost everything that works is ok.



      In your example I would probably use eqparbox:



      documentclass[preview]standalone
      usepackagepgfplots
      usepackageeqparbox
      begindocument
      begintikzpicture
      beginaxis[
      title=$y=kx+m$,
      ylabel=$y$,
      xlabel=$x$,
      legend cell align=left,
      %legend columns=2
      ]
      addplot[blue, domain=1:4, samples=4] 10*x - 5;
      addlegendentryeqmakebox[m1][l]$k = 10$, $m = -5$
      addplot[red, domain=1:4, samples=4] 2*x + 3;
      addlegendentryeqmakebox[m1][l]$k = 2$, $m = 3$
      endaxis
      endtikzpicture
      enddocument


      enter image description here






      share|improve this answer
























        up vote
        4
        down vote



        accepted










        I don't think that there is general way, at the end you have two more or less independant nodes. How at best to "synchronize" their content depends a lot on the concrete case. And almost everything that works is ok.



        In your example I would probably use eqparbox:



        documentclass[preview]standalone
        usepackagepgfplots
        usepackageeqparbox
        begindocument
        begintikzpicture
        beginaxis[
        title=$y=kx+m$,
        ylabel=$y$,
        xlabel=$x$,
        legend cell align=left,
        %legend columns=2
        ]
        addplot[blue, domain=1:4, samples=4] 10*x - 5;
        addlegendentryeqmakebox[m1][l]$k = 10$, $m = -5$
        addplot[red, domain=1:4, samples=4] 2*x + 3;
        addlegendentryeqmakebox[m1][l]$k = 2$, $m = 3$
        endaxis
        endtikzpicture
        enddocument


        enter image description here






        share|improve this answer






















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          I don't think that there is general way, at the end you have two more or less independant nodes. How at best to "synchronize" their content depends a lot on the concrete case. And almost everything that works is ok.



          In your example I would probably use eqparbox:



          documentclass[preview]standalone
          usepackagepgfplots
          usepackageeqparbox
          begindocument
          begintikzpicture
          beginaxis[
          title=$y=kx+m$,
          ylabel=$y$,
          xlabel=$x$,
          legend cell align=left,
          %legend columns=2
          ]
          addplot[blue, domain=1:4, samples=4] 10*x - 5;
          addlegendentryeqmakebox[m1][l]$k = 10$, $m = -5$
          addplot[red, domain=1:4, samples=4] 2*x + 3;
          addlegendentryeqmakebox[m1][l]$k = 2$, $m = 3$
          endaxis
          endtikzpicture
          enddocument


          enter image description here






          share|improve this answer












          I don't think that there is general way, at the end you have two more or less independant nodes. How at best to "synchronize" their content depends a lot on the concrete case. And almost everything that works is ok.



          In your example I would probably use eqparbox:



          documentclass[preview]standalone
          usepackagepgfplots
          usepackageeqparbox
          begindocument
          begintikzpicture
          beginaxis[
          title=$y=kx+m$,
          ylabel=$y$,
          xlabel=$x$,
          legend cell align=left,
          %legend columns=2
          ]
          addplot[blue, domain=1:4, samples=4] 10*x - 5;
          addlegendentryeqmakebox[m1][l]$k = 10$, $m = -5$
          addplot[red, domain=1:4, samples=4] 2*x + 3;
          addlegendentryeqmakebox[m1][l]$k = 2$, $m = 3$
          endaxis
          endtikzpicture
          enddocument


          enter image description here







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 15 at 12:58









          Ulrike Fischer

          177k7281649




          177k7281649




















              up vote
              3
              down vote













              Quick hack:



              documentclass[preview]standalone
              usepackagepgfplots
              begindocument
              begintikzpicture
              beginaxis[
              title=$y=kx+m$,
              ylabel=$y$,
              xlabel=$x$,
              legend cell align=left,
              ]
              addplot[blue, domain=1:4, samples=4] 10*x - 5;
              addlegendentry$k = 10$, $m = -5$
              addplot[red, domain=1:4, samples=4] 2*x + 3;
              addlegendentry$k = 2$,phantom0 $m = 3$
              endaxis
              endtikzpicture
              enddocument


              enter image description here






              share|improve this answer




















              • Yes, I could also do it by inserting a hfill with the appropriate length as well, but I was wondering if there was a general way.
                – Ema
                Aug 15 at 12:32














              up vote
              3
              down vote













              Quick hack:



              documentclass[preview]standalone
              usepackagepgfplots
              begindocument
              begintikzpicture
              beginaxis[
              title=$y=kx+m$,
              ylabel=$y$,
              xlabel=$x$,
              legend cell align=left,
              ]
              addplot[blue, domain=1:4, samples=4] 10*x - 5;
              addlegendentry$k = 10$, $m = -5$
              addplot[red, domain=1:4, samples=4] 2*x + 3;
              addlegendentry$k = 2$,phantom0 $m = 3$
              endaxis
              endtikzpicture
              enddocument


              enter image description here






              share|improve this answer




















              • Yes, I could also do it by inserting a hfill with the appropriate length as well, but I was wondering if there was a general way.
                – Ema
                Aug 15 at 12:32












              up vote
              3
              down vote










              up vote
              3
              down vote









              Quick hack:



              documentclass[preview]standalone
              usepackagepgfplots
              begindocument
              begintikzpicture
              beginaxis[
              title=$y=kx+m$,
              ylabel=$y$,
              xlabel=$x$,
              legend cell align=left,
              ]
              addplot[blue, domain=1:4, samples=4] 10*x - 5;
              addlegendentry$k = 10$, $m = -5$
              addplot[red, domain=1:4, samples=4] 2*x + 3;
              addlegendentry$k = 2$,phantom0 $m = 3$
              endaxis
              endtikzpicture
              enddocument


              enter image description here






              share|improve this answer












              Quick hack:



              documentclass[preview]standalone
              usepackagepgfplots
              begindocument
              begintikzpicture
              beginaxis[
              title=$y=kx+m$,
              ylabel=$y$,
              xlabel=$x$,
              legend cell align=left,
              ]
              addplot[blue, domain=1:4, samples=4] 10*x - 5;
              addlegendentry$k = 10$, $m = -5$
              addplot[red, domain=1:4, samples=4] 2*x + 3;
              addlegendentry$k = 2$,phantom0 $m = 3$
              endaxis
              endtikzpicture
              enddocument


              enter image description here







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Aug 15 at 12:21









              samcarter

              74k784238




              74k784238











              • Yes, I could also do it by inserting a hfill with the appropriate length as well, but I was wondering if there was a general way.
                – Ema
                Aug 15 at 12:32
















              • Yes, I could also do it by inserting a hfill with the appropriate length as well, but I was wondering if there was a general way.
                – Ema
                Aug 15 at 12:32















              Yes, I could also do it by inserting a hfill with the appropriate length as well, but I was wondering if there was a general way.
              – Ema
              Aug 15 at 12:32




              Yes, I could also do it by inserting a hfill with the appropriate length as well, but I was wondering if there was a general way.
              – Ema
              Aug 15 at 12:32










              up vote
              1
              down vote













              This solution is similar to the linked one except that I use an array instead of align. Note that the array is actually overlaid over a bunch of struts. Not sure why one needs to tweak arraystretch for this.



              documentclass[preview]standalone
              usepackagepgfplots

              begindocument
              begintikzpicture
              beginaxis[
              title=$y=kx+m$,
              ylabel=$y$,
              xlabel=$x$,
              legend cell align=left,
              ]
              addplot[blue, domain=1:4, samples=4] 10*x - 5;
              addplot[red, domain=1:4, samples=4] 2*x + 3;
              legendsmashdefarraystretch1.15% align tabular to struts
              $beginarray[t]@k=nulll@$ $m=nulll@
              10,&-5\
              2,&3
              endarray$strut,strut
              endaxis
              endtikzpicture
              enddocument


              demo






              share|improve this answer


























                up vote
                1
                down vote













                This solution is similar to the linked one except that I use an array instead of align. Note that the array is actually overlaid over a bunch of struts. Not sure why one needs to tweak arraystretch for this.



                documentclass[preview]standalone
                usepackagepgfplots

                begindocument
                begintikzpicture
                beginaxis[
                title=$y=kx+m$,
                ylabel=$y$,
                xlabel=$x$,
                legend cell align=left,
                ]
                addplot[blue, domain=1:4, samples=4] 10*x - 5;
                addplot[red, domain=1:4, samples=4] 2*x + 3;
                legendsmashdefarraystretch1.15% align tabular to struts
                $beginarray[t]@k=nulll@$ $m=nulll@
                10,&-5\
                2,&3
                endarray$strut,strut
                endaxis
                endtikzpicture
                enddocument


                demo






                share|improve this answer
























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  This solution is similar to the linked one except that I use an array instead of align. Note that the array is actually overlaid over a bunch of struts. Not sure why one needs to tweak arraystretch for this.



                  documentclass[preview]standalone
                  usepackagepgfplots

                  begindocument
                  begintikzpicture
                  beginaxis[
                  title=$y=kx+m$,
                  ylabel=$y$,
                  xlabel=$x$,
                  legend cell align=left,
                  ]
                  addplot[blue, domain=1:4, samples=4] 10*x - 5;
                  addplot[red, domain=1:4, samples=4] 2*x + 3;
                  legendsmashdefarraystretch1.15% align tabular to struts
                  $beginarray[t]@k=nulll@$ $m=nulll@
                  10,&-5\
                  2,&3
                  endarray$strut,strut
                  endaxis
                  endtikzpicture
                  enddocument


                  demo






                  share|improve this answer














                  This solution is similar to the linked one except that I use an array instead of align. Note that the array is actually overlaid over a bunch of struts. Not sure why one needs to tweak arraystretch for this.



                  documentclass[preview]standalone
                  usepackagepgfplots

                  begindocument
                  begintikzpicture
                  beginaxis[
                  title=$y=kx+m$,
                  ylabel=$y$,
                  xlabel=$x$,
                  legend cell align=left,
                  ]
                  addplot[blue, domain=1:4, samples=4] 10*x - 5;
                  addplot[red, domain=1:4, samples=4] 2*x + 3;
                  legendsmashdefarraystretch1.15% align tabular to struts
                  $beginarray[t]@k=nulll@$ $m=nulll@
                  10,&-5\
                  2,&3
                  endarray$strut,strut
                  endaxis
                  endtikzpicture
                  enddocument


                  demo







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Aug 15 at 14:41

























                  answered Aug 15 at 14:02









                  John Kormylo

                  40.5k12363




                  40.5k12363



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f446131%2faligning-legends-in-multiple-columns-with-pgfplots%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      List of Gilmore Girls characters

                      What does second last employer means? [closed]

                      One-line joke