LyX | change macro argument order











up vote
2
down vote

favorite












In LyX, is it possible to change the order of required and option arguments of a macro?



I want to make the option argument come after the required argument in this macro.



If it's possible, how can I do it?



Screenshot of the macro










share|improve this question














bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • It is possible in LaTeX of course. LyX is only a editor that hides (too much, in my opinion) of LaTeX
    – Christian Hupfer
    Apr 14 '17 at 15:40






  • 1




    I'm looking for a solution in LyX, if that's possible, to avoid writing code.
    – Gal Grünfeld
    Apr 15 '17 at 9:50






  • 1




    This way you learn nothing and will be a slave of TeX.SE ;-)
    – Christian Hupfer
    Apr 15 '17 at 9:51






  • 1




    Haha. I've no problem with writing code and learning relevant LaTeX/TeX code - I just want to automate as much as possible to decrease time needed to do things. And that's something that LyX is quite good at doing. Also, I want to have as least code as possible in my LyX document due to elegance (I render things automatically in LyX).
    – Gal Grünfeld
    Apr 15 '17 at 9:54

















up vote
2
down vote

favorite












In LyX, is it possible to change the order of required and option arguments of a macro?



I want to make the option argument come after the required argument in this macro.



If it's possible, how can I do it?



Screenshot of the macro










share|improve this question














bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.















  • It is possible in LaTeX of course. LyX is only a editor that hides (too much, in my opinion) of LaTeX
    – Christian Hupfer
    Apr 14 '17 at 15:40






  • 1




    I'm looking for a solution in LyX, if that's possible, to avoid writing code.
    – Gal Grünfeld
    Apr 15 '17 at 9:50






  • 1




    This way you learn nothing and will be a slave of TeX.SE ;-)
    – Christian Hupfer
    Apr 15 '17 at 9:51






  • 1




    Haha. I've no problem with writing code and learning relevant LaTeX/TeX code - I just want to automate as much as possible to decrease time needed to do things. And that's something that LyX is quite good at doing. Also, I want to have as least code as possible in my LyX document due to elegance (I render things automatically in LyX).
    – Gal Grünfeld
    Apr 15 '17 at 9:54















up vote
2
down vote

favorite









up vote
2
down vote

favorite











In LyX, is it possible to change the order of required and option arguments of a macro?



I want to make the option argument come after the required argument in this macro.



If it's possible, how can I do it?



Screenshot of the macro










share|improve this question













In LyX, is it possible to change the order of required and option arguments of a macro?



I want to make the option argument come after the required argument in this macro.



If it's possible, how can I do it?



Screenshot of the macro







macros lyx arguments






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 14 '17 at 14:30









Gal Grünfeld

14813




14813





bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







bumped to the homepage by Community yesterday


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.














  • It is possible in LaTeX of course. LyX is only a editor that hides (too much, in my opinion) of LaTeX
    – Christian Hupfer
    Apr 14 '17 at 15:40






  • 1




    I'm looking for a solution in LyX, if that's possible, to avoid writing code.
    – Gal Grünfeld
    Apr 15 '17 at 9:50






  • 1




    This way you learn nothing and will be a slave of TeX.SE ;-)
    – Christian Hupfer
    Apr 15 '17 at 9:51






  • 1




    Haha. I've no problem with writing code and learning relevant LaTeX/TeX code - I just want to automate as much as possible to decrease time needed to do things. And that's something that LyX is quite good at doing. Also, I want to have as least code as possible in my LyX document due to elegance (I render things automatically in LyX).
    – Gal Grünfeld
    Apr 15 '17 at 9:54




















  • It is possible in LaTeX of course. LyX is only a editor that hides (too much, in my opinion) of LaTeX
    – Christian Hupfer
    Apr 14 '17 at 15:40






  • 1




    I'm looking for a solution in LyX, if that's possible, to avoid writing code.
    – Gal Grünfeld
    Apr 15 '17 at 9:50






  • 1




    This way you learn nothing and will be a slave of TeX.SE ;-)
    – Christian Hupfer
    Apr 15 '17 at 9:51






  • 1




    Haha. I've no problem with writing code and learning relevant LaTeX/TeX code - I just want to automate as much as possible to decrease time needed to do things. And that's something that LyX is quite good at doing. Also, I want to have as least code as possible in my LyX document due to elegance (I render things automatically in LyX).
    – Gal Grünfeld
    Apr 15 '17 at 9:54


















It is possible in LaTeX of course. LyX is only a editor that hides (too much, in my opinion) of LaTeX
– Christian Hupfer
Apr 14 '17 at 15:40




It is possible in LaTeX of course. LyX is only a editor that hides (too much, in my opinion) of LaTeX
– Christian Hupfer
Apr 14 '17 at 15:40




1




1




I'm looking for a solution in LyX, if that's possible, to avoid writing code.
– Gal Grünfeld
Apr 15 '17 at 9:50




I'm looking for a solution in LyX, if that's possible, to avoid writing code.
– Gal Grünfeld
Apr 15 '17 at 9:50




1




1




This way you learn nothing and will be a slave of TeX.SE ;-)
– Christian Hupfer
Apr 15 '17 at 9:51




This way you learn nothing and will be a slave of TeX.SE ;-)
– Christian Hupfer
Apr 15 '17 at 9:51




1




1




Haha. I've no problem with writing code and learning relevant LaTeX/TeX code - I just want to automate as much as possible to decrease time needed to do things. And that's something that LyX is quite good at doing. Also, I want to have as least code as possible in my LyX document due to elegance (I render things automatically in LyX).
– Gal Grünfeld
Apr 15 '17 at 9:54






Haha. I've no problem with writing code and learning relevant LaTeX/TeX code - I just want to automate as much as possible to decrease time needed to do things. And that's something that LyX is quite good at doing. Also, I want to have as least code as possible in my LyX document due to elegance (I render things automatically in LyX).
– Gal Grünfeld
Apr 15 '17 at 9:54












1 Answer
1






active

oldest

votes

















up vote
0
down vote













I don't recommend doing this because it'll be confusing to anyone else who reads your document. It will also not work for all macros, particularly ones that play around with category codes prior to reading their arguments. That said it is possible.



Here's some LaTeX code that defines a macro flip that will flip the required and optional arguments of a macro that's defined to have exactly one required argument and one optional argument.



usepackage{etoolbox}

makeatletter
newcommand*flip[1]{%
cslet{flip@string#1}#1%
longdef#1##1{%
@ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
}%
}
longdef@flip#1#2[#3]{%
csuse{flip@string#1}[#3]{#2}%
}
makeatother


For example, given the macro



newcommandfoo[2][none]{%
par
Required argument: #2\
Optional argument: #1%
}


you can write flipfoo one time and it'll redefine foo to take a required argument followed by an optional argument. Here's a complete document illustrating this.



documentclass{article}
usepackage{parskip}
usepackage{etoolbox}

makeatletter
newcommand*flip[1]{%
cslet{flip@string#1}#1%
longdef#1##1{%
@ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
}%
}
longdef@flip#1#2[#3]{%
csuse{flip@string#1}[#3]{#2}%
}
makeatother

newcommandfoo[2][none]{%
par
Required argument: #2\
Optional argument: #1%
}


begin{document}

Normal use:
foo{blah}
foo[option]{asdf}

Now we can flip the arguments:
flipfoo
foo{blah}
foo{asdf}[option]
end{document}


enter image description here



I've never used LyX before now, but I was able to insert my code into the preamble and achieve the same effect.



Note that flip doesn't respect the lack of long on a macro (i.e., macros defined via the def family without long or defined via newcommand*). This is likely to lead to confusing errors in some cases. It's possible to deal with this similar to how etoolbox patches macros, but it didn't seem worth it to do so for this answer.



It would also be possible to define flip such that it didn't redefine its argument but instead such that flipfoo{required}[optional] expanded to foo[optional]{required}. Something like (untested)



longdefflip#1#2[#3]{#1[#3]{#2}}


which would turn the optional argument into a required one, but if you don't have the optional argument, there's no reason to use the flip.



In conclusion, it's possible to do, but you probably shouldn't do it.






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: 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%2f364738%2flyx-change-macro-argument-order%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    I don't recommend doing this because it'll be confusing to anyone else who reads your document. It will also not work for all macros, particularly ones that play around with category codes prior to reading their arguments. That said it is possible.



    Here's some LaTeX code that defines a macro flip that will flip the required and optional arguments of a macro that's defined to have exactly one required argument and one optional argument.



    usepackage{etoolbox}

    makeatletter
    newcommand*flip[1]{%
    cslet{flip@string#1}#1%
    longdef#1##1{%
    @ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
    }%
    }
    longdef@flip#1#2[#3]{%
    csuse{flip@string#1}[#3]{#2}%
    }
    makeatother


    For example, given the macro



    newcommandfoo[2][none]{%
    par
    Required argument: #2\
    Optional argument: #1%
    }


    you can write flipfoo one time and it'll redefine foo to take a required argument followed by an optional argument. Here's a complete document illustrating this.



    documentclass{article}
    usepackage{parskip}
    usepackage{etoolbox}

    makeatletter
    newcommand*flip[1]{%
    cslet{flip@string#1}#1%
    longdef#1##1{%
    @ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
    }%
    }
    longdef@flip#1#2[#3]{%
    csuse{flip@string#1}[#3]{#2}%
    }
    makeatother

    newcommandfoo[2][none]{%
    par
    Required argument: #2\
    Optional argument: #1%
    }


    begin{document}

    Normal use:
    foo{blah}
    foo[option]{asdf}

    Now we can flip the arguments:
    flipfoo
    foo{blah}
    foo{asdf}[option]
    end{document}


    enter image description here



    I've never used LyX before now, but I was able to insert my code into the preamble and achieve the same effect.



    Note that flip doesn't respect the lack of long on a macro (i.e., macros defined via the def family without long or defined via newcommand*). This is likely to lead to confusing errors in some cases. It's possible to deal with this similar to how etoolbox patches macros, but it didn't seem worth it to do so for this answer.



    It would also be possible to define flip such that it didn't redefine its argument but instead such that flipfoo{required}[optional] expanded to foo[optional]{required}. Something like (untested)



    longdefflip#1#2[#3]{#1[#3]{#2}}


    which would turn the optional argument into a required one, but if you don't have the optional argument, there's no reason to use the flip.



    In conclusion, it's possible to do, but you probably shouldn't do it.






    share|improve this answer

























      up vote
      0
      down vote













      I don't recommend doing this because it'll be confusing to anyone else who reads your document. It will also not work for all macros, particularly ones that play around with category codes prior to reading their arguments. That said it is possible.



      Here's some LaTeX code that defines a macro flip that will flip the required and optional arguments of a macro that's defined to have exactly one required argument and one optional argument.



      usepackage{etoolbox}

      makeatletter
      newcommand*flip[1]{%
      cslet{flip@string#1}#1%
      longdef#1##1{%
      @ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
      }%
      }
      longdef@flip#1#2[#3]{%
      csuse{flip@string#1}[#3]{#2}%
      }
      makeatother


      For example, given the macro



      newcommandfoo[2][none]{%
      par
      Required argument: #2\
      Optional argument: #1%
      }


      you can write flipfoo one time and it'll redefine foo to take a required argument followed by an optional argument. Here's a complete document illustrating this.



      documentclass{article}
      usepackage{parskip}
      usepackage{etoolbox}

      makeatletter
      newcommand*flip[1]{%
      cslet{flip@string#1}#1%
      longdef#1##1{%
      @ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
      }%
      }
      longdef@flip#1#2[#3]{%
      csuse{flip@string#1}[#3]{#2}%
      }
      makeatother

      newcommandfoo[2][none]{%
      par
      Required argument: #2\
      Optional argument: #1%
      }


      begin{document}

      Normal use:
      foo{blah}
      foo[option]{asdf}

      Now we can flip the arguments:
      flipfoo
      foo{blah}
      foo{asdf}[option]
      end{document}


      enter image description here



      I've never used LyX before now, but I was able to insert my code into the preamble and achieve the same effect.



      Note that flip doesn't respect the lack of long on a macro (i.e., macros defined via the def family without long or defined via newcommand*). This is likely to lead to confusing errors in some cases. It's possible to deal with this similar to how etoolbox patches macros, but it didn't seem worth it to do so for this answer.



      It would also be possible to define flip such that it didn't redefine its argument but instead such that flipfoo{required}[optional] expanded to foo[optional]{required}. Something like (untested)



      longdefflip#1#2[#3]{#1[#3]{#2}}


      which would turn the optional argument into a required one, but if you don't have the optional argument, there's no reason to use the flip.



      In conclusion, it's possible to do, but you probably shouldn't do it.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        I don't recommend doing this because it'll be confusing to anyone else who reads your document. It will also not work for all macros, particularly ones that play around with category codes prior to reading their arguments. That said it is possible.



        Here's some LaTeX code that defines a macro flip that will flip the required and optional arguments of a macro that's defined to have exactly one required argument and one optional argument.



        usepackage{etoolbox}

        makeatletter
        newcommand*flip[1]{%
        cslet{flip@string#1}#1%
        longdef#1##1{%
        @ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
        }%
        }
        longdef@flip#1#2[#3]{%
        csuse{flip@string#1}[#3]{#2}%
        }
        makeatother


        For example, given the macro



        newcommandfoo[2][none]{%
        par
        Required argument: #2\
        Optional argument: #1%
        }


        you can write flipfoo one time and it'll redefine foo to take a required argument followed by an optional argument. Here's a complete document illustrating this.



        documentclass{article}
        usepackage{parskip}
        usepackage{etoolbox}

        makeatletter
        newcommand*flip[1]{%
        cslet{flip@string#1}#1%
        longdef#1##1{%
        @ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
        }%
        }
        longdef@flip#1#2[#3]{%
        csuse{flip@string#1}[#3]{#2}%
        }
        makeatother

        newcommandfoo[2][none]{%
        par
        Required argument: #2\
        Optional argument: #1%
        }


        begin{document}

        Normal use:
        foo{blah}
        foo[option]{asdf}

        Now we can flip the arguments:
        flipfoo
        foo{blah}
        foo{asdf}[option]
        end{document}


        enter image description here



        I've never used LyX before now, but I was able to insert my code into the preamble and achieve the same effect.



        Note that flip doesn't respect the lack of long on a macro (i.e., macros defined via the def family without long or defined via newcommand*). This is likely to lead to confusing errors in some cases. It's possible to deal with this similar to how etoolbox patches macros, but it didn't seem worth it to do so for this answer.



        It would also be possible to define flip such that it didn't redefine its argument but instead such that flipfoo{required}[optional] expanded to foo[optional]{required}. Something like (untested)



        longdefflip#1#2[#3]{#1[#3]{#2}}


        which would turn the optional argument into a required one, but if you don't have the optional argument, there's no reason to use the flip.



        In conclusion, it's possible to do, but you probably shouldn't do it.






        share|improve this answer












        I don't recommend doing this because it'll be confusing to anyone else who reads your document. It will also not work for all macros, particularly ones that play around with category codes prior to reading their arguments. That said it is possible.



        Here's some LaTeX code that defines a macro flip that will flip the required and optional arguments of a macro that's defined to have exactly one required argument and one optional argument.



        usepackage{etoolbox}

        makeatletter
        newcommand*flip[1]{%
        cslet{flip@string#1}#1%
        longdef#1##1{%
        @ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
        }%
        }
        longdef@flip#1#2[#3]{%
        csuse{flip@string#1}[#3]{#2}%
        }
        makeatother


        For example, given the macro



        newcommandfoo[2][none]{%
        par
        Required argument: #2\
        Optional argument: #1%
        }


        you can write flipfoo one time and it'll redefine foo to take a required argument followed by an optional argument. Here's a complete document illustrating this.



        documentclass{article}
        usepackage{parskip}
        usepackage{etoolbox}

        makeatletter
        newcommand*flip[1]{%
        cslet{flip@string#1}#1%
        longdef#1##1{%
        @ifnextchar[{@flip{#1}{##1}}{csuse{flip@string#1}{##1}}%
        }%
        }
        longdef@flip#1#2[#3]{%
        csuse{flip@string#1}[#3]{#2}%
        }
        makeatother

        newcommandfoo[2][none]{%
        par
        Required argument: #2\
        Optional argument: #1%
        }


        begin{document}

        Normal use:
        foo{blah}
        foo[option]{asdf}

        Now we can flip the arguments:
        flipfoo
        foo{blah}
        foo{asdf}[option]
        end{document}


        enter image description here



        I've never used LyX before now, but I was able to insert my code into the preamble and achieve the same effect.



        Note that flip doesn't respect the lack of long on a macro (i.e., macros defined via the def family without long or defined via newcommand*). This is likely to lead to confusing errors in some cases. It's possible to deal with this similar to how etoolbox patches macros, but it didn't seem worth it to do so for this answer.



        It would also be possible to define flip such that it didn't redefine its argument but instead such that flipfoo{required}[optional] expanded to foo[optional]{required}. Something like (untested)



        longdefflip#1#2[#3]{#1[#3]{#2}}


        which would turn the optional argument into a required one, but if you don't have the optional argument, there's no reason to use the flip.



        In conclusion, it's possible to do, but you probably shouldn't do it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jun 26 '17 at 16:21









        TH.

        46.9k10128195




        46.9k10128195






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f364738%2flyx-change-macro-argument-order%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

            Accessing regular linux commands in Huawei's Dopra Linux

            Can't connect RFCOMM socket: Host is down

            Kernel panic - not syncing: Fatal Exception in Interrupt