How to call command with optional arg using {} instead of []












0















This is a question re: Lost global definition after ifthenelse using package xifthen. I'll reference the Answer given in this question here since it's clear and clean.



MWE:



documentclass[parskip]{scrartcl}
usepackage[margin=15mm]{geometry}
usepackage{xifthen}

newcommand{test}[1]{%
ifthenelse{equal{#1}{}}{omitted}{given}%
}

begin{document}
par The optional argument was test.
par The optional argument was test[shubidu].
end{document}


Problem:



How to rewrite the command definition of test so that:



test{}


Output: omitted



test{shubidu}


Output: given



EDIT:



Thank you everyone for your tips and tricks. For some more information that may help clarify the purpose of this question:
I'm trying to define optional args using {} instead of because I'm in the process of making a TeX -> LyX transfer. LyX inserts an optional arg with if you click "Insert" on a drop-down menu and type the arg. Otherwise, LyX will insert your arg with {} if you do what I've done below. I would like to keep the formatting of what I've done below which is written with this: newcommand{BriefName}[1]{xdefBriefName{#1}} but make the argument inserted for BriefName optional. Thus, if the LyX user enters nothing in the BriefName line, the program still compiles, with an "empty" argument for the BriefName command.



enter image description here



Updated MWE:



This is more what I'm looking for. I created this thanks to this Q&A.



documentclass{article}
usepackage{xifthen}

newcommand{BriefName}[1]{ %
xdefBriefName{#1}ifxBriefNameempty
{}%
else
{#1}%
fi
}%

begin{document}
par This should be empty:BriefName{}
par This should also be empty: BriefName
par This should say Argument: BriefName{Argument}
par This should also say Argument: BriefName
end{document}









share|improve this question




















  • 4





    So strictly speaking, the argument is mandatory, but it may be empty. (Which is different from the usual meaning of optional argument in LaTeX, where one has foo without opt. arg. and foo[shubidoo] with opt. arg.)

    – moewe
    Jan 14 at 20:13






  • 1





    tex.stackexchange.com/q/53068/35864 may be interesting.

    – moewe
    Jan 14 at 20:14






  • 3





    This is confusing syntax and is not recommended, but it is possible with NewDocumentCommand{test}{+g}{IfValueF{#1}{omitted}{given}} from xparse

    – Christian Hupfer
    Jan 14 at 20:14








  • 1





    why are you using the iftenelse test there at all, rather than just use the optional argument? In both your uses test and test[shubidu] you have used the optional argument, but you have defined the argument to be optional so you could just use test if you do not need the argument.

    – David Carlisle
    Jan 14 at 20:49











  • Thank you for these comments, they were helpful. @DavidCarlisle yes I agree with you that test can be called simply as test{} and this was badly represented in my question/MWE. The issue is in LyX, once I create a command that I want to be used written like this: newcommand{BriefName}[1]{xdefBriefName{#1}} the LyX application will automatically call the command as BriefName{Argument} if the user types anything or as BriefName{} if the user types nothing. In the latter case, the document breaks as a result of this LyX feature.

    – Jalep
    Jan 15 at 15:47


















0















This is a question re: Lost global definition after ifthenelse using package xifthen. I'll reference the Answer given in this question here since it's clear and clean.



MWE:



documentclass[parskip]{scrartcl}
usepackage[margin=15mm]{geometry}
usepackage{xifthen}

newcommand{test}[1]{%
ifthenelse{equal{#1}{}}{omitted}{given}%
}

begin{document}
par The optional argument was test.
par The optional argument was test[shubidu].
end{document}


Problem:



How to rewrite the command definition of test so that:



test{}


Output: omitted



test{shubidu}


Output: given



EDIT:



Thank you everyone for your tips and tricks. For some more information that may help clarify the purpose of this question:
I'm trying to define optional args using {} instead of because I'm in the process of making a TeX -> LyX transfer. LyX inserts an optional arg with if you click "Insert" on a drop-down menu and type the arg. Otherwise, LyX will insert your arg with {} if you do what I've done below. I would like to keep the formatting of what I've done below which is written with this: newcommand{BriefName}[1]{xdefBriefName{#1}} but make the argument inserted for BriefName optional. Thus, if the LyX user enters nothing in the BriefName line, the program still compiles, with an "empty" argument for the BriefName command.



enter image description here



Updated MWE:



This is more what I'm looking for. I created this thanks to this Q&A.



documentclass{article}
usepackage{xifthen}

newcommand{BriefName}[1]{ %
xdefBriefName{#1}ifxBriefNameempty
{}%
else
{#1}%
fi
}%

begin{document}
par This should be empty:BriefName{}
par This should also be empty: BriefName
par This should say Argument: BriefName{Argument}
par This should also say Argument: BriefName
end{document}









share|improve this question




















  • 4





    So strictly speaking, the argument is mandatory, but it may be empty. (Which is different from the usual meaning of optional argument in LaTeX, where one has foo without opt. arg. and foo[shubidoo] with opt. arg.)

    – moewe
    Jan 14 at 20:13






  • 1





    tex.stackexchange.com/q/53068/35864 may be interesting.

    – moewe
    Jan 14 at 20:14






  • 3





    This is confusing syntax and is not recommended, but it is possible with NewDocumentCommand{test}{+g}{IfValueF{#1}{omitted}{given}} from xparse

    – Christian Hupfer
    Jan 14 at 20:14








  • 1





    why are you using the iftenelse test there at all, rather than just use the optional argument? In both your uses test and test[shubidu] you have used the optional argument, but you have defined the argument to be optional so you could just use test if you do not need the argument.

    – David Carlisle
    Jan 14 at 20:49











  • Thank you for these comments, they were helpful. @DavidCarlisle yes I agree with you that test can be called simply as test{} and this was badly represented in my question/MWE. The issue is in LyX, once I create a command that I want to be used written like this: newcommand{BriefName}[1]{xdefBriefName{#1}} the LyX application will automatically call the command as BriefName{Argument} if the user types anything or as BriefName{} if the user types nothing. In the latter case, the document breaks as a result of this LyX feature.

    – Jalep
    Jan 15 at 15:47
















0












0








0








This is a question re: Lost global definition after ifthenelse using package xifthen. I'll reference the Answer given in this question here since it's clear and clean.



MWE:



documentclass[parskip]{scrartcl}
usepackage[margin=15mm]{geometry}
usepackage{xifthen}

newcommand{test}[1]{%
ifthenelse{equal{#1}{}}{omitted}{given}%
}

begin{document}
par The optional argument was test.
par The optional argument was test[shubidu].
end{document}


Problem:



How to rewrite the command definition of test so that:



test{}


Output: omitted



test{shubidu}


Output: given



EDIT:



Thank you everyone for your tips and tricks. For some more information that may help clarify the purpose of this question:
I'm trying to define optional args using {} instead of because I'm in the process of making a TeX -> LyX transfer. LyX inserts an optional arg with if you click "Insert" on a drop-down menu and type the arg. Otherwise, LyX will insert your arg with {} if you do what I've done below. I would like to keep the formatting of what I've done below which is written with this: newcommand{BriefName}[1]{xdefBriefName{#1}} but make the argument inserted for BriefName optional. Thus, if the LyX user enters nothing in the BriefName line, the program still compiles, with an "empty" argument for the BriefName command.



enter image description here



Updated MWE:



This is more what I'm looking for. I created this thanks to this Q&A.



documentclass{article}
usepackage{xifthen}

newcommand{BriefName}[1]{ %
xdefBriefName{#1}ifxBriefNameempty
{}%
else
{#1}%
fi
}%

begin{document}
par This should be empty:BriefName{}
par This should also be empty: BriefName
par This should say Argument: BriefName{Argument}
par This should also say Argument: BriefName
end{document}









share|improve this question
















This is a question re: Lost global definition after ifthenelse using package xifthen. I'll reference the Answer given in this question here since it's clear and clean.



MWE:



documentclass[parskip]{scrartcl}
usepackage[margin=15mm]{geometry}
usepackage{xifthen}

newcommand{test}[1]{%
ifthenelse{equal{#1}{}}{omitted}{given}%
}

begin{document}
par The optional argument was test.
par The optional argument was test[shubidu].
end{document}


Problem:



How to rewrite the command definition of test so that:



test{}


Output: omitted



test{shubidu}


Output: given



EDIT:



Thank you everyone for your tips and tricks. For some more information that may help clarify the purpose of this question:
I'm trying to define optional args using {} instead of because I'm in the process of making a TeX -> LyX transfer. LyX inserts an optional arg with if you click "Insert" on a drop-down menu and type the arg. Otherwise, LyX will insert your arg with {} if you do what I've done below. I would like to keep the formatting of what I've done below which is written with this: newcommand{BriefName}[1]{xdefBriefName{#1}} but make the argument inserted for BriefName optional. Thus, if the LyX user enters nothing in the BriefName line, the program still compiles, with an "empty" argument for the BriefName command.



enter image description here



Updated MWE:



This is more what I'm looking for. I created this thanks to this Q&A.



documentclass{article}
usepackage{xifthen}

newcommand{BriefName}[1]{ %
xdefBriefName{#1}ifxBriefNameempty
{}%
else
{#1}%
fi
}%

begin{document}
par This should be empty:BriefName{}
par This should also be empty: BriefName
par This should say Argument: BriefName{Argument}
par This should also say Argument: BriefName
end{document}






macros






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 14 at 21:16







Jalep

















asked Jan 14 at 20:12









JalepJalep

718




718








  • 4





    So strictly speaking, the argument is mandatory, but it may be empty. (Which is different from the usual meaning of optional argument in LaTeX, where one has foo without opt. arg. and foo[shubidoo] with opt. arg.)

    – moewe
    Jan 14 at 20:13






  • 1





    tex.stackexchange.com/q/53068/35864 may be interesting.

    – moewe
    Jan 14 at 20:14






  • 3





    This is confusing syntax and is not recommended, but it is possible with NewDocumentCommand{test}{+g}{IfValueF{#1}{omitted}{given}} from xparse

    – Christian Hupfer
    Jan 14 at 20:14








  • 1





    why are you using the iftenelse test there at all, rather than just use the optional argument? In both your uses test and test[shubidu] you have used the optional argument, but you have defined the argument to be optional so you could just use test if you do not need the argument.

    – David Carlisle
    Jan 14 at 20:49











  • Thank you for these comments, they were helpful. @DavidCarlisle yes I agree with you that test can be called simply as test{} and this was badly represented in my question/MWE. The issue is in LyX, once I create a command that I want to be used written like this: newcommand{BriefName}[1]{xdefBriefName{#1}} the LyX application will automatically call the command as BriefName{Argument} if the user types anything or as BriefName{} if the user types nothing. In the latter case, the document breaks as a result of this LyX feature.

    – Jalep
    Jan 15 at 15:47
















  • 4





    So strictly speaking, the argument is mandatory, but it may be empty. (Which is different from the usual meaning of optional argument in LaTeX, where one has foo without opt. arg. and foo[shubidoo] with opt. arg.)

    – moewe
    Jan 14 at 20:13






  • 1





    tex.stackexchange.com/q/53068/35864 may be interesting.

    – moewe
    Jan 14 at 20:14






  • 3





    This is confusing syntax and is not recommended, but it is possible with NewDocumentCommand{test}{+g}{IfValueF{#1}{omitted}{given}} from xparse

    – Christian Hupfer
    Jan 14 at 20:14








  • 1





    why are you using the iftenelse test there at all, rather than just use the optional argument? In both your uses test and test[shubidu] you have used the optional argument, but you have defined the argument to be optional so you could just use test if you do not need the argument.

    – David Carlisle
    Jan 14 at 20:49











  • Thank you for these comments, they were helpful. @DavidCarlisle yes I agree with you that test can be called simply as test{} and this was badly represented in my question/MWE. The issue is in LyX, once I create a command that I want to be used written like this: newcommand{BriefName}[1]{xdefBriefName{#1}} the LyX application will automatically call the command as BriefName{Argument} if the user types anything or as BriefName{} if the user types nothing. In the latter case, the document breaks as a result of this LyX feature.

    – Jalep
    Jan 15 at 15:47










4




4





So strictly speaking, the argument is mandatory, but it may be empty. (Which is different from the usual meaning of optional argument in LaTeX, where one has foo without opt. arg. and foo[shubidoo] with opt. arg.)

– moewe
Jan 14 at 20:13





So strictly speaking, the argument is mandatory, but it may be empty. (Which is different from the usual meaning of optional argument in LaTeX, where one has foo without opt. arg. and foo[shubidoo] with opt. arg.)

– moewe
Jan 14 at 20:13




1




1





tex.stackexchange.com/q/53068/35864 may be interesting.

– moewe
Jan 14 at 20:14





tex.stackexchange.com/q/53068/35864 may be interesting.

– moewe
Jan 14 at 20:14




3




3





This is confusing syntax and is not recommended, but it is possible with NewDocumentCommand{test}{+g}{IfValueF{#1}{omitted}{given}} from xparse

– Christian Hupfer
Jan 14 at 20:14







This is confusing syntax and is not recommended, but it is possible with NewDocumentCommand{test}{+g}{IfValueF{#1}{omitted}{given}} from xparse

– Christian Hupfer
Jan 14 at 20:14






1




1





why are you using the iftenelse test there at all, rather than just use the optional argument? In both your uses test and test[shubidu] you have used the optional argument, but you have defined the argument to be optional so you could just use test if you do not need the argument.

– David Carlisle
Jan 14 at 20:49





why are you using the iftenelse test there at all, rather than just use the optional argument? In both your uses test and test[shubidu] you have used the optional argument, but you have defined the argument to be optional so you could just use test if you do not need the argument.

– David Carlisle
Jan 14 at 20:49













Thank you for these comments, they were helpful. @DavidCarlisle yes I agree with you that test can be called simply as test{} and this was badly represented in my question/MWE. The issue is in LyX, once I create a command that I want to be used written like this: newcommand{BriefName}[1]{xdefBriefName{#1}} the LyX application will automatically call the command as BriefName{Argument} if the user types anything or as BriefName{} if the user types nothing. In the latter case, the document breaks as a result of this LyX feature.

– Jalep
Jan 15 at 15:47







Thank you for these comments, they were helpful. @DavidCarlisle yes I agree with you that test can be called simply as test{} and this was badly represented in my question/MWE. The issue is in LyX, once I create a command that I want to be used written like this: newcommand{BriefName}[1]{xdefBriefName{#1}} the LyX application will automatically call the command as BriefName{Argument} if the user types anything or as BriefName{} if the user types nothing. In the latter case, the document breaks as a result of this LyX feature.

– Jalep
Jan 15 at 15:47












2 Answers
2






active

oldest

votes


















1














You can do it with xparse; the trick is not redefining BriefName as you're trying to do, but another control sequence holding the current value (empty at the beginning).



documentclass{article}
usepackage{xparse}

ExplSyntaxOn
NewDocumentCommand{BriefName}{g}
{
IfNoValueF{#1}
{
tl_gset:Nn g_jalep_briefname_tl { #1 }
}
tl_use:N g_jalep_briefname_tl
}
tl_new:N g_jalep_briefname_tl
ExplSyntaxOff

begin{document}

This should be empty:BriefName

This should also be empty: BriefName

This should say Argument: BriefName{Argument}

This should also say Argument: BriefName

end{document}


However, the standard LaTeX syntax uses for optional arguments, so it would be much better to stick with it.



documentclass{article}
usepackage{xparse}

ExplSyntaxOn
NewDocumentCommand{BriefName}{o}
{
IfNoValueF{#1}
{
tl_gset:Nn g_jalep_briefname_tl { #1 }
}
tl_use:N g_jalep_briefname_tl
}
tl_new:N g_jalep_briefname_tl
ExplSyntaxOff

begin{document}

This should be empty:BriefName

This should also be empty: BriefName

This should say Argument: BriefName[Argument]

This should also say Argument: BriefName

end{document}


enter image description here



The version with also admits a solution with classical tools.



documentclass{article}

newcommand{BriefName}[1]{%
ifrelaxdetokenize{#1}relax
else
gdefjalepbriefname{#1}%
fi
jalepbriefname
}
defjalepbriefname{}

begin{document}

This should be empty:BriefName

This should also be empty: BriefName

This should say Argument: BriefName[Argument]

This should also say Argument: BriefName

end{document}





share|improve this answer


























  • Works perfect, thank you! Can I ask how to change the first solution with {} so that optional arg is only printed when called like: BriefName and not when inserting the optional argument in: BriefName{Argument}? Your solution perfectly answered my question asked here but I simplified the need of my final TeX document for this question.

    – Jalep
    Jan 15 at 15:34






  • 1





    @Jalep Sorry, I know nothing about LyX and don't want to. If a tool imposes its will on you, change the tool.

    – egreg
    Jan 15 at 18:55











  • Ok, no worries. Thanks so much for your solution anyway -- it was helpful and I learned a lot.

    – Jalep
    Jan 15 at 19:08





















1














To get what you ask you just need to delete in the declaration



documentclass[parskip]{scrartcl}
usepackage[margin=15mm]{geometry}
usepackage{xifthen}

newcommand{test}[1]{%
ifthenelse{equal{#1}{}}{omitted}{given}%
}

begin{document}

The optional argument was test{}.

The optional argument was test{shubidu}.

end{document}


However the original example appears to have been mis-using the optional argument syntax.



The intended syntax for the "not given" case is test not test



So you do not need to test for an empty argument, latex has already tested for [ being present:



documentclass[parskip]{scrartcl}
usepackage[margin=15mm]{geometry}
usepackage{xifthen}

newcommand{test}[1][default]{#1ldots}

begin{document}

The optional argument was test.

The optional argument was test[shubidu].

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%2f470137%2fhow-to-call-command-with-optional-arg-using-instead-of%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    You can do it with xparse; the trick is not redefining BriefName as you're trying to do, but another control sequence holding the current value (empty at the beginning).



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewDocumentCommand{BriefName}{g}
    {
    IfNoValueF{#1}
    {
    tl_gset:Nn g_jalep_briefname_tl { #1 }
    }
    tl_use:N g_jalep_briefname_tl
    }
    tl_new:N g_jalep_briefname_tl
    ExplSyntaxOff

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName{Argument}

    This should also say Argument: BriefName

    end{document}


    However, the standard LaTeX syntax uses for optional arguments, so it would be much better to stick with it.



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewDocumentCommand{BriefName}{o}
    {
    IfNoValueF{#1}
    {
    tl_gset:Nn g_jalep_briefname_tl { #1 }
    }
    tl_use:N g_jalep_briefname_tl
    }
    tl_new:N g_jalep_briefname_tl
    ExplSyntaxOff

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName[Argument]

    This should also say Argument: BriefName

    end{document}


    enter image description here



    The version with also admits a solution with classical tools.



    documentclass{article}

    newcommand{BriefName}[1]{%
    ifrelaxdetokenize{#1}relax
    else
    gdefjalepbriefname{#1}%
    fi
    jalepbriefname
    }
    defjalepbriefname{}

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName[Argument]

    This should also say Argument: BriefName

    end{document}





    share|improve this answer


























    • Works perfect, thank you! Can I ask how to change the first solution with {} so that optional arg is only printed when called like: BriefName and not when inserting the optional argument in: BriefName{Argument}? Your solution perfectly answered my question asked here but I simplified the need of my final TeX document for this question.

      – Jalep
      Jan 15 at 15:34






    • 1





      @Jalep Sorry, I know nothing about LyX and don't want to. If a tool imposes its will on you, change the tool.

      – egreg
      Jan 15 at 18:55











    • Ok, no worries. Thanks so much for your solution anyway -- it was helpful and I learned a lot.

      – Jalep
      Jan 15 at 19:08


















    1














    You can do it with xparse; the trick is not redefining BriefName as you're trying to do, but another control sequence holding the current value (empty at the beginning).



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewDocumentCommand{BriefName}{g}
    {
    IfNoValueF{#1}
    {
    tl_gset:Nn g_jalep_briefname_tl { #1 }
    }
    tl_use:N g_jalep_briefname_tl
    }
    tl_new:N g_jalep_briefname_tl
    ExplSyntaxOff

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName{Argument}

    This should also say Argument: BriefName

    end{document}


    However, the standard LaTeX syntax uses for optional arguments, so it would be much better to stick with it.



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewDocumentCommand{BriefName}{o}
    {
    IfNoValueF{#1}
    {
    tl_gset:Nn g_jalep_briefname_tl { #1 }
    }
    tl_use:N g_jalep_briefname_tl
    }
    tl_new:N g_jalep_briefname_tl
    ExplSyntaxOff

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName[Argument]

    This should also say Argument: BriefName

    end{document}


    enter image description here



    The version with also admits a solution with classical tools.



    documentclass{article}

    newcommand{BriefName}[1]{%
    ifrelaxdetokenize{#1}relax
    else
    gdefjalepbriefname{#1}%
    fi
    jalepbriefname
    }
    defjalepbriefname{}

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName[Argument]

    This should also say Argument: BriefName

    end{document}





    share|improve this answer


























    • Works perfect, thank you! Can I ask how to change the first solution with {} so that optional arg is only printed when called like: BriefName and not when inserting the optional argument in: BriefName{Argument}? Your solution perfectly answered my question asked here but I simplified the need of my final TeX document for this question.

      – Jalep
      Jan 15 at 15:34






    • 1





      @Jalep Sorry, I know nothing about LyX and don't want to. If a tool imposes its will on you, change the tool.

      – egreg
      Jan 15 at 18:55











    • Ok, no worries. Thanks so much for your solution anyway -- it was helpful and I learned a lot.

      – Jalep
      Jan 15 at 19:08
















    1












    1








    1







    You can do it with xparse; the trick is not redefining BriefName as you're trying to do, but another control sequence holding the current value (empty at the beginning).



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewDocumentCommand{BriefName}{g}
    {
    IfNoValueF{#1}
    {
    tl_gset:Nn g_jalep_briefname_tl { #1 }
    }
    tl_use:N g_jalep_briefname_tl
    }
    tl_new:N g_jalep_briefname_tl
    ExplSyntaxOff

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName{Argument}

    This should also say Argument: BriefName

    end{document}


    However, the standard LaTeX syntax uses for optional arguments, so it would be much better to stick with it.



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewDocumentCommand{BriefName}{o}
    {
    IfNoValueF{#1}
    {
    tl_gset:Nn g_jalep_briefname_tl { #1 }
    }
    tl_use:N g_jalep_briefname_tl
    }
    tl_new:N g_jalep_briefname_tl
    ExplSyntaxOff

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName[Argument]

    This should also say Argument: BriefName

    end{document}


    enter image description here



    The version with also admits a solution with classical tools.



    documentclass{article}

    newcommand{BriefName}[1]{%
    ifrelaxdetokenize{#1}relax
    else
    gdefjalepbriefname{#1}%
    fi
    jalepbriefname
    }
    defjalepbriefname{}

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName[Argument]

    This should also say Argument: BriefName

    end{document}





    share|improve this answer















    You can do it with xparse; the trick is not redefining BriefName as you're trying to do, but another control sequence holding the current value (empty at the beginning).



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewDocumentCommand{BriefName}{g}
    {
    IfNoValueF{#1}
    {
    tl_gset:Nn g_jalep_briefname_tl { #1 }
    }
    tl_use:N g_jalep_briefname_tl
    }
    tl_new:N g_jalep_briefname_tl
    ExplSyntaxOff

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName{Argument}

    This should also say Argument: BriefName

    end{document}


    However, the standard LaTeX syntax uses for optional arguments, so it would be much better to stick with it.



    documentclass{article}
    usepackage{xparse}

    ExplSyntaxOn
    NewDocumentCommand{BriefName}{o}
    {
    IfNoValueF{#1}
    {
    tl_gset:Nn g_jalep_briefname_tl { #1 }
    }
    tl_use:N g_jalep_briefname_tl
    }
    tl_new:N g_jalep_briefname_tl
    ExplSyntaxOff

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName[Argument]

    This should also say Argument: BriefName

    end{document}


    enter image description here



    The version with also admits a solution with classical tools.



    documentclass{article}

    newcommand{BriefName}[1]{%
    ifrelaxdetokenize{#1}relax
    else
    gdefjalepbriefname{#1}%
    fi
    jalepbriefname
    }
    defjalepbriefname{}

    begin{document}

    This should be empty:BriefName

    This should also be empty: BriefName

    This should say Argument: BriefName[Argument]

    This should also say Argument: BriefName

    end{document}






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 14 at 21:46

























    answered Jan 14 at 21:31









    egregegreg

    714k8618973184




    714k8618973184













    • Works perfect, thank you! Can I ask how to change the first solution with {} so that optional arg is only printed when called like: BriefName and not when inserting the optional argument in: BriefName{Argument}? Your solution perfectly answered my question asked here but I simplified the need of my final TeX document for this question.

      – Jalep
      Jan 15 at 15:34






    • 1





      @Jalep Sorry, I know nothing about LyX and don't want to. If a tool imposes its will on you, change the tool.

      – egreg
      Jan 15 at 18:55











    • Ok, no worries. Thanks so much for your solution anyway -- it was helpful and I learned a lot.

      – Jalep
      Jan 15 at 19:08





















    • Works perfect, thank you! Can I ask how to change the first solution with {} so that optional arg is only printed when called like: BriefName and not when inserting the optional argument in: BriefName{Argument}? Your solution perfectly answered my question asked here but I simplified the need of my final TeX document for this question.

      – Jalep
      Jan 15 at 15:34






    • 1





      @Jalep Sorry, I know nothing about LyX and don't want to. If a tool imposes its will on you, change the tool.

      – egreg
      Jan 15 at 18:55











    • Ok, no worries. Thanks so much for your solution anyway -- it was helpful and I learned a lot.

      – Jalep
      Jan 15 at 19:08



















    Works perfect, thank you! Can I ask how to change the first solution with {} so that optional arg is only printed when called like: BriefName and not when inserting the optional argument in: BriefName{Argument}? Your solution perfectly answered my question asked here but I simplified the need of my final TeX document for this question.

    – Jalep
    Jan 15 at 15:34





    Works perfect, thank you! Can I ask how to change the first solution with {} so that optional arg is only printed when called like: BriefName and not when inserting the optional argument in: BriefName{Argument}? Your solution perfectly answered my question asked here but I simplified the need of my final TeX document for this question.

    – Jalep
    Jan 15 at 15:34




    1




    1





    @Jalep Sorry, I know nothing about LyX and don't want to. If a tool imposes its will on you, change the tool.

    – egreg
    Jan 15 at 18:55





    @Jalep Sorry, I know nothing about LyX and don't want to. If a tool imposes its will on you, change the tool.

    – egreg
    Jan 15 at 18:55













    Ok, no worries. Thanks so much for your solution anyway -- it was helpful and I learned a lot.

    – Jalep
    Jan 15 at 19:08







    Ok, no worries. Thanks so much for your solution anyway -- it was helpful and I learned a lot.

    – Jalep
    Jan 15 at 19:08













    1














    To get what you ask you just need to delete in the declaration



    documentclass[parskip]{scrartcl}
    usepackage[margin=15mm]{geometry}
    usepackage{xifthen}

    newcommand{test}[1]{%
    ifthenelse{equal{#1}{}}{omitted}{given}%
    }

    begin{document}

    The optional argument was test{}.

    The optional argument was test{shubidu}.

    end{document}


    However the original example appears to have been mis-using the optional argument syntax.



    The intended syntax for the "not given" case is test not test



    So you do not need to test for an empty argument, latex has already tested for [ being present:



    documentclass[parskip]{scrartcl}
    usepackage[margin=15mm]{geometry}
    usepackage{xifthen}

    newcommand{test}[1][default]{#1ldots}

    begin{document}

    The optional argument was test.

    The optional argument was test[shubidu].

    end{document}





    share|improve this answer




























      1














      To get what you ask you just need to delete in the declaration



      documentclass[parskip]{scrartcl}
      usepackage[margin=15mm]{geometry}
      usepackage{xifthen}

      newcommand{test}[1]{%
      ifthenelse{equal{#1}{}}{omitted}{given}%
      }

      begin{document}

      The optional argument was test{}.

      The optional argument was test{shubidu}.

      end{document}


      However the original example appears to have been mis-using the optional argument syntax.



      The intended syntax for the "not given" case is test not test



      So you do not need to test for an empty argument, latex has already tested for [ being present:



      documentclass[parskip]{scrartcl}
      usepackage[margin=15mm]{geometry}
      usepackage{xifthen}

      newcommand{test}[1][default]{#1ldots}

      begin{document}

      The optional argument was test.

      The optional argument was test[shubidu].

      end{document}





      share|improve this answer


























        1












        1








        1







        To get what you ask you just need to delete in the declaration



        documentclass[parskip]{scrartcl}
        usepackage[margin=15mm]{geometry}
        usepackage{xifthen}

        newcommand{test}[1]{%
        ifthenelse{equal{#1}{}}{omitted}{given}%
        }

        begin{document}

        The optional argument was test{}.

        The optional argument was test{shubidu}.

        end{document}


        However the original example appears to have been mis-using the optional argument syntax.



        The intended syntax for the "not given" case is test not test



        So you do not need to test for an empty argument, latex has already tested for [ being present:



        documentclass[parskip]{scrartcl}
        usepackage[margin=15mm]{geometry}
        usepackage{xifthen}

        newcommand{test}[1][default]{#1ldots}

        begin{document}

        The optional argument was test.

        The optional argument was test[shubidu].

        end{document}





        share|improve this answer













        To get what you ask you just need to delete in the declaration



        documentclass[parskip]{scrartcl}
        usepackage[margin=15mm]{geometry}
        usepackage{xifthen}

        newcommand{test}[1]{%
        ifthenelse{equal{#1}{}}{omitted}{given}%
        }

        begin{document}

        The optional argument was test{}.

        The optional argument was test{shubidu}.

        end{document}


        However the original example appears to have been mis-using the optional argument syntax.



        The intended syntax for the "not given" case is test not test



        So you do not need to test for an empty argument, latex has already tested for [ being present:



        documentclass[parskip]{scrartcl}
        usepackage[margin=15mm]{geometry}
        usepackage{xifthen}

        newcommand{test}[1][default]{#1ldots}

        begin{document}

        The optional argument was test.

        The optional argument was test[shubidu].

        end{document}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 14 at 21:04









        David CarlisleDavid Carlisle

        486k4111231867




        486k4111231867






























            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%2f470137%2fhow-to-call-command-with-optional-arg-using-instead-of%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

            How to send String Array data to Server using php in android

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Is anime1.com a legal site for watching anime?