How can I separate the number from the unit in argument?












3















Let us say that I have a function, in which I give a number plus a unit. I would like to get only the number, is it possible ?



Here is a MWE:



documentclass{article}
usepackage[utf8]{inputenc}

newcommand{cmd}[1]{#1} % change here to capture only the number.
begin{document}
cmd{12pt} % print 12pt while I would get only 12, in a generic case.
end{document}









share|improve this question




















  • 1





    documentclass{article} defcmd#1pt{#1} begin{document} cmd12pt end{document}

    – marmot
    Apr 1 at 18:55













  • @marmot - That'll work for pt as the unit, but for em, mm, km, etc. :-)

    – Mico
    Apr 1 at 20:24













  • @Mico Yes, I know. But it does answer the question.

    – marmot
    Apr 1 at 20:25
















3















Let us say that I have a function, in which I give a number plus a unit. I would like to get only the number, is it possible ?



Here is a MWE:



documentclass{article}
usepackage[utf8]{inputenc}

newcommand{cmd}[1]{#1} % change here to capture only the number.
begin{document}
cmd{12pt} % print 12pt while I would get only 12, in a generic case.
end{document}









share|improve this question




















  • 1





    documentclass{article} defcmd#1pt{#1} begin{document} cmd12pt end{document}

    – marmot
    Apr 1 at 18:55













  • @marmot - That'll work for pt as the unit, but for em, mm, km, etc. :-)

    – Mico
    Apr 1 at 20:24













  • @Mico Yes, I know. But it does answer the question.

    – marmot
    Apr 1 at 20:25














3












3








3


0






Let us say that I have a function, in which I give a number plus a unit. I would like to get only the number, is it possible ?



Here is a MWE:



documentclass{article}
usepackage[utf8]{inputenc}

newcommand{cmd}[1]{#1} % change here to capture only the number.
begin{document}
cmd{12pt} % print 12pt while I would get only 12, in a generic case.
end{document}









share|improve this question
















Let us say that I have a function, in which I give a number plus a unit. I would like to get only the number, is it possible ?



Here is a MWE:



documentclass{article}
usepackage[utf8]{inputenc}

newcommand{cmd}[1]{#1} % change here to capture only the number.
begin{document}
cmd{12pt} % print 12pt while I would get only 12, in a generic case.
end{document}






macros lengths unit-of-measure






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 1 at 18:41









Bernard

175k776207




175k776207










asked Apr 1 at 18:38









R. NR. N

320214




320214








  • 1





    documentclass{article} defcmd#1pt{#1} begin{document} cmd12pt end{document}

    – marmot
    Apr 1 at 18:55













  • @marmot - That'll work for pt as the unit, but for em, mm, km, etc. :-)

    – Mico
    Apr 1 at 20:24













  • @Mico Yes, I know. But it does answer the question.

    – marmot
    Apr 1 at 20:25














  • 1





    documentclass{article} defcmd#1pt{#1} begin{document} cmd12pt end{document}

    – marmot
    Apr 1 at 18:55













  • @marmot - That'll work for pt as the unit, but for em, mm, km, etc. :-)

    – Mico
    Apr 1 at 20:24













  • @Mico Yes, I know. But it does answer the question.

    – marmot
    Apr 1 at 20:25








1




1





documentclass{article} defcmd#1pt{#1} begin{document} cmd12pt end{document}

– marmot
Apr 1 at 18:55







documentclass{article} defcmd#1pt{#1} begin{document} cmd12pt end{document}

– marmot
Apr 1 at 18:55















@marmot - That'll work for pt as the unit, but for em, mm, km, etc. :-)

– Mico
Apr 1 at 20:24







@marmot - That'll work for pt as the unit, but for em, mm, km, etc. :-)

– Mico
Apr 1 at 20:24















@Mico Yes, I know. But it does answer the question.

– marmot
Apr 1 at 20:25





@Mico Yes, I know. But it does answer the question.

– marmot
Apr 1 at 20:25










3 Answers
3






active

oldest

votes


















3














pgf does that without the need to invoke external programs and converts the units into points.



documentclass{article}
usepackage{pgf}

newcommand{cmd}[1]{pgfmathparse{#1}pgfmathresult}
begin{document}
cmd{12pt} cmd{1cm}
end{document}


enter image description here



Note that if you're bugged by the .0: this can easily be removed with pgfmathprintnumber[<your number format here>]{pgfmathresult} if you choose a number format that you like.






share|improve this answer































    5














    Assuming the unit consists of two characters, you can do it in an expandable way:



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn

    NewExpandableDocumentCommand{getnumber}{m}
    {
    tl_range:nnn { #1 } { 1 } { -3 } % from the first to the last but two character
    }

    ExplSyntaxOff

    begin{document}

    getnumber{12pt}, $getnumber{-47km}$, getnumber{+5.7in}, getnumber{3,14159CM}

    end{document}


    enter image description here






    share|improve this answer
























    • Could getnumber be generalized, say by taking an integer as an optional argument (with the default value of the optional argument set to 2), to handle cases of units which contain a number of characters not equal to 2? E.g., getnumber[3]{5.678mol} or getnumber[1]{1.23m}.

      – Mico
      2 days ago





















    4














    Here's a LuaLaTeX-based solution. It sets up a LaTeX macro called cmd -- a "wrapper" -- that invokes a Lua function that does all of the work. The Lua function expects its argument to consist of two parts: the first part is numeric, i.e., consists of the digits 0 thru 9, plus possibly the characters ,, ., -, and +; the part second is alphabetic, i.e., uppercase and lowercase letters, plus possibly whitespace.



    Per your typesetting objective, the function returns just the numeric, part. If the argument of cmd does not start with a numeric component, the prefix part is discarded as well. E.g., the output of arg{XX55km} is 55, and the output of cmd{km} is blank (empty).



    enter image description here



    % !TEX TS-program = lualatex
    documentclass{article}
    usepackage{luacode} % for 'luacode' environment
    begin{luacode}
    function get_num ( s )
    tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
    end
    end{luacode}
    newcommandcmd[1]{directlua{get_num("#1")}} % "wrapper" macro

    begin{document}
    cmd{12pt}, $cmd{-47km}$, cmd{+5.7in}, cmd{3,14159CM}
    end{document}





    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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      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%2f482628%2fhow-can-i-separate-the-number-from-the-unit-in-argument%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      3














      pgf does that without the need to invoke external programs and converts the units into points.



      documentclass{article}
      usepackage{pgf}

      newcommand{cmd}[1]{pgfmathparse{#1}pgfmathresult}
      begin{document}
      cmd{12pt} cmd{1cm}
      end{document}


      enter image description here



      Note that if you're bugged by the .0: this can easily be removed with pgfmathprintnumber[<your number format here>]{pgfmathresult} if you choose a number format that you like.






      share|improve this answer




























        3














        pgf does that without the need to invoke external programs and converts the units into points.



        documentclass{article}
        usepackage{pgf}

        newcommand{cmd}[1]{pgfmathparse{#1}pgfmathresult}
        begin{document}
        cmd{12pt} cmd{1cm}
        end{document}


        enter image description here



        Note that if you're bugged by the .0: this can easily be removed with pgfmathprintnumber[<your number format here>]{pgfmathresult} if you choose a number format that you like.






        share|improve this answer


























          3












          3








          3







          pgf does that without the need to invoke external programs and converts the units into points.



          documentclass{article}
          usepackage{pgf}

          newcommand{cmd}[1]{pgfmathparse{#1}pgfmathresult}
          begin{document}
          cmd{12pt} cmd{1cm}
          end{document}


          enter image description here



          Note that if you're bugged by the .0: this can easily be removed with pgfmathprintnumber[<your number format here>]{pgfmathresult} if you choose a number format that you like.






          share|improve this answer













          pgf does that without the need to invoke external programs and converts the units into points.



          documentclass{article}
          usepackage{pgf}

          newcommand{cmd}[1]{pgfmathparse{#1}pgfmathresult}
          begin{document}
          cmd{12pt} cmd{1cm}
          end{document}


          enter image description here



          Note that if you're bugged by the .0: this can easily be removed with pgfmathprintnumber[<your number format here>]{pgfmathresult} if you choose a number format that you like.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 1 at 20:30









          marmotmarmot

          114k5145276




          114k5145276























              5














              Assuming the unit consists of two characters, you can do it in an expandable way:



              documentclass{article}
              usepackage{xparse}

              ExplSyntaxOn

              NewExpandableDocumentCommand{getnumber}{m}
              {
              tl_range:nnn { #1 } { 1 } { -3 } % from the first to the last but two character
              }

              ExplSyntaxOff

              begin{document}

              getnumber{12pt}, $getnumber{-47km}$, getnumber{+5.7in}, getnumber{3,14159CM}

              end{document}


              enter image description here






              share|improve this answer
























              • Could getnumber be generalized, say by taking an integer as an optional argument (with the default value of the optional argument set to 2), to handle cases of units which contain a number of characters not equal to 2? E.g., getnumber[3]{5.678mol} or getnumber[1]{1.23m}.

                – Mico
                2 days ago


















              5














              Assuming the unit consists of two characters, you can do it in an expandable way:



              documentclass{article}
              usepackage{xparse}

              ExplSyntaxOn

              NewExpandableDocumentCommand{getnumber}{m}
              {
              tl_range:nnn { #1 } { 1 } { -3 } % from the first to the last but two character
              }

              ExplSyntaxOff

              begin{document}

              getnumber{12pt}, $getnumber{-47km}$, getnumber{+5.7in}, getnumber{3,14159CM}

              end{document}


              enter image description here






              share|improve this answer
























              • Could getnumber be generalized, say by taking an integer as an optional argument (with the default value of the optional argument set to 2), to handle cases of units which contain a number of characters not equal to 2? E.g., getnumber[3]{5.678mol} or getnumber[1]{1.23m}.

                – Mico
                2 days ago
















              5












              5








              5







              Assuming the unit consists of two characters, you can do it in an expandable way:



              documentclass{article}
              usepackage{xparse}

              ExplSyntaxOn

              NewExpandableDocumentCommand{getnumber}{m}
              {
              tl_range:nnn { #1 } { 1 } { -3 } % from the first to the last but two character
              }

              ExplSyntaxOff

              begin{document}

              getnumber{12pt}, $getnumber{-47km}$, getnumber{+5.7in}, getnumber{3,14159CM}

              end{document}


              enter image description here






              share|improve this answer













              Assuming the unit consists of two characters, you can do it in an expandable way:



              documentclass{article}
              usepackage{xparse}

              ExplSyntaxOn

              NewExpandableDocumentCommand{getnumber}{m}
              {
              tl_range:nnn { #1 } { 1 } { -3 } % from the first to the last but two character
              }

              ExplSyntaxOff

              begin{document}

              getnumber{12pt}, $getnumber{-47km}$, getnumber{+5.7in}, getnumber{3,14159CM}

              end{document}


              enter image description here







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Apr 1 at 20:30









              egregegreg

              731k8919303252




              731k8919303252













              • Could getnumber be generalized, say by taking an integer as an optional argument (with the default value of the optional argument set to 2), to handle cases of units which contain a number of characters not equal to 2? E.g., getnumber[3]{5.678mol} or getnumber[1]{1.23m}.

                – Mico
                2 days ago





















              • Could getnumber be generalized, say by taking an integer as an optional argument (with the default value of the optional argument set to 2), to handle cases of units which contain a number of characters not equal to 2? E.g., getnumber[3]{5.678mol} or getnumber[1]{1.23m}.

                – Mico
                2 days ago



















              Could getnumber be generalized, say by taking an integer as an optional argument (with the default value of the optional argument set to 2), to handle cases of units which contain a number of characters not equal to 2? E.g., getnumber[3]{5.678mol} or getnumber[1]{1.23m}.

              – Mico
              2 days ago







              Could getnumber be generalized, say by taking an integer as an optional argument (with the default value of the optional argument set to 2), to handle cases of units which contain a number of characters not equal to 2? E.g., getnumber[3]{5.678mol} or getnumber[1]{1.23m}.

              – Mico
              2 days ago













              4














              Here's a LuaLaTeX-based solution. It sets up a LaTeX macro called cmd -- a "wrapper" -- that invokes a Lua function that does all of the work. The Lua function expects its argument to consist of two parts: the first part is numeric, i.e., consists of the digits 0 thru 9, plus possibly the characters ,, ., -, and +; the part second is alphabetic, i.e., uppercase and lowercase letters, plus possibly whitespace.



              Per your typesetting objective, the function returns just the numeric, part. If the argument of cmd does not start with a numeric component, the prefix part is discarded as well. E.g., the output of arg{XX55km} is 55, and the output of cmd{km} is blank (empty).



              enter image description here



              % !TEX TS-program = lualatex
              documentclass{article}
              usepackage{luacode} % for 'luacode' environment
              begin{luacode}
              function get_num ( s )
              tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
              end
              end{luacode}
              newcommandcmd[1]{directlua{get_num("#1")}} % "wrapper" macro

              begin{document}
              cmd{12pt}, $cmd{-47km}$, cmd{+5.7in}, cmd{3,14159CM}
              end{document}





              share|improve this answer






























                4














                Here's a LuaLaTeX-based solution. It sets up a LaTeX macro called cmd -- a "wrapper" -- that invokes a Lua function that does all of the work. The Lua function expects its argument to consist of two parts: the first part is numeric, i.e., consists of the digits 0 thru 9, plus possibly the characters ,, ., -, and +; the part second is alphabetic, i.e., uppercase and lowercase letters, plus possibly whitespace.



                Per your typesetting objective, the function returns just the numeric, part. If the argument of cmd does not start with a numeric component, the prefix part is discarded as well. E.g., the output of arg{XX55km} is 55, and the output of cmd{km} is blank (empty).



                enter image description here



                % !TEX TS-program = lualatex
                documentclass{article}
                usepackage{luacode} % for 'luacode' environment
                begin{luacode}
                function get_num ( s )
                tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
                end
                end{luacode}
                newcommandcmd[1]{directlua{get_num("#1")}} % "wrapper" macro

                begin{document}
                cmd{12pt}, $cmd{-47km}$, cmd{+5.7in}, cmd{3,14159CM}
                end{document}





                share|improve this answer




























                  4












                  4








                  4







                  Here's a LuaLaTeX-based solution. It sets up a LaTeX macro called cmd -- a "wrapper" -- that invokes a Lua function that does all of the work. The Lua function expects its argument to consist of two parts: the first part is numeric, i.e., consists of the digits 0 thru 9, plus possibly the characters ,, ., -, and +; the part second is alphabetic, i.e., uppercase and lowercase letters, plus possibly whitespace.



                  Per your typesetting objective, the function returns just the numeric, part. If the argument of cmd does not start with a numeric component, the prefix part is discarded as well. E.g., the output of arg{XX55km} is 55, and the output of cmd{km} is blank (empty).



                  enter image description here



                  % !TEX TS-program = lualatex
                  documentclass{article}
                  usepackage{luacode} % for 'luacode' environment
                  begin{luacode}
                  function get_num ( s )
                  tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
                  end
                  end{luacode}
                  newcommandcmd[1]{directlua{get_num("#1")}} % "wrapper" macro

                  begin{document}
                  cmd{12pt}, $cmd{-47km}$, cmd{+5.7in}, cmd{3,14159CM}
                  end{document}





                  share|improve this answer















                  Here's a LuaLaTeX-based solution. It sets up a LaTeX macro called cmd -- a "wrapper" -- that invokes a Lua function that does all of the work. The Lua function expects its argument to consist of two parts: the first part is numeric, i.e., consists of the digits 0 thru 9, plus possibly the characters ,, ., -, and +; the part second is alphabetic, i.e., uppercase and lowercase letters, plus possibly whitespace.



                  Per your typesetting objective, the function returns just the numeric, part. If the argument of cmd does not start with a numeric component, the prefix part is discarded as well. E.g., the output of arg{XX55km} is 55, and the output of cmd{km} is blank (empty).



                  enter image description here



                  % !TEX TS-program = lualatex
                  documentclass{article}
                  usepackage{luacode} % for 'luacode' environment
                  begin{luacode}
                  function get_num ( s )
                  tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
                  end
                  end{luacode}
                  newcommandcmd[1]{directlua{get_num("#1")}} % "wrapper" macro

                  begin{document}
                  cmd{12pt}, $cmd{-47km}$, cmd{+5.7in}, cmd{3,14159CM}
                  end{document}






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Apr 1 at 20:22

























                  answered Apr 1 at 20:05









                  MicoMico

                  285k31388778




                  285k31388778






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to TeX - LaTeX Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f482628%2fhow-can-i-separate-the-number-from-the-unit-in-argument%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      數位音樂下載

                      When can things happen in Etherscan, such as the picture below?

                      格利澤436b