How can I separate the number from the unit in argument? The Next CEO of Stack OverflowA macro that expands to the length of its argumentMeasuring the distance from text to the top of the pageHow to add a unit to a command argument?Extracting the basename from a filepath argumentWrapper for siunitx' SI macro to automatically split number and unitHow do I use an auxilliary file for my own commands?Is there an `ex` unit equivalent for the capital 'X' in LaTeXDuplicate and modify section hierarchyMultiple Choice Answer Key in exam package at the end of documentCan one use the Potrzebie unit system in (La)TeX?

Is it possible to make a 9x9 table fit within the default margins?

What are the unusually-enlarged wing sections on this P-38 Lightning?

Prodigo = pro + ago?

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact

Could a dragon use its wings to swim?

How can a day be of 24 hours?

How do I keep Mac Emacs from trapping M-`?

Plausibility of squid whales

What does it mean 'exit 1' for a job status after rclone sync

Is there a rule of thumb for determining the amount one should accept for of a settlement offer?

What is the difference between 'contrib' and 'non-free' packages repositories?

Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?

Creating a script with console commands

Man transported from Alternate World into ours by a Neutrino Detector

Find the majority element, which appears more than half the time

Salesforce opportunity stages

Why was Sir Cadogan fired?

How to pronounce fünf in 45

Can you teleport closer to a creature you are Frightened of?

What day is it again?

Does int main() need a declaration on C++?

Can Sri Krishna be called 'a person'?

Shortening a title without changing its meaning

What did the word "leisure" mean in late 18th Century usage?



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



The Next CEO of Stack OverflowA macro that expands to the length of its argumentMeasuring the distance from text to the top of the pageHow to add a unit to a command argument?Extracting the basename from a filepath argumentWrapper for siunitx' SI macro to automatically split number and unitHow do I use an auxilliary file for my own commands?Is there an `ex` unit equivalent for the capital 'X' in LaTeXDuplicate and modify section hierarchyMultiple Choice Answer Key in exam package at the end of documentCan one use the Potrzebie unit system in (La)TeX?










2















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:



documentclassarticle
usepackage[utf8]inputenc

newcommandcmd[1]#1 % change here to capture only the number.
begindocument
cmd12pt % print 12pt while I would get only 12, in a generic case.
enddocument









share|improve this question
























  • documentclassarticle defcmd#1pt#1 begindocument cmd12pt enddocument

    – marmot
    3 hours ago












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

    – Mico
    1 hour ago












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

    – marmot
    1 hour ago















2















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:



documentclassarticle
usepackage[utf8]inputenc

newcommandcmd[1]#1 % change here to capture only the number.
begindocument
cmd12pt % print 12pt while I would get only 12, in a generic case.
enddocument









share|improve this question
























  • documentclassarticle defcmd#1pt#1 begindocument cmd12pt enddocument

    – marmot
    3 hours ago












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

    – Mico
    1 hour ago












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

    – marmot
    1 hour ago













2












2








2








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:



documentclassarticle
usepackage[utf8]inputenc

newcommandcmd[1]#1 % change here to capture only the number.
begindocument
cmd12pt % print 12pt while I would get only 12, in a generic case.
enddocument









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:



documentclassarticle
usepackage[utf8]inputenc

newcommandcmd[1]#1 % change here to capture only the number.
begindocument
cmd12pt % print 12pt while I would get only 12, in a generic case.
enddocument






macros lengths unit-of-measure






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago









Bernard

175k776207




175k776207










asked 3 hours ago









R. NR. N

313214




313214












  • documentclassarticle defcmd#1pt#1 begindocument cmd12pt enddocument

    – marmot
    3 hours ago












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

    – Mico
    1 hour ago












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

    – marmot
    1 hour ago

















  • documentclassarticle defcmd#1pt#1 begindocument cmd12pt enddocument

    – marmot
    3 hours ago












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

    – Mico
    1 hour ago












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

    – marmot
    1 hour ago
















documentclassarticle defcmd#1pt#1 begindocument cmd12pt enddocument

– marmot
3 hours ago






documentclassarticle defcmd#1pt#1 begindocument cmd12pt enddocument

– marmot
3 hours ago














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

– Mico
1 hour ago






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

– Mico
1 hour ago














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

– marmot
1 hour ago





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

– marmot
1 hour ago










3 Answers
3






active

oldest

votes


















2














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 argXX55km is 55, and the output of cmdkm is blank (empty).



enter image description here



% !TEX TS-program = lualatex
documentclassarticle
usepackageluacode % for 'luacode' environment
beginluacode
function get_num ( s )
tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
end
endluacode
newcommandcmd[1]directluaget_num("#1") % "wrapper" macro

begindocument
cmd12pt, $cmd-47km$, cmd+5.7in, cmd3,14159CM
enddocument





share|improve this answer
































    2














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



    documentclassarticle
    usepackagexparse

    ExplSyntaxOn

    NewExpandableDocumentCommandgetnumberm

    tl_range:nnn #1 1 -3 % from the first to the last but two character


    ExplSyntaxOff

    begindocument

    getnumber12pt, $getnumber-47km$, getnumber+5.7in, getnumber3,14159CM

    enddocument


    enter image description here






    share|improve this answer






























      1














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



      documentclassarticle
      usepackagepgf

      newcommandcmd[1]pgfmathparse#1pgfmathresult
      begindocument
      cmd12pt cmd1cm
      enddocument


      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























        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









        2














        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 argXX55km is 55, and the output of cmdkm is blank (empty).



        enter image description here



        % !TEX TS-program = lualatex
        documentclassarticle
        usepackageluacode % for 'luacode' environment
        beginluacode
        function get_num ( s )
        tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
        end
        endluacode
        newcommandcmd[1]directluaget_num("#1") % "wrapper" macro

        begindocument
        cmd12pt, $cmd-47km$, cmd+5.7in, cmd3,14159CM
        enddocument





        share|improve this answer





























          2














          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 argXX55km is 55, and the output of cmdkm is blank (empty).



          enter image description here



          % !TEX TS-program = lualatex
          documentclassarticle
          usepackageluacode % for 'luacode' environment
          beginluacode
          function get_num ( s )
          tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
          end
          endluacode
          newcommandcmd[1]directluaget_num("#1") % "wrapper" macro

          begindocument
          cmd12pt, $cmd-47km$, cmd+5.7in, cmd3,14159CM
          enddocument





          share|improve this answer



























            2












            2








            2







            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 argXX55km is 55, and the output of cmdkm is blank (empty).



            enter image description here



            % !TEX TS-program = lualatex
            documentclassarticle
            usepackageluacode % for 'luacode' environment
            beginluacode
            function get_num ( s )
            tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
            end
            endluacode
            newcommandcmd[1]directluaget_num("#1") % "wrapper" macro

            begindocument
            cmd12pt, $cmd-47km$, cmd+5.7in, cmd3,14159CM
            enddocument





            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 argXX55km is 55, and the output of cmdkm is blank (empty).



            enter image description here



            % !TEX TS-program = lualatex
            documentclassarticle
            usepackageluacode % for 'luacode' environment
            beginluacode
            function get_num ( s )
            tex.sprint ( ( s:gsub ( "([%d%.%,%-%+]*)([%a%s]*)" , "%1" ) ) )
            end
            endluacode
            newcommandcmd[1]directluaget_num("#1") % "wrapper" macro

            begindocument
            cmd12pt, $cmd-47km$, cmd+5.7in, cmd3,14159CM
            enddocument






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered 1 hour ago









            MicoMico

            285k31388778




            285k31388778





















                2














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



                documentclassarticle
                usepackagexparse

                ExplSyntaxOn

                NewExpandableDocumentCommandgetnumberm

                tl_range:nnn #1 1 -3 % from the first to the last but two character


                ExplSyntaxOff

                begindocument

                getnumber12pt, $getnumber-47km$, getnumber+5.7in, getnumber3,14159CM

                enddocument


                enter image description here






                share|improve this answer



























                  2














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



                  documentclassarticle
                  usepackagexparse

                  ExplSyntaxOn

                  NewExpandableDocumentCommandgetnumberm

                  tl_range:nnn #1 1 -3 % from the first to the last but two character


                  ExplSyntaxOff

                  begindocument

                  getnumber12pt, $getnumber-47km$, getnumber+5.7in, getnumber3,14159CM

                  enddocument


                  enter image description here






                  share|improve this answer

























                    2












                    2








                    2







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



                    documentclassarticle
                    usepackagexparse

                    ExplSyntaxOn

                    NewExpandableDocumentCommandgetnumberm

                    tl_range:nnn #1 1 -3 % from the first to the last but two character


                    ExplSyntaxOff

                    begindocument

                    getnumber12pt, $getnumber-47km$, getnumber+5.7in, getnumber3,14159CM

                    enddocument


                    enter image description here






                    share|improve this answer













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



                    documentclassarticle
                    usepackagexparse

                    ExplSyntaxOn

                    NewExpandableDocumentCommandgetnumberm

                    tl_range:nnn #1 1 -3 % from the first to the last but two character


                    ExplSyntaxOff

                    begindocument

                    getnumber12pt, $getnumber-47km$, getnumber+5.7in, getnumber3,14159CM

                    enddocument


                    enter image description here







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 1 hour ago









                    egregegreg

                    731k8819293244




                    731k8819293244





















                        1














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



                        documentclassarticle
                        usepackagepgf

                        newcommandcmd[1]pgfmathparse#1pgfmathresult
                        begindocument
                        cmd12pt cmd1cm
                        enddocument


                        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



























                          1














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



                          documentclassarticle
                          usepackagepgf

                          newcommandcmd[1]pgfmathparse#1pgfmathresult
                          begindocument
                          cmd12pt cmd1cm
                          enddocument


                          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

























                            1












                            1








                            1







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



                            documentclassarticle
                            usepackagepgf

                            newcommandcmd[1]pgfmathparse#1pgfmathresult
                            begindocument
                            cmd12pt cmd1cm
                            enddocument


                            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.



                            documentclassarticle
                            usepackagepgf

                            newcommandcmd[1]pgfmathparse#1pgfmathresult
                            begindocument
                            cmd12pt cmd1cm
                            enddocument


                            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 1 hour ago









                            marmotmarmot

                            113k5145275




                            113k5145275



























                                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

                                名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

                                Is my guitar’s action too high? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)Strings too stiff on a recently purchased acoustic guitar | Cort AD880CEIs the action of my guitar really high?Μy little finger is too weak to play guitarWith guitar, how long should I give my fingers to strengthen / callous?When playing a fret the guitar sounds mutedPlaying (Barre) chords up the guitar neckI think my guitar strings are wound too tight and I can't play barre chordsF barre chord on an SG guitarHow to find to the right strings of a barre chord by feel?High action on higher fret on my steel acoustic guitar

                                香港授勳及嘉獎制度 目录 勳章及獎狀類別 嘉獎等級 授勳及嘉獎提名 統計數字 多次獲頒勳章或獎狀的人士 爭議 褫奪機制 参考文献 外部連結 参见 导航菜单統計數字一九九七年七月二日(星期三)香港特別行政區的授勳制度六七暴動領袖獲大紫荊勳章 董建華被斥為肯定殺人放火董建華授勳楊光 議員窮追猛打蘋論:顛倒是非黑白的大紫荊董讚楊光有貢獻避談暴動董拒答授勳楊光原因撤除勳銜撤除勳銜撤除勳銜特首掌「搣柴」生殺權行為失當罪 隨時「搣柴」失長糧政府刊憲 許仕仁郭炳江遭「搣柴」去年中終極上訴失敗 許仕仁郭炳江撤勳章太平紳士猛料阿Sir講古—— 「搣柴」有故一九九八年授勳名單一九九九年授勳名單二○○三年授勳名單二○○八年授勳名單二○○七年授勳名單政府總部禮賓處 - 授勳及嘉獎香港特別行政區勳章綬帶一覽(PDF)(非官方)