Including default parameter into def












5















I'm using the classic thesis style with the graffito command (example here). How can I modify the command below to add some vertical spacing before the text which is set to a standard value by default but can be changed by including a parameter?



% Graffiti as in GKP's book "Concrete Mathematics"
% thanks to Lorenzo Pantieri and Enrico Gregorio
defgraffito@setup{%
vspace{0.7em}
% slshapefootnotesize% this was due to slhape in above book
itshapefootnotesizeleavevmodecolor{Black}%
parindent=0pt lineskip=0pt lineskiplimit=0pt %
tolerance=2000 hyphenpenalty=300 exhyphenpenalty=300%
doublehyphendemerits=100000%
finalhyphendemerits=doublehyphendemerits}


My first approach would be to add a vspace{#1} into the beginning, but I'm not aware how to include (i) this parameter #1 into the definition; (ii) include a default value for the parameter. I also didn't managed to translate the code snippet below into a newcommand definition which, as I am aware, supports default parameters.










share|improve this question


















  • 2





    Why not use newcommandgraffito@setup[1][0.7em]{vspace{#1}...} instead (since you use LaTeX syntax anyway)? Then you can call graffito@setup[1em] to use something else than the default.

    – Skillmon
    Jan 15 at 11:49













  • @Skillmon Good idea, but I might have to switch frequently between the default and a custom offset. So rather than re-specifying the offset explicitly, I'd prefer to have a default setup that the command falls back to if no parameter is given.

    – Chris
    Jan 15 at 12:00











  • Also, I do not understand the @setup notation: how come that we define a graffito@setup command which is later used as graffito{text}? I'd appreciate some intuitions here, too.

    – Chris
    Jan 15 at 12:01











  • I need more information, can you create a complete MWE? I don't know what graffito should do and how it is defined, what you want to do with it, and where your optional parameter should be. The easiest way to patch above's definition to include a changeable offset, is to define a length and change the definition to use that length: newlengthmylengthsetlengthmylength{.7em} and then vspace{mylength} instead of vspace{0.7em}. To change it, just change the length. No argument hacking needed.

    – Skillmon
    Jan 15 at 12:26






  • 1





    The % after 300 and 10000 are definitely not mine. ;-)

    – egreg
    Jan 15 at 12:36
















5















I'm using the classic thesis style with the graffito command (example here). How can I modify the command below to add some vertical spacing before the text which is set to a standard value by default but can be changed by including a parameter?



% Graffiti as in GKP's book "Concrete Mathematics"
% thanks to Lorenzo Pantieri and Enrico Gregorio
defgraffito@setup{%
vspace{0.7em}
% slshapefootnotesize% this was due to slhape in above book
itshapefootnotesizeleavevmodecolor{Black}%
parindent=0pt lineskip=0pt lineskiplimit=0pt %
tolerance=2000 hyphenpenalty=300 exhyphenpenalty=300%
doublehyphendemerits=100000%
finalhyphendemerits=doublehyphendemerits}


My first approach would be to add a vspace{#1} into the beginning, but I'm not aware how to include (i) this parameter #1 into the definition; (ii) include a default value for the parameter. I also didn't managed to translate the code snippet below into a newcommand definition which, as I am aware, supports default parameters.










share|improve this question


















  • 2





    Why not use newcommandgraffito@setup[1][0.7em]{vspace{#1}...} instead (since you use LaTeX syntax anyway)? Then you can call graffito@setup[1em] to use something else than the default.

    – Skillmon
    Jan 15 at 11:49













  • @Skillmon Good idea, but I might have to switch frequently between the default and a custom offset. So rather than re-specifying the offset explicitly, I'd prefer to have a default setup that the command falls back to if no parameter is given.

    – Chris
    Jan 15 at 12:00











  • Also, I do not understand the @setup notation: how come that we define a graffito@setup command which is later used as graffito{text}? I'd appreciate some intuitions here, too.

    – Chris
    Jan 15 at 12:01











  • I need more information, can you create a complete MWE? I don't know what graffito should do and how it is defined, what you want to do with it, and where your optional parameter should be. The easiest way to patch above's definition to include a changeable offset, is to define a length and change the definition to use that length: newlengthmylengthsetlengthmylength{.7em} and then vspace{mylength} instead of vspace{0.7em}. To change it, just change the length. No argument hacking needed.

    – Skillmon
    Jan 15 at 12:26






  • 1





    The % after 300 and 10000 are definitely not mine. ;-)

    – egreg
    Jan 15 at 12:36














5












5








5








I'm using the classic thesis style with the graffito command (example here). How can I modify the command below to add some vertical spacing before the text which is set to a standard value by default but can be changed by including a parameter?



% Graffiti as in GKP's book "Concrete Mathematics"
% thanks to Lorenzo Pantieri and Enrico Gregorio
defgraffito@setup{%
vspace{0.7em}
% slshapefootnotesize% this was due to slhape in above book
itshapefootnotesizeleavevmodecolor{Black}%
parindent=0pt lineskip=0pt lineskiplimit=0pt %
tolerance=2000 hyphenpenalty=300 exhyphenpenalty=300%
doublehyphendemerits=100000%
finalhyphendemerits=doublehyphendemerits}


My first approach would be to add a vspace{#1} into the beginning, but I'm not aware how to include (i) this parameter #1 into the definition; (ii) include a default value for the parameter. I also didn't managed to translate the code snippet below into a newcommand definition which, as I am aware, supports default parameters.










share|improve this question














I'm using the classic thesis style with the graffito command (example here). How can I modify the command below to add some vertical spacing before the text which is set to a standard value by default but can be changed by including a parameter?



% Graffiti as in GKP's book "Concrete Mathematics"
% thanks to Lorenzo Pantieri and Enrico Gregorio
defgraffito@setup{%
vspace{0.7em}
% slshapefootnotesize% this was due to slhape in above book
itshapefootnotesizeleavevmodecolor{Black}%
parindent=0pt lineskip=0pt lineskiplimit=0pt %
tolerance=2000 hyphenpenalty=300 exhyphenpenalty=300%
doublehyphendemerits=100000%
finalhyphendemerits=doublehyphendemerits}


My first approach would be to add a vspace{#1} into the beginning, but I'm not aware how to include (i) this parameter #1 into the definition; (ii) include a default value for the parameter. I also didn't managed to translate the code snippet below into a newcommand definition which, as I am aware, supports default parameters.







vertical-alignment parameters definition






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 15 at 11:47









ChrisChris

245210




245210








  • 2





    Why not use newcommandgraffito@setup[1][0.7em]{vspace{#1}...} instead (since you use LaTeX syntax anyway)? Then you can call graffito@setup[1em] to use something else than the default.

    – Skillmon
    Jan 15 at 11:49













  • @Skillmon Good idea, but I might have to switch frequently between the default and a custom offset. So rather than re-specifying the offset explicitly, I'd prefer to have a default setup that the command falls back to if no parameter is given.

    – Chris
    Jan 15 at 12:00











  • Also, I do not understand the @setup notation: how come that we define a graffito@setup command which is later used as graffito{text}? I'd appreciate some intuitions here, too.

    – Chris
    Jan 15 at 12:01











  • I need more information, can you create a complete MWE? I don't know what graffito should do and how it is defined, what you want to do with it, and where your optional parameter should be. The easiest way to patch above's definition to include a changeable offset, is to define a length and change the definition to use that length: newlengthmylengthsetlengthmylength{.7em} and then vspace{mylength} instead of vspace{0.7em}. To change it, just change the length. No argument hacking needed.

    – Skillmon
    Jan 15 at 12:26






  • 1





    The % after 300 and 10000 are definitely not mine. ;-)

    – egreg
    Jan 15 at 12:36














  • 2





    Why not use newcommandgraffito@setup[1][0.7em]{vspace{#1}...} instead (since you use LaTeX syntax anyway)? Then you can call graffito@setup[1em] to use something else than the default.

    – Skillmon
    Jan 15 at 11:49













  • @Skillmon Good idea, but I might have to switch frequently between the default and a custom offset. So rather than re-specifying the offset explicitly, I'd prefer to have a default setup that the command falls back to if no parameter is given.

    – Chris
    Jan 15 at 12:00











  • Also, I do not understand the @setup notation: how come that we define a graffito@setup command which is later used as graffito{text}? I'd appreciate some intuitions here, too.

    – Chris
    Jan 15 at 12:01











  • I need more information, can you create a complete MWE? I don't know what graffito should do and how it is defined, what you want to do with it, and where your optional parameter should be. The easiest way to patch above's definition to include a changeable offset, is to define a length and change the definition to use that length: newlengthmylengthsetlengthmylength{.7em} and then vspace{mylength} instead of vspace{0.7em}. To change it, just change the length. No argument hacking needed.

    – Skillmon
    Jan 15 at 12:26






  • 1





    The % after 300 and 10000 are definitely not mine. ;-)

    – egreg
    Jan 15 at 12:36








2




2





Why not use newcommandgraffito@setup[1][0.7em]{vspace{#1}...} instead (since you use LaTeX syntax anyway)? Then you can call graffito@setup[1em] to use something else than the default.

– Skillmon
Jan 15 at 11:49







Why not use newcommandgraffito@setup[1][0.7em]{vspace{#1}...} instead (since you use LaTeX syntax anyway)? Then you can call graffito@setup[1em] to use something else than the default.

– Skillmon
Jan 15 at 11:49















@Skillmon Good idea, but I might have to switch frequently between the default and a custom offset. So rather than re-specifying the offset explicitly, I'd prefer to have a default setup that the command falls back to if no parameter is given.

– Chris
Jan 15 at 12:00





@Skillmon Good idea, but I might have to switch frequently between the default and a custom offset. So rather than re-specifying the offset explicitly, I'd prefer to have a default setup that the command falls back to if no parameter is given.

– Chris
Jan 15 at 12:00













Also, I do not understand the @setup notation: how come that we define a graffito@setup command which is later used as graffito{text}? I'd appreciate some intuitions here, too.

– Chris
Jan 15 at 12:01





Also, I do not understand the @setup notation: how come that we define a graffito@setup command which is later used as graffito{text}? I'd appreciate some intuitions here, too.

– Chris
Jan 15 at 12:01













I need more information, can you create a complete MWE? I don't know what graffito should do and how it is defined, what you want to do with it, and where your optional parameter should be. The easiest way to patch above's definition to include a changeable offset, is to define a length and change the definition to use that length: newlengthmylengthsetlengthmylength{.7em} and then vspace{mylength} instead of vspace{0.7em}. To change it, just change the length. No argument hacking needed.

– Skillmon
Jan 15 at 12:26





I need more information, can you create a complete MWE? I don't know what graffito should do and how it is defined, what you want to do with it, and where your optional parameter should be. The easiest way to patch above's definition to include a changeable offset, is to define a length and change the definition to use that length: newlengthmylengthsetlengthmylength{.7em} and then vspace{mylength} instead of vspace{0.7em}. To change it, just change the length. No argument hacking needed.

– Skillmon
Jan 15 at 12:26




1




1





The % after 300 and 10000 are definitely not mine. ;-)

– egreg
Jan 15 at 12:36





The % after 300 and 10000 are definitely not mine. ;-)

– egreg
Jan 15 at 12:36










2 Answers
2






active

oldest

votes


















6














Just define graffito with an optional argument:



documentclass{article}
usepackage{lipsum}

makeatletter
newcommand{graffito}[2][0.7em]{%
marginpar
[graffito@setup{#1}raggedlefthspace{0pt}#2]
{graffito@setup{#1}raggedrighthspace{0pt}#2}%
}
newcommandgraffito@setup[1]{%
vspace{#1}%
parindent=0pt lineskip=0pt lineskiplimit=0pt
tolerance=2000 hyphenpenalty=300 exhyphenpenalty=300
doublehyphendemerits=100000
finalhyphendemerits=doublehyphendemerits
itshapefootnotesize
leavevmodecolor{Black}%
}
makeatother

begin{document}

This has a graffitograffito{This is a standard graffito}
lipsum[1][1-5]

This has a graffitograffito[-0.7em]{This is a moved up graffito}
lipsum[1][1-5]

This has a graffitograffito[1cm]{This is a moved down graffito}
lipsum[1][1-5]

end{document}


enter image description here



Side note: the original code has % after 300 and 100000: they are wrong.






share|improve this answer
























  • Great answer and mwe example appreciated for others who are not familiar with the command / style.

    – Chris
    Jan 15 at 14:17











  • Btw this only works for me (TexLive2018 on OSX) if I change 'Black' to 'black' and add usepackage{xcolor} before.

    – Chris
    Jan 15 at 16:40






  • 1





    @Chris That might depend on the release of LaTeX you're using. Use black if that works for you.

    – egreg
    Jan 15 at 18:54



















4














Since you're using LaTeX syntax anyway, you can use the LaTeX command newcommand to set a default argument with: newcommandgraffito@setup[1][0.7em]{vspace{#1}...}. If you want to not use LaTeX syntax for educational purposes, you can do the following:



We need a first macro which checks whether the optional argument follows. For this we use futurelet (definition made protected because we need an assignment here):



protecteddefmycmd{futureletnextmycmd@a}


We need to check whether the next token is a bracket, if it is the next macro reads its argument, else we give it its default.



defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}


Here I use the macros afterelsefi and afterfi for some logic branching. They eat their argument and put it after fi.



longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}


Finally the last macro needed for this, this one is the one generating the output:



longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}


Complete MWE:



documentclass{article}

makeatletter
longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}
protecteddefmycmd{futureletnextmycmd@a}
defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}
longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}
makeatother

begin{document}
mycmd

mycmd[1em]
end{document}





share|improve this answer
























  • Great elaborate response @skillmon, thanks very much. Would love to accept both answers but since this isn't possible, I go for the quickest one.

    – Chris
    Jan 15 at 14:16











  • @Chris quicker was mine, but egregs is the one solving your problem, so accepting his is fine. If an answer helps you and you want to say thank you, you might consider upvoting it, if you deem it worth it.

    – Skillmon
    Jan 15 at 14:20











  • Done that, thanks!

    – Chris
    Jan 15 at 14:40











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%2f470220%2fincluding-default-parameter-into-def%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









6














Just define graffito with an optional argument:



documentclass{article}
usepackage{lipsum}

makeatletter
newcommand{graffito}[2][0.7em]{%
marginpar
[graffito@setup{#1}raggedlefthspace{0pt}#2]
{graffito@setup{#1}raggedrighthspace{0pt}#2}%
}
newcommandgraffito@setup[1]{%
vspace{#1}%
parindent=0pt lineskip=0pt lineskiplimit=0pt
tolerance=2000 hyphenpenalty=300 exhyphenpenalty=300
doublehyphendemerits=100000
finalhyphendemerits=doublehyphendemerits
itshapefootnotesize
leavevmodecolor{Black}%
}
makeatother

begin{document}

This has a graffitograffito{This is a standard graffito}
lipsum[1][1-5]

This has a graffitograffito[-0.7em]{This is a moved up graffito}
lipsum[1][1-5]

This has a graffitograffito[1cm]{This is a moved down graffito}
lipsum[1][1-5]

end{document}


enter image description here



Side note: the original code has % after 300 and 100000: they are wrong.






share|improve this answer
























  • Great answer and mwe example appreciated for others who are not familiar with the command / style.

    – Chris
    Jan 15 at 14:17











  • Btw this only works for me (TexLive2018 on OSX) if I change 'Black' to 'black' and add usepackage{xcolor} before.

    – Chris
    Jan 15 at 16:40






  • 1





    @Chris That might depend on the release of LaTeX you're using. Use black if that works for you.

    – egreg
    Jan 15 at 18:54
















6














Just define graffito with an optional argument:



documentclass{article}
usepackage{lipsum}

makeatletter
newcommand{graffito}[2][0.7em]{%
marginpar
[graffito@setup{#1}raggedlefthspace{0pt}#2]
{graffito@setup{#1}raggedrighthspace{0pt}#2}%
}
newcommandgraffito@setup[1]{%
vspace{#1}%
parindent=0pt lineskip=0pt lineskiplimit=0pt
tolerance=2000 hyphenpenalty=300 exhyphenpenalty=300
doublehyphendemerits=100000
finalhyphendemerits=doublehyphendemerits
itshapefootnotesize
leavevmodecolor{Black}%
}
makeatother

begin{document}

This has a graffitograffito{This is a standard graffito}
lipsum[1][1-5]

This has a graffitograffito[-0.7em]{This is a moved up graffito}
lipsum[1][1-5]

This has a graffitograffito[1cm]{This is a moved down graffito}
lipsum[1][1-5]

end{document}


enter image description here



Side note: the original code has % after 300 and 100000: they are wrong.






share|improve this answer
























  • Great answer and mwe example appreciated for others who are not familiar with the command / style.

    – Chris
    Jan 15 at 14:17











  • Btw this only works for me (TexLive2018 on OSX) if I change 'Black' to 'black' and add usepackage{xcolor} before.

    – Chris
    Jan 15 at 16:40






  • 1





    @Chris That might depend on the release of LaTeX you're using. Use black if that works for you.

    – egreg
    Jan 15 at 18:54














6












6








6







Just define graffito with an optional argument:



documentclass{article}
usepackage{lipsum}

makeatletter
newcommand{graffito}[2][0.7em]{%
marginpar
[graffito@setup{#1}raggedlefthspace{0pt}#2]
{graffito@setup{#1}raggedrighthspace{0pt}#2}%
}
newcommandgraffito@setup[1]{%
vspace{#1}%
parindent=0pt lineskip=0pt lineskiplimit=0pt
tolerance=2000 hyphenpenalty=300 exhyphenpenalty=300
doublehyphendemerits=100000
finalhyphendemerits=doublehyphendemerits
itshapefootnotesize
leavevmodecolor{Black}%
}
makeatother

begin{document}

This has a graffitograffito{This is a standard graffito}
lipsum[1][1-5]

This has a graffitograffito[-0.7em]{This is a moved up graffito}
lipsum[1][1-5]

This has a graffitograffito[1cm]{This is a moved down graffito}
lipsum[1][1-5]

end{document}


enter image description here



Side note: the original code has % after 300 and 100000: they are wrong.






share|improve this answer













Just define graffito with an optional argument:



documentclass{article}
usepackage{lipsum}

makeatletter
newcommand{graffito}[2][0.7em]{%
marginpar
[graffito@setup{#1}raggedlefthspace{0pt}#2]
{graffito@setup{#1}raggedrighthspace{0pt}#2}%
}
newcommandgraffito@setup[1]{%
vspace{#1}%
parindent=0pt lineskip=0pt lineskiplimit=0pt
tolerance=2000 hyphenpenalty=300 exhyphenpenalty=300
doublehyphendemerits=100000
finalhyphendemerits=doublehyphendemerits
itshapefootnotesize
leavevmodecolor{Black}%
}
makeatother

begin{document}

This has a graffitograffito{This is a standard graffito}
lipsum[1][1-5]

This has a graffitograffito[-0.7em]{This is a moved up graffito}
lipsum[1][1-5]

This has a graffitograffito[1cm]{This is a moved down graffito}
lipsum[1][1-5]

end{document}


enter image description here



Side note: the original code has % after 300 and 100000: they are wrong.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 15 at 12:45









egregegreg

713k8618943182




713k8618943182













  • Great answer and mwe example appreciated for others who are not familiar with the command / style.

    – Chris
    Jan 15 at 14:17











  • Btw this only works for me (TexLive2018 on OSX) if I change 'Black' to 'black' and add usepackage{xcolor} before.

    – Chris
    Jan 15 at 16:40






  • 1





    @Chris That might depend on the release of LaTeX you're using. Use black if that works for you.

    – egreg
    Jan 15 at 18:54



















  • Great answer and mwe example appreciated for others who are not familiar with the command / style.

    – Chris
    Jan 15 at 14:17











  • Btw this only works for me (TexLive2018 on OSX) if I change 'Black' to 'black' and add usepackage{xcolor} before.

    – Chris
    Jan 15 at 16:40






  • 1





    @Chris That might depend on the release of LaTeX you're using. Use black if that works for you.

    – egreg
    Jan 15 at 18:54

















Great answer and mwe example appreciated for others who are not familiar with the command / style.

– Chris
Jan 15 at 14:17





Great answer and mwe example appreciated for others who are not familiar with the command / style.

– Chris
Jan 15 at 14:17













Btw this only works for me (TexLive2018 on OSX) if I change 'Black' to 'black' and add usepackage{xcolor} before.

– Chris
Jan 15 at 16:40





Btw this only works for me (TexLive2018 on OSX) if I change 'Black' to 'black' and add usepackage{xcolor} before.

– Chris
Jan 15 at 16:40




1




1





@Chris That might depend on the release of LaTeX you're using. Use black if that works for you.

– egreg
Jan 15 at 18:54





@Chris That might depend on the release of LaTeX you're using. Use black if that works for you.

– egreg
Jan 15 at 18:54











4














Since you're using LaTeX syntax anyway, you can use the LaTeX command newcommand to set a default argument with: newcommandgraffito@setup[1][0.7em]{vspace{#1}...}. If you want to not use LaTeX syntax for educational purposes, you can do the following:



We need a first macro which checks whether the optional argument follows. For this we use futurelet (definition made protected because we need an assignment here):



protecteddefmycmd{futureletnextmycmd@a}


We need to check whether the next token is a bracket, if it is the next macro reads its argument, else we give it its default.



defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}


Here I use the macros afterelsefi and afterfi for some logic branching. They eat their argument and put it after fi.



longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}


Finally the last macro needed for this, this one is the one generating the output:



longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}


Complete MWE:



documentclass{article}

makeatletter
longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}
protecteddefmycmd{futureletnextmycmd@a}
defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}
longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}
makeatother

begin{document}
mycmd

mycmd[1em]
end{document}





share|improve this answer
























  • Great elaborate response @skillmon, thanks very much. Would love to accept both answers but since this isn't possible, I go for the quickest one.

    – Chris
    Jan 15 at 14:16











  • @Chris quicker was mine, but egregs is the one solving your problem, so accepting his is fine. If an answer helps you and you want to say thank you, you might consider upvoting it, if you deem it worth it.

    – Skillmon
    Jan 15 at 14:20











  • Done that, thanks!

    – Chris
    Jan 15 at 14:40
















4














Since you're using LaTeX syntax anyway, you can use the LaTeX command newcommand to set a default argument with: newcommandgraffito@setup[1][0.7em]{vspace{#1}...}. If you want to not use LaTeX syntax for educational purposes, you can do the following:



We need a first macro which checks whether the optional argument follows. For this we use futurelet (definition made protected because we need an assignment here):



protecteddefmycmd{futureletnextmycmd@a}


We need to check whether the next token is a bracket, if it is the next macro reads its argument, else we give it its default.



defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}


Here I use the macros afterelsefi and afterfi for some logic branching. They eat their argument and put it after fi.



longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}


Finally the last macro needed for this, this one is the one generating the output:



longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}


Complete MWE:



documentclass{article}

makeatletter
longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}
protecteddefmycmd{futureletnextmycmd@a}
defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}
longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}
makeatother

begin{document}
mycmd

mycmd[1em]
end{document}





share|improve this answer
























  • Great elaborate response @skillmon, thanks very much. Would love to accept both answers but since this isn't possible, I go for the quickest one.

    – Chris
    Jan 15 at 14:16











  • @Chris quicker was mine, but egregs is the one solving your problem, so accepting his is fine. If an answer helps you and you want to say thank you, you might consider upvoting it, if you deem it worth it.

    – Skillmon
    Jan 15 at 14:20











  • Done that, thanks!

    – Chris
    Jan 15 at 14:40














4












4








4







Since you're using LaTeX syntax anyway, you can use the LaTeX command newcommand to set a default argument with: newcommandgraffito@setup[1][0.7em]{vspace{#1}...}. If you want to not use LaTeX syntax for educational purposes, you can do the following:



We need a first macro which checks whether the optional argument follows. For this we use futurelet (definition made protected because we need an assignment here):



protecteddefmycmd{futureletnextmycmd@a}


We need to check whether the next token is a bracket, if it is the next macro reads its argument, else we give it its default.



defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}


Here I use the macros afterelsefi and afterfi for some logic branching. They eat their argument and put it after fi.



longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}


Finally the last macro needed for this, this one is the one generating the output:



longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}


Complete MWE:



documentclass{article}

makeatletter
longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}
protecteddefmycmd{futureletnextmycmd@a}
defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}
longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}
makeatother

begin{document}
mycmd

mycmd[1em]
end{document}





share|improve this answer













Since you're using LaTeX syntax anyway, you can use the LaTeX command newcommand to set a default argument with: newcommandgraffito@setup[1][0.7em]{vspace{#1}...}. If you want to not use LaTeX syntax for educational purposes, you can do the following:



We need a first macro which checks whether the optional argument follows. For this we use futurelet (definition made protected because we need an assignment here):



protecteddefmycmd{futureletnextmycmd@a}


We need to check whether the next token is a bracket, if it is the next macro reads its argument, else we give it its default.



defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}


Here I use the macros afterelsefi and afterfi for some logic branching. They eat their argument and put it after fi.



longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}


Finally the last macro needed for this, this one is the one generating the output:



longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}


Complete MWE:



documentclass{article}

makeatletter
longdefafterelsefi#1else#2fi{fi#1}
longdefafterfi#1fi{fi#1}
protecteddefmycmd{futureletnextmycmd@a}
defmycmd@a
{%
ifx[next
afterelsefi{mycmd@b}%
else
afterfi{mycmd@b[0.7em]}%
fi
}
longdefmycmd@b[#1]%
{%
Argument was: texttt{detokenize{#1}}%
}
makeatother

begin{document}
mycmd

mycmd[1em]
end{document}






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 15 at 12:21









SkillmonSkillmon

21.5k11941




21.5k11941













  • Great elaborate response @skillmon, thanks very much. Would love to accept both answers but since this isn't possible, I go for the quickest one.

    – Chris
    Jan 15 at 14:16











  • @Chris quicker was mine, but egregs is the one solving your problem, so accepting his is fine. If an answer helps you and you want to say thank you, you might consider upvoting it, if you deem it worth it.

    – Skillmon
    Jan 15 at 14:20











  • Done that, thanks!

    – Chris
    Jan 15 at 14:40



















  • Great elaborate response @skillmon, thanks very much. Would love to accept both answers but since this isn't possible, I go for the quickest one.

    – Chris
    Jan 15 at 14:16











  • @Chris quicker was mine, but egregs is the one solving your problem, so accepting his is fine. If an answer helps you and you want to say thank you, you might consider upvoting it, if you deem it worth it.

    – Skillmon
    Jan 15 at 14:20











  • Done that, thanks!

    – Chris
    Jan 15 at 14:40

















Great elaborate response @skillmon, thanks very much. Would love to accept both answers but since this isn't possible, I go for the quickest one.

– Chris
Jan 15 at 14:16





Great elaborate response @skillmon, thanks very much. Would love to accept both answers but since this isn't possible, I go for the quickest one.

– Chris
Jan 15 at 14:16













@Chris quicker was mine, but egregs is the one solving your problem, so accepting his is fine. If an answer helps you and you want to say thank you, you might consider upvoting it, if you deem it worth it.

– Skillmon
Jan 15 at 14:20





@Chris quicker was mine, but egregs is the one solving your problem, so accepting his is fine. If an answer helps you and you want to say thank you, you might consider upvoting it, if you deem it worth it.

– Skillmon
Jan 15 at 14:20













Done that, thanks!

– Chris
Jan 15 at 14:40





Done that, thanks!

– Chris
Jan 15 at 14:40


















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%2f470220%2fincluding-default-parameter-into-def%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

An IMO inspired problem

Management

Investment