To be or not to be - Optional arguments inside definition of macroFancyheaders: how to turn off the page...

How do I narratively explain how in-game circumstances do not mechanically allow a PC to instantly kill an NPC?

Is it really OK to use "because of"?

Rigorous justification for non-relativistic QM perturbation theory assumptions?

The No-Straight Maze

Democratic Socialism vs Social Democracy

Why do neural networks need so many examples to perform?

Why does 0.-5 evaluate to -5?

Does the US government have any planning in place to ensure there's no shortages of food, fuel, steel and other commodities?

Coombinatorics- The number of ways of choosing with parameters

Crack the bank account's password!

Besides PR credit, does diversity provide anything that meritocracy does not?

Is it possible to rotate the Isolines on a Surface Using `MeshFunction`?

What would be some possible ways of escaping higher gravity planets?

Where does documentation like business and software requirement spec docs fit in an agile project?

How do I add a strong "onion flavor" to the biryani (in restaurant style)?

Plausible reason for gold-digging ant

How to change a .eps figure to standalone class?

Case protection with emphasis in biblatex

Reading Mishnayos without understanding

Possible issue with my W4 and tax return

Is the fingering of thirds flexible or do I have to follow the rules?

What is a good reason for every spaceship to carry gun on board?

Context html export bibliography

Renting a 2CV in France



To be or not to be - Optional arguments inside definition of macro


Fancyheaders: how to turn off the page number on the first page?What is the proper way of moving my content but not my header/footer?How to insert footer in first pages of chapter?Fancyhdr adding multiline header causes footer margin to be inconsistent?How to get a separate header only for the first page?Header on odd pagesWhy does if with fancypagestyle only work inside header and footer declarations?How to customize footer/header in each pages?Removing the footer on the first pageDistance rectangle - text chapter using fancy header













1















The version A part in the following example is to define header/footer by macros from package fancyhdr.



Though this version is workable, it's a awkward achievement because the same part of code(ifnum...) is written twice.



Except the optional argument #2, all other code is exactly the same. To improve it, I tried the version B below, but it does not work at all.



It will be awfull especially when the same part of code is large.



So, to sumarize, is there a better way of handling optional arguments (to be there or not) to avoid heavy duplicate code.



documentclass{article}
usepackage{geometry,fancyhdr,xparse}
geometry{showframe}

begin{document}
first pageparvspace{80em} second page
pagestyle{fancy}
fancyhf{}

%Version A: workable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NewDocumentCommand{firstpage}{momm}{%
IfNoValueTF{#2}
{#1{%
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}
}
{#1[#2]% the only diferrence between the two branches of IfNoValueTF is, whether introducing the optional argument #2
{%
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}
}
}%firstpage

% Test
firstpage{fancyhead}[c]{only at the first page}{at rest pages}
firstpage{rhead}{r first}{r rest}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Version B: not workable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NewDocumentCommand{firstpageA}{momm}{%
#1 IfNoValueTF{#2}{}{[#2]}
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}%firstpageA

% Test
firstpageA{fancyhead}[c]{only at the first page}{at rest pages}
firstpageA{rhead}{r first}{r rest}

end{document}


enter image description here










share|improve this question

























  • I would suggest to use O{} instead of o and then just unconditionally put #1[#2] with empty #2 in case of no optional arg.

    – Sergei Golovan
    4 hours ago








  • 2





    You don't need such a complicated setup for the page style of the first page. Simply put thispagestyle{firstpage} at the begin

    – Ulrike Fischer
    4 hours ago






  • 2





    But to avoid large code repetition put the code in a auxiliary command and then use this in tje switches.

    – Ulrike Fischer
    4 hours ago











  • @ Sergei As the document of fancyhdr does not mention it I don't know macros like chead, rfoot can have a optioanal argument. In this example, an optioanal arg with a value of empty does work. By this example, what I want is to find a universal approch to handle those macro that does not permit empty optional argument.

    – lyl
    4 hours ago











  • @Ulrike Yes, an auxiliry command is a good idea. But for some reason(expand sequence, detokenize, tokenize...etc), I wonder if there are much better methods. And I think this is a category question which is of universal significance.

    – lyl
    4 hours ago


















1















The version A part in the following example is to define header/footer by macros from package fancyhdr.



Though this version is workable, it's a awkward achievement because the same part of code(ifnum...) is written twice.



Except the optional argument #2, all other code is exactly the same. To improve it, I tried the version B below, but it does not work at all.



It will be awfull especially when the same part of code is large.



So, to sumarize, is there a better way of handling optional arguments (to be there or not) to avoid heavy duplicate code.



documentclass{article}
usepackage{geometry,fancyhdr,xparse}
geometry{showframe}

begin{document}
first pageparvspace{80em} second page
pagestyle{fancy}
fancyhf{}

%Version A: workable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NewDocumentCommand{firstpage}{momm}{%
IfNoValueTF{#2}
{#1{%
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}
}
{#1[#2]% the only diferrence between the two branches of IfNoValueTF is, whether introducing the optional argument #2
{%
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}
}
}%firstpage

% Test
firstpage{fancyhead}[c]{only at the first page}{at rest pages}
firstpage{rhead}{r first}{r rest}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Version B: not workable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NewDocumentCommand{firstpageA}{momm}{%
#1 IfNoValueTF{#2}{}{[#2]}
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}%firstpageA

% Test
firstpageA{fancyhead}[c]{only at the first page}{at rest pages}
firstpageA{rhead}{r first}{r rest}

end{document}


enter image description here










share|improve this question

























  • I would suggest to use O{} instead of o and then just unconditionally put #1[#2] with empty #2 in case of no optional arg.

    – Sergei Golovan
    4 hours ago








  • 2





    You don't need such a complicated setup for the page style of the first page. Simply put thispagestyle{firstpage} at the begin

    – Ulrike Fischer
    4 hours ago






  • 2





    But to avoid large code repetition put the code in a auxiliary command and then use this in tje switches.

    – Ulrike Fischer
    4 hours ago











  • @ Sergei As the document of fancyhdr does not mention it I don't know macros like chead, rfoot can have a optioanal argument. In this example, an optioanal arg with a value of empty does work. By this example, what I want is to find a universal approch to handle those macro that does not permit empty optional argument.

    – lyl
    4 hours ago











  • @Ulrike Yes, an auxiliry command is a good idea. But for some reason(expand sequence, detokenize, tokenize...etc), I wonder if there are much better methods. And I think this is a category question which is of universal significance.

    – lyl
    4 hours ago
















1












1








1








The version A part in the following example is to define header/footer by macros from package fancyhdr.



Though this version is workable, it's a awkward achievement because the same part of code(ifnum...) is written twice.



Except the optional argument #2, all other code is exactly the same. To improve it, I tried the version B below, but it does not work at all.



It will be awfull especially when the same part of code is large.



So, to sumarize, is there a better way of handling optional arguments (to be there or not) to avoid heavy duplicate code.



documentclass{article}
usepackage{geometry,fancyhdr,xparse}
geometry{showframe}

begin{document}
first pageparvspace{80em} second page
pagestyle{fancy}
fancyhf{}

%Version A: workable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NewDocumentCommand{firstpage}{momm}{%
IfNoValueTF{#2}
{#1{%
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}
}
{#1[#2]% the only diferrence between the two branches of IfNoValueTF is, whether introducing the optional argument #2
{%
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}
}
}%firstpage

% Test
firstpage{fancyhead}[c]{only at the first page}{at rest pages}
firstpage{rhead}{r first}{r rest}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Version B: not workable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NewDocumentCommand{firstpageA}{momm}{%
#1 IfNoValueTF{#2}{}{[#2]}
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}%firstpageA

% Test
firstpageA{fancyhead}[c]{only at the first page}{at rest pages}
firstpageA{rhead}{r first}{r rest}

end{document}


enter image description here










share|improve this question
















The version A part in the following example is to define header/footer by macros from package fancyhdr.



Though this version is workable, it's a awkward achievement because the same part of code(ifnum...) is written twice.



Except the optional argument #2, all other code is exactly the same. To improve it, I tried the version B below, but it does not work at all.



It will be awfull especially when the same part of code is large.



So, to sumarize, is there a better way of handling optional arguments (to be there or not) to avoid heavy duplicate code.



documentclass{article}
usepackage{geometry,fancyhdr,xparse}
geometry{showframe}

begin{document}
first pageparvspace{80em} second page
pagestyle{fancy}
fancyhf{}

%Version A: workable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NewDocumentCommand{firstpage}{momm}{%
IfNoValueTF{#2}
{#1{%
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}
}
{#1[#2]% the only diferrence between the two branches of IfNoValueTF is, whether introducing the optional argument #2
{%
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}
}
}%firstpage

% Test
firstpage{fancyhead}[c]{only at the first page}{at rest pages}
firstpage{rhead}{r first}{r rest}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Version B: not workable %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NewDocumentCommand{firstpageA}{momm}{%
#1 IfNoValueTF{#2}{}{[#2]}
ifnumvalue{page}=1
#3% content of header/footer only at the first page
else
#4% content of header/footer at the rest pages
fi
}%firstpageA

% Test
firstpageA{fancyhead}[c]{only at the first page}{at rest pages}
firstpageA{rhead}{r first}{r rest}

end{document}


enter image description here







fancyhdr xparse






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 1 hour ago









AndréC

9,28111447




9,28111447










asked 5 hours ago









lyllyl

64128




64128













  • I would suggest to use O{} instead of o and then just unconditionally put #1[#2] with empty #2 in case of no optional arg.

    – Sergei Golovan
    4 hours ago








  • 2





    You don't need such a complicated setup for the page style of the first page. Simply put thispagestyle{firstpage} at the begin

    – Ulrike Fischer
    4 hours ago






  • 2





    But to avoid large code repetition put the code in a auxiliary command and then use this in tje switches.

    – Ulrike Fischer
    4 hours ago











  • @ Sergei As the document of fancyhdr does not mention it I don't know macros like chead, rfoot can have a optioanal argument. In this example, an optioanal arg with a value of empty does work. By this example, what I want is to find a universal approch to handle those macro that does not permit empty optional argument.

    – lyl
    4 hours ago











  • @Ulrike Yes, an auxiliry command is a good idea. But for some reason(expand sequence, detokenize, tokenize...etc), I wonder if there are much better methods. And I think this is a category question which is of universal significance.

    – lyl
    4 hours ago





















  • I would suggest to use O{} instead of o and then just unconditionally put #1[#2] with empty #2 in case of no optional arg.

    – Sergei Golovan
    4 hours ago








  • 2





    You don't need such a complicated setup for the page style of the first page. Simply put thispagestyle{firstpage} at the begin

    – Ulrike Fischer
    4 hours ago






  • 2





    But to avoid large code repetition put the code in a auxiliary command and then use this in tje switches.

    – Ulrike Fischer
    4 hours ago











  • @ Sergei As the document of fancyhdr does not mention it I don't know macros like chead, rfoot can have a optioanal argument. In this example, an optioanal arg with a value of empty does work. By this example, what I want is to find a universal approch to handle those macro that does not permit empty optional argument.

    – lyl
    4 hours ago











  • @Ulrike Yes, an auxiliry command is a good idea. But for some reason(expand sequence, detokenize, tokenize...etc), I wonder if there are much better methods. And I think this is a category question which is of universal significance.

    – lyl
    4 hours ago



















I would suggest to use O{} instead of o and then just unconditionally put #1[#2] with empty #2 in case of no optional arg.

– Sergei Golovan
4 hours ago







I would suggest to use O{} instead of o and then just unconditionally put #1[#2] with empty #2 in case of no optional arg.

– Sergei Golovan
4 hours ago






2




2





You don't need such a complicated setup for the page style of the first page. Simply put thispagestyle{firstpage} at the begin

– Ulrike Fischer
4 hours ago





You don't need such a complicated setup for the page style of the first page. Simply put thispagestyle{firstpage} at the begin

– Ulrike Fischer
4 hours ago




2




2





But to avoid large code repetition put the code in a auxiliary command and then use this in tje switches.

– Ulrike Fischer
4 hours ago





But to avoid large code repetition put the code in a auxiliary command and then use this in tje switches.

– Ulrike Fischer
4 hours ago













@ Sergei As the document of fancyhdr does not mention it I don't know macros like chead, rfoot can have a optioanal argument. In this example, an optioanal arg with a value of empty does work. By this example, what I want is to find a universal approch to handle those macro that does not permit empty optional argument.

– lyl
4 hours ago





@ Sergei As the document of fancyhdr does not mention it I don't know macros like chead, rfoot can have a optioanal argument. In this example, an optioanal arg with a value of empty does work. By this example, what I want is to find a universal approch to handle those macro that does not permit empty optional argument.

– lyl
4 hours ago













@Ulrike Yes, an auxiliry command is a good idea. But for some reason(expand sequence, detokenize, tokenize...etc), I wonder if there are much better methods. And I think this is a category question which is of universal significance.

– lyl
4 hours ago







@Ulrike Yes, an auxiliry command is a good idea. But for some reason(expand sequence, detokenize, tokenize...etc), I wonder if there are much better methods. And I think this is a category question which is of universal significance.

– lyl
4 hours ago












1 Answer
1






active

oldest

votes


















4














I believe it's not useful to try and fit an awkward syntax into a command like that.



It's more rewarding to spend a bit more time in devising a friendlier syntax. Here I propose



sethf{
head = <contents of all headers, except possibly the first>,
foot = <contents of all footers, except possibly the first>,
pos = <l|c|r>,
first = <possible exception for first header/footer>,
}


Only one among head or foot should be used in a call of sethf.



documentclass{article}
usepackage{fancyhdr,xparse}

usepackage[paper=a6paper]{geometry} % smaller pictures
usepackage{lipsum} % mock text

pagestyle{fancy}
fancyhf{}

ExplSyntaxOn
NewDocumentCommand{sethf}{m}
{
tl_clear:N l__lyl_hf_pos_tl
tl_clear:N l__lyl_hf_first_tl
tl_clear:N l__lyl_hf_headfoot_tl
keys_set:nn { lyl/hf } { #1 }
lyl_hf_set:
}

keys_define:nn { lyl/hf }
{
head .code:n = __lyl_hf_aux:Nn fancyhead { #1 },
foot .code:n = __lyl_hf_aux:Nn fancyfoot { #1 },
pos .tl_set:N = l__lyl_hf_pos_tl,
first .tl_set:N = l__lyl_hf_first_tl,
}

tl_new:N l__lyl_hf_headfoot_tl

cs_new_protected:Nn __lyl_hf_aux:Nn
{
cs_set_eq:NN __lyl_hf_temp:w #1
tl_set:Nn l__lyl_hf_headfoot_tl { #2 }
}

cs_new_protected:Nn lyl_hf_set:
{
tl_if_empty:NT l__lyl_hf_first_tl
{
tl_set_eq:NN l__lyl_hf_first_tl l__lyl_hf_headfoot_tl
}
__lyl_hf_set:NVVV
__lyl_hf_temp:w % fancyhead or fancyfoot
l__lyl_hf_pos_tl % position
l__lyl_hf_first_tl % first page
l__lyl_hf_headfoot_tl % other pages
}
cs_new_protected:Nn __lyl_hf_set:Nnnn
{
#1 [ #2 ] { int_compare:nTF { value{page} = 1 } { #3 } { #4 } }
}
cs_generate_variant:Nn __lyl_hf_set:Nnnn { NVVV }
ExplSyntaxOff

sethf{
head = other pages,
first = first page,
pos = c,
}

sethf{pos = r, foot=thepage}

begin{document}

lipsum[1-2]

end{document}


enter image description here



To answer your question: use a macro for the duplicate code.



documentclass{article}
usepackage{fancyhdr,xparse}

usepackage[paper=a6paper]{geometry} % smaller pictures
usepackage{lipsum} % mock text

pagestyle{fancy}
fancyhf{}

NewDocumentCommand{firstpage}{momm}{%
IfNoValueTF{#2}
{setfirstpage{#1}{#3}{#4}}
{setfirstpage{#1[#2]}{#3}{#4}}%
}
NewDocumentCommand{setfirstpage}{mmm}
{
#1{ifnumvalue{page}=1 #2else #3fi}
}

firstpage{fancyhead}[c]{first page}{other pages}
% can also be firstpage{chead}{first page}{other pages}

begin{document}

lipsum[1-2]

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%2f476544%2fto-be-or-not-to-be-optional-arguments-inside-definition-of-macro%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









    4














    I believe it's not useful to try and fit an awkward syntax into a command like that.



    It's more rewarding to spend a bit more time in devising a friendlier syntax. Here I propose



    sethf{
    head = <contents of all headers, except possibly the first>,
    foot = <contents of all footers, except possibly the first>,
    pos = <l|c|r>,
    first = <possible exception for first header/footer>,
    }


    Only one among head or foot should be used in a call of sethf.



    documentclass{article}
    usepackage{fancyhdr,xparse}

    usepackage[paper=a6paper]{geometry} % smaller pictures
    usepackage{lipsum} % mock text

    pagestyle{fancy}
    fancyhf{}

    ExplSyntaxOn
    NewDocumentCommand{sethf}{m}
    {
    tl_clear:N l__lyl_hf_pos_tl
    tl_clear:N l__lyl_hf_first_tl
    tl_clear:N l__lyl_hf_headfoot_tl
    keys_set:nn { lyl/hf } { #1 }
    lyl_hf_set:
    }

    keys_define:nn { lyl/hf }
    {
    head .code:n = __lyl_hf_aux:Nn fancyhead { #1 },
    foot .code:n = __lyl_hf_aux:Nn fancyfoot { #1 },
    pos .tl_set:N = l__lyl_hf_pos_tl,
    first .tl_set:N = l__lyl_hf_first_tl,
    }

    tl_new:N l__lyl_hf_headfoot_tl

    cs_new_protected:Nn __lyl_hf_aux:Nn
    {
    cs_set_eq:NN __lyl_hf_temp:w #1
    tl_set:Nn l__lyl_hf_headfoot_tl { #2 }
    }

    cs_new_protected:Nn lyl_hf_set:
    {
    tl_if_empty:NT l__lyl_hf_first_tl
    {
    tl_set_eq:NN l__lyl_hf_first_tl l__lyl_hf_headfoot_tl
    }
    __lyl_hf_set:NVVV
    __lyl_hf_temp:w % fancyhead or fancyfoot
    l__lyl_hf_pos_tl % position
    l__lyl_hf_first_tl % first page
    l__lyl_hf_headfoot_tl % other pages
    }
    cs_new_protected:Nn __lyl_hf_set:Nnnn
    {
    #1 [ #2 ] { int_compare:nTF { value{page} = 1 } { #3 } { #4 } }
    }
    cs_generate_variant:Nn __lyl_hf_set:Nnnn { NVVV }
    ExplSyntaxOff

    sethf{
    head = other pages,
    first = first page,
    pos = c,
    }

    sethf{pos = r, foot=thepage}

    begin{document}

    lipsum[1-2]

    end{document}


    enter image description here



    To answer your question: use a macro for the duplicate code.



    documentclass{article}
    usepackage{fancyhdr,xparse}

    usepackage[paper=a6paper]{geometry} % smaller pictures
    usepackage{lipsum} % mock text

    pagestyle{fancy}
    fancyhf{}

    NewDocumentCommand{firstpage}{momm}{%
    IfNoValueTF{#2}
    {setfirstpage{#1}{#3}{#4}}
    {setfirstpage{#1[#2]}{#3}{#4}}%
    }
    NewDocumentCommand{setfirstpage}{mmm}
    {
    #1{ifnumvalue{page}=1 #2else #3fi}
    }

    firstpage{fancyhead}[c]{first page}{other pages}
    % can also be firstpage{chead}{first page}{other pages}

    begin{document}

    lipsum[1-2]

    end{document}





    share|improve this answer




























      4














      I believe it's not useful to try and fit an awkward syntax into a command like that.



      It's more rewarding to spend a bit more time in devising a friendlier syntax. Here I propose



      sethf{
      head = <contents of all headers, except possibly the first>,
      foot = <contents of all footers, except possibly the first>,
      pos = <l|c|r>,
      first = <possible exception for first header/footer>,
      }


      Only one among head or foot should be used in a call of sethf.



      documentclass{article}
      usepackage{fancyhdr,xparse}

      usepackage[paper=a6paper]{geometry} % smaller pictures
      usepackage{lipsum} % mock text

      pagestyle{fancy}
      fancyhf{}

      ExplSyntaxOn
      NewDocumentCommand{sethf}{m}
      {
      tl_clear:N l__lyl_hf_pos_tl
      tl_clear:N l__lyl_hf_first_tl
      tl_clear:N l__lyl_hf_headfoot_tl
      keys_set:nn { lyl/hf } { #1 }
      lyl_hf_set:
      }

      keys_define:nn { lyl/hf }
      {
      head .code:n = __lyl_hf_aux:Nn fancyhead { #1 },
      foot .code:n = __lyl_hf_aux:Nn fancyfoot { #1 },
      pos .tl_set:N = l__lyl_hf_pos_tl,
      first .tl_set:N = l__lyl_hf_first_tl,
      }

      tl_new:N l__lyl_hf_headfoot_tl

      cs_new_protected:Nn __lyl_hf_aux:Nn
      {
      cs_set_eq:NN __lyl_hf_temp:w #1
      tl_set:Nn l__lyl_hf_headfoot_tl { #2 }
      }

      cs_new_protected:Nn lyl_hf_set:
      {
      tl_if_empty:NT l__lyl_hf_first_tl
      {
      tl_set_eq:NN l__lyl_hf_first_tl l__lyl_hf_headfoot_tl
      }
      __lyl_hf_set:NVVV
      __lyl_hf_temp:w % fancyhead or fancyfoot
      l__lyl_hf_pos_tl % position
      l__lyl_hf_first_tl % first page
      l__lyl_hf_headfoot_tl % other pages
      }
      cs_new_protected:Nn __lyl_hf_set:Nnnn
      {
      #1 [ #2 ] { int_compare:nTF { value{page} = 1 } { #3 } { #4 } }
      }
      cs_generate_variant:Nn __lyl_hf_set:Nnnn { NVVV }
      ExplSyntaxOff

      sethf{
      head = other pages,
      first = first page,
      pos = c,
      }

      sethf{pos = r, foot=thepage}

      begin{document}

      lipsum[1-2]

      end{document}


      enter image description here



      To answer your question: use a macro for the duplicate code.



      documentclass{article}
      usepackage{fancyhdr,xparse}

      usepackage[paper=a6paper]{geometry} % smaller pictures
      usepackage{lipsum} % mock text

      pagestyle{fancy}
      fancyhf{}

      NewDocumentCommand{firstpage}{momm}{%
      IfNoValueTF{#2}
      {setfirstpage{#1}{#3}{#4}}
      {setfirstpage{#1[#2]}{#3}{#4}}%
      }
      NewDocumentCommand{setfirstpage}{mmm}
      {
      #1{ifnumvalue{page}=1 #2else #3fi}
      }

      firstpage{fancyhead}[c]{first page}{other pages}
      % can also be firstpage{chead}{first page}{other pages}

      begin{document}

      lipsum[1-2]

      end{document}





      share|improve this answer


























        4












        4








        4







        I believe it's not useful to try and fit an awkward syntax into a command like that.



        It's more rewarding to spend a bit more time in devising a friendlier syntax. Here I propose



        sethf{
        head = <contents of all headers, except possibly the first>,
        foot = <contents of all footers, except possibly the first>,
        pos = <l|c|r>,
        first = <possible exception for first header/footer>,
        }


        Only one among head or foot should be used in a call of sethf.



        documentclass{article}
        usepackage{fancyhdr,xparse}

        usepackage[paper=a6paper]{geometry} % smaller pictures
        usepackage{lipsum} % mock text

        pagestyle{fancy}
        fancyhf{}

        ExplSyntaxOn
        NewDocumentCommand{sethf}{m}
        {
        tl_clear:N l__lyl_hf_pos_tl
        tl_clear:N l__lyl_hf_first_tl
        tl_clear:N l__lyl_hf_headfoot_tl
        keys_set:nn { lyl/hf } { #1 }
        lyl_hf_set:
        }

        keys_define:nn { lyl/hf }
        {
        head .code:n = __lyl_hf_aux:Nn fancyhead { #1 },
        foot .code:n = __lyl_hf_aux:Nn fancyfoot { #1 },
        pos .tl_set:N = l__lyl_hf_pos_tl,
        first .tl_set:N = l__lyl_hf_first_tl,
        }

        tl_new:N l__lyl_hf_headfoot_tl

        cs_new_protected:Nn __lyl_hf_aux:Nn
        {
        cs_set_eq:NN __lyl_hf_temp:w #1
        tl_set:Nn l__lyl_hf_headfoot_tl { #2 }
        }

        cs_new_protected:Nn lyl_hf_set:
        {
        tl_if_empty:NT l__lyl_hf_first_tl
        {
        tl_set_eq:NN l__lyl_hf_first_tl l__lyl_hf_headfoot_tl
        }
        __lyl_hf_set:NVVV
        __lyl_hf_temp:w % fancyhead or fancyfoot
        l__lyl_hf_pos_tl % position
        l__lyl_hf_first_tl % first page
        l__lyl_hf_headfoot_tl % other pages
        }
        cs_new_protected:Nn __lyl_hf_set:Nnnn
        {
        #1 [ #2 ] { int_compare:nTF { value{page} = 1 } { #3 } { #4 } }
        }
        cs_generate_variant:Nn __lyl_hf_set:Nnnn { NVVV }
        ExplSyntaxOff

        sethf{
        head = other pages,
        first = first page,
        pos = c,
        }

        sethf{pos = r, foot=thepage}

        begin{document}

        lipsum[1-2]

        end{document}


        enter image description here



        To answer your question: use a macro for the duplicate code.



        documentclass{article}
        usepackage{fancyhdr,xparse}

        usepackage[paper=a6paper]{geometry} % smaller pictures
        usepackage{lipsum} % mock text

        pagestyle{fancy}
        fancyhf{}

        NewDocumentCommand{firstpage}{momm}{%
        IfNoValueTF{#2}
        {setfirstpage{#1}{#3}{#4}}
        {setfirstpage{#1[#2]}{#3}{#4}}%
        }
        NewDocumentCommand{setfirstpage}{mmm}
        {
        #1{ifnumvalue{page}=1 #2else #3fi}
        }

        firstpage{fancyhead}[c]{first page}{other pages}
        % can also be firstpage{chead}{first page}{other pages}

        begin{document}

        lipsum[1-2]

        end{document}





        share|improve this answer













        I believe it's not useful to try and fit an awkward syntax into a command like that.



        It's more rewarding to spend a bit more time in devising a friendlier syntax. Here I propose



        sethf{
        head = <contents of all headers, except possibly the first>,
        foot = <contents of all footers, except possibly the first>,
        pos = <l|c|r>,
        first = <possible exception for first header/footer>,
        }


        Only one among head or foot should be used in a call of sethf.



        documentclass{article}
        usepackage{fancyhdr,xparse}

        usepackage[paper=a6paper]{geometry} % smaller pictures
        usepackage{lipsum} % mock text

        pagestyle{fancy}
        fancyhf{}

        ExplSyntaxOn
        NewDocumentCommand{sethf}{m}
        {
        tl_clear:N l__lyl_hf_pos_tl
        tl_clear:N l__lyl_hf_first_tl
        tl_clear:N l__lyl_hf_headfoot_tl
        keys_set:nn { lyl/hf } { #1 }
        lyl_hf_set:
        }

        keys_define:nn { lyl/hf }
        {
        head .code:n = __lyl_hf_aux:Nn fancyhead { #1 },
        foot .code:n = __lyl_hf_aux:Nn fancyfoot { #1 },
        pos .tl_set:N = l__lyl_hf_pos_tl,
        first .tl_set:N = l__lyl_hf_first_tl,
        }

        tl_new:N l__lyl_hf_headfoot_tl

        cs_new_protected:Nn __lyl_hf_aux:Nn
        {
        cs_set_eq:NN __lyl_hf_temp:w #1
        tl_set:Nn l__lyl_hf_headfoot_tl { #2 }
        }

        cs_new_protected:Nn lyl_hf_set:
        {
        tl_if_empty:NT l__lyl_hf_first_tl
        {
        tl_set_eq:NN l__lyl_hf_first_tl l__lyl_hf_headfoot_tl
        }
        __lyl_hf_set:NVVV
        __lyl_hf_temp:w % fancyhead or fancyfoot
        l__lyl_hf_pos_tl % position
        l__lyl_hf_first_tl % first page
        l__lyl_hf_headfoot_tl % other pages
        }
        cs_new_protected:Nn __lyl_hf_set:Nnnn
        {
        #1 [ #2 ] { int_compare:nTF { value{page} = 1 } { #3 } { #4 } }
        }
        cs_generate_variant:Nn __lyl_hf_set:Nnnn { NVVV }
        ExplSyntaxOff

        sethf{
        head = other pages,
        first = first page,
        pos = c,
        }

        sethf{pos = r, foot=thepage}

        begin{document}

        lipsum[1-2]

        end{document}


        enter image description here



        To answer your question: use a macro for the duplicate code.



        documentclass{article}
        usepackage{fancyhdr,xparse}

        usepackage[paper=a6paper]{geometry} % smaller pictures
        usepackage{lipsum} % mock text

        pagestyle{fancy}
        fancyhf{}

        NewDocumentCommand{firstpage}{momm}{%
        IfNoValueTF{#2}
        {setfirstpage{#1}{#3}{#4}}
        {setfirstpage{#1[#2]}{#3}{#4}}%
        }
        NewDocumentCommand{setfirstpage}{mmm}
        {
        #1{ifnumvalue{page}=1 #2else #3fi}
        }

        firstpage{fancyhead}[c]{first page}{other pages}
        % can also be firstpage{chead}{first page}{other pages}

        begin{document}

        lipsum[1-2]

        end{document}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 1 hour ago









        egregegreg

        722k8719153215




        722k8719153215






























            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%2f476544%2fto-be-or-not-to-be-optional-arguments-inside-definition-of-macro%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

            Щит и меч (фильм) Содержание Названия серий | Сюжет |...

            is 'sed' thread safeWhat should someone know about using Python scripts in the shell?Nexenta bash script uses...

            Meter-Bus Содержание Параметры шины | Стандартизация |...