An extra row in the table generated by whileboolexpr command











up vote
3
down vote

favorite












I used whileboolexpr command from etoolbox package to generate a table. But there was an extra unwanted row. What is the problem with the following code?
enter image description here



documentclass[12pt]{article}

usepackage{etoolbox}

begin{document}

newcounter{row}
newcounter{rowcount}
newcommand{myand}{&}

newcommand{mylines}{%
setcounter{rowcount}{0}%
whileboolexpr{
test{ifnumless{value{rowcount}}{value{row}}}
}{%
stepcounter{rowcount}%
Number myand 1 myand 2 myand 3 \ hline
}%
}

newcommand{mytable}[1]{
setcounter{row}{#1}%
begin{tabular}{|c|*{3}{c|}}
hline
mylines
end{tabular}%
}

mytable{4}

end{document}









share|improve this question


















  • 2




    LaTeX has already started a new cell when it realizes the loop should end.
    – egreg
    Nov 19 at 10:12















up vote
3
down vote

favorite












I used whileboolexpr command from etoolbox package to generate a table. But there was an extra unwanted row. What is the problem with the following code?
enter image description here



documentclass[12pt]{article}

usepackage{etoolbox}

begin{document}

newcounter{row}
newcounter{rowcount}
newcommand{myand}{&}

newcommand{mylines}{%
setcounter{rowcount}{0}%
whileboolexpr{
test{ifnumless{value{rowcount}}{value{row}}}
}{%
stepcounter{rowcount}%
Number myand 1 myand 2 myand 3 \ hline
}%
}

newcommand{mytable}[1]{
setcounter{row}{#1}%
begin{tabular}{|c|*{3}{c|}}
hline
mylines
end{tabular}%
}

mytable{4}

end{document}









share|improve this question


















  • 2




    LaTeX has already started a new cell when it realizes the loop should end.
    – egreg
    Nov 19 at 10:12













up vote
3
down vote

favorite









up vote
3
down vote

favorite











I used whileboolexpr command from etoolbox package to generate a table. But there was an extra unwanted row. What is the problem with the following code?
enter image description here



documentclass[12pt]{article}

usepackage{etoolbox}

begin{document}

newcounter{row}
newcounter{rowcount}
newcommand{myand}{&}

newcommand{mylines}{%
setcounter{rowcount}{0}%
whileboolexpr{
test{ifnumless{value{rowcount}}{value{row}}}
}{%
stepcounter{rowcount}%
Number myand 1 myand 2 myand 3 \ hline
}%
}

newcommand{mytable}[1]{
setcounter{row}{#1}%
begin{tabular}{|c|*{3}{c|}}
hline
mylines
end{tabular}%
}

mytable{4}

end{document}









share|improve this question













I used whileboolexpr command from etoolbox package to generate a table. But there was an extra unwanted row. What is the problem with the following code?
enter image description here



documentclass[12pt]{article}

usepackage{etoolbox}

begin{document}

newcounter{row}
newcounter{rowcount}
newcommand{myand}{&}

newcommand{mylines}{%
setcounter{rowcount}{0}%
whileboolexpr{
test{ifnumless{value{rowcount}}{value{row}}}
}{%
stepcounter{rowcount}%
Number myand 1 myand 2 myand 3 \ hline
}%
}

newcommand{mytable}[1]{
setcounter{row}{#1}%
begin{tabular}{|c|*{3}{c|}}
hline
mylines
end{tabular}%
}

mytable{4}

end{document}






tables etoolbox






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 9:45









Z.H.

1,7411326




1,7411326








  • 2




    LaTeX has already started a new cell when it realizes the loop should end.
    – egreg
    Nov 19 at 10:12














  • 2




    LaTeX has already started a new cell when it realizes the loop should end.
    – egreg
    Nov 19 at 10:12








2




2




LaTeX has already started a new cell when it realizes the loop should end.
– egreg
Nov 19 at 10:12




LaTeX has already started a new cell when it realizes the loop should end.
– egreg
Nov 19 at 10:12










3 Answers
3






active

oldest

votes

















up vote
2
down vote



accepted










As egreg says, LaTeX starts the next cell before it realises that the table has finished. I realise that your real application is more complex, but a really efficient way to implement your MWE is using prg_replicate:nn from LaTeX3 to produce:



enter image description here



Here's the code:



documentclass{article}
usepackage{expl3}
ExplSyntaxOn
newcommandmytable[1]{%
begin{tabular}{|c|*{3}{c|}}hline
prg_replicate:nn {#1} {Number &1&2&3\hline}
end{tabular}%
}
ExplSyntaxOff
begin{document}

mytable{4}

end{document}


Edit



Given the comment below, here is a more general approach that might be closer to what is required. It produces



enter image description here



from the input



  mytable{
{one &1&2&3},
{two &4&6},
{three&1},
{four &1&2&2}
}


where one, two, ... are some random macros.



The mytable macro accepts a comma separated list of table-rows. By default the table has four columns but this can be changes with an optional argument so that, for example, mytable[6]{...} gives a table with 6 columns. There is an obvious issue here with "dangling hlines. This is easy enough to fix but as the OP does not gives details as to what is actually required there is little doing this without more information. For the reasons explained in the booktabs manual I'd drop the vertical rules and use toprule and bottomrule for the top and bottom rules, this would fix this problem and produce:



enter image description here



Here's the code:



documentclass{article}
usepackage{expl3}
usepackage{booktabs}

ExplSyntaxOn
clist_new:N l_table_clist
newcommandmytable[2][4]{
clist_set:Nn l_table_clist {#2}
begin{tabular}{*{#1}{c}}toprule
clist_use:Nn l_table_clist {\hline}
\bottomrule
end{tabular}%
}
ExplSyntaxOff

begin{document}

newcommandone{One}
newcommandtwo{Two}
newcommandthree{Three}
newcommandfour{Four}
mytable{
{one&1&2&3},
{two& 4&6},
{three& 1},
{four&1&2&2}
}

end{document}





share|improve this answer























  • Yes, my real application is more complex: number of columns is not fixed, and all cells are generated by commands, and every row is different. So I could not accept your answer.
    – Z.H.
    Nov 19 at 10:44










  • @Z.H. Without knowing what you want to do it is hard to suggest what you should do except provide a better MWE. I suspect that there are better tools that you could use. Above I have given another approach. You might also find posts like tex.stackexchange.com/questions/367979 helpful
    – Andrew
    Nov 19 at 19:30


















up vote
3
down vote













My weekly (some say daily) xintFor exercise.



documentclass[12pt]{article}

usepackage{xinttools}

newcommand{mytable}[1]{
begin{tabular}{|c|*{3}{c|}}
hline
xintFor* ##1 in {xintSeq{1}{#1}}:
{%
Number & 1 & 2 & 3 \ hline
}%
end{tabular}%
}

begin{document}

mytable{4}

end{document}


enter image description here






share|improve this answer





















  • The idea here is that each row may be entirely decided by the value of ##1 which will be 1, then 2, then
    – jfbu
    Nov 19 at 22:12


















up vote
1
down vote













You can generate the row code outside of the tabular:



enter image description here



documentclass{article}

usepackage{etoolbox}

begin{document}

newcounter{row}
newcommand{myand}{&}

makeatletter
newcommand{mylines}[1]{%
setcounter{row}{#1}%
def@mylines{}%
whileboolexpr{%
test{ifnumless{0}{value{row}}}%
}{%
addtocounter{row}{-1}%
xdef@mylines{@mylines hline
Number myand 1 myand 2 myand 3 \
}%
}%
}

newcommand{mytable}[1]{%
{lethlinerelax % Avoid expansion of hline in xdef
let\relax % Avoid expansion of \ in xdef
mylines{#1}}% This generates @mylines
begin{tabular}{ | c | *{3}{c|} }
@mylines hline % Print @mylines
end{tabular}%
}
makeatother

mytable{4}

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',
    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%2f460708%2fan-extra-row-in-the-table-generated-by-whileboolexpr-command%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote



    accepted










    As egreg says, LaTeX starts the next cell before it realises that the table has finished. I realise that your real application is more complex, but a really efficient way to implement your MWE is using prg_replicate:nn from LaTeX3 to produce:



    enter image description here



    Here's the code:



    documentclass{article}
    usepackage{expl3}
    ExplSyntaxOn
    newcommandmytable[1]{%
    begin{tabular}{|c|*{3}{c|}}hline
    prg_replicate:nn {#1} {Number &1&2&3\hline}
    end{tabular}%
    }
    ExplSyntaxOff
    begin{document}

    mytable{4}

    end{document}


    Edit



    Given the comment below, here is a more general approach that might be closer to what is required. It produces



    enter image description here



    from the input



      mytable{
    {one &1&2&3},
    {two &4&6},
    {three&1},
    {four &1&2&2}
    }


    where one, two, ... are some random macros.



    The mytable macro accepts a comma separated list of table-rows. By default the table has four columns but this can be changes with an optional argument so that, for example, mytable[6]{...} gives a table with 6 columns. There is an obvious issue here with "dangling hlines. This is easy enough to fix but as the OP does not gives details as to what is actually required there is little doing this without more information. For the reasons explained in the booktabs manual I'd drop the vertical rules and use toprule and bottomrule for the top and bottom rules, this would fix this problem and produce:



    enter image description here



    Here's the code:



    documentclass{article}
    usepackage{expl3}
    usepackage{booktabs}

    ExplSyntaxOn
    clist_new:N l_table_clist
    newcommandmytable[2][4]{
    clist_set:Nn l_table_clist {#2}
    begin{tabular}{*{#1}{c}}toprule
    clist_use:Nn l_table_clist {\hline}
    \bottomrule
    end{tabular}%
    }
    ExplSyntaxOff

    begin{document}

    newcommandone{One}
    newcommandtwo{Two}
    newcommandthree{Three}
    newcommandfour{Four}
    mytable{
    {one&1&2&3},
    {two& 4&6},
    {three& 1},
    {four&1&2&2}
    }

    end{document}





    share|improve this answer























    • Yes, my real application is more complex: number of columns is not fixed, and all cells are generated by commands, and every row is different. So I could not accept your answer.
      – Z.H.
      Nov 19 at 10:44










    • @Z.H. Without knowing what you want to do it is hard to suggest what you should do except provide a better MWE. I suspect that there are better tools that you could use. Above I have given another approach. You might also find posts like tex.stackexchange.com/questions/367979 helpful
      – Andrew
      Nov 19 at 19:30















    up vote
    2
    down vote



    accepted










    As egreg says, LaTeX starts the next cell before it realises that the table has finished. I realise that your real application is more complex, but a really efficient way to implement your MWE is using prg_replicate:nn from LaTeX3 to produce:



    enter image description here



    Here's the code:



    documentclass{article}
    usepackage{expl3}
    ExplSyntaxOn
    newcommandmytable[1]{%
    begin{tabular}{|c|*{3}{c|}}hline
    prg_replicate:nn {#1} {Number &1&2&3\hline}
    end{tabular}%
    }
    ExplSyntaxOff
    begin{document}

    mytable{4}

    end{document}


    Edit



    Given the comment below, here is a more general approach that might be closer to what is required. It produces



    enter image description here



    from the input



      mytable{
    {one &1&2&3},
    {two &4&6},
    {three&1},
    {four &1&2&2}
    }


    where one, two, ... are some random macros.



    The mytable macro accepts a comma separated list of table-rows. By default the table has four columns but this can be changes with an optional argument so that, for example, mytable[6]{...} gives a table with 6 columns. There is an obvious issue here with "dangling hlines. This is easy enough to fix but as the OP does not gives details as to what is actually required there is little doing this without more information. For the reasons explained in the booktabs manual I'd drop the vertical rules and use toprule and bottomrule for the top and bottom rules, this would fix this problem and produce:



    enter image description here



    Here's the code:



    documentclass{article}
    usepackage{expl3}
    usepackage{booktabs}

    ExplSyntaxOn
    clist_new:N l_table_clist
    newcommandmytable[2][4]{
    clist_set:Nn l_table_clist {#2}
    begin{tabular}{*{#1}{c}}toprule
    clist_use:Nn l_table_clist {\hline}
    \bottomrule
    end{tabular}%
    }
    ExplSyntaxOff

    begin{document}

    newcommandone{One}
    newcommandtwo{Two}
    newcommandthree{Three}
    newcommandfour{Four}
    mytable{
    {one&1&2&3},
    {two& 4&6},
    {three& 1},
    {four&1&2&2}
    }

    end{document}





    share|improve this answer























    • Yes, my real application is more complex: number of columns is not fixed, and all cells are generated by commands, and every row is different. So I could not accept your answer.
      – Z.H.
      Nov 19 at 10:44










    • @Z.H. Without knowing what you want to do it is hard to suggest what you should do except provide a better MWE. I suspect that there are better tools that you could use. Above I have given another approach. You might also find posts like tex.stackexchange.com/questions/367979 helpful
      – Andrew
      Nov 19 at 19:30













    up vote
    2
    down vote



    accepted







    up vote
    2
    down vote



    accepted






    As egreg says, LaTeX starts the next cell before it realises that the table has finished. I realise that your real application is more complex, but a really efficient way to implement your MWE is using prg_replicate:nn from LaTeX3 to produce:



    enter image description here



    Here's the code:



    documentclass{article}
    usepackage{expl3}
    ExplSyntaxOn
    newcommandmytable[1]{%
    begin{tabular}{|c|*{3}{c|}}hline
    prg_replicate:nn {#1} {Number &1&2&3\hline}
    end{tabular}%
    }
    ExplSyntaxOff
    begin{document}

    mytable{4}

    end{document}


    Edit



    Given the comment below, here is a more general approach that might be closer to what is required. It produces



    enter image description here



    from the input



      mytable{
    {one &1&2&3},
    {two &4&6},
    {three&1},
    {four &1&2&2}
    }


    where one, two, ... are some random macros.



    The mytable macro accepts a comma separated list of table-rows. By default the table has four columns but this can be changes with an optional argument so that, for example, mytable[6]{...} gives a table with 6 columns. There is an obvious issue here with "dangling hlines. This is easy enough to fix but as the OP does not gives details as to what is actually required there is little doing this without more information. For the reasons explained in the booktabs manual I'd drop the vertical rules and use toprule and bottomrule for the top and bottom rules, this would fix this problem and produce:



    enter image description here



    Here's the code:



    documentclass{article}
    usepackage{expl3}
    usepackage{booktabs}

    ExplSyntaxOn
    clist_new:N l_table_clist
    newcommandmytable[2][4]{
    clist_set:Nn l_table_clist {#2}
    begin{tabular}{*{#1}{c}}toprule
    clist_use:Nn l_table_clist {\hline}
    \bottomrule
    end{tabular}%
    }
    ExplSyntaxOff

    begin{document}

    newcommandone{One}
    newcommandtwo{Two}
    newcommandthree{Three}
    newcommandfour{Four}
    mytable{
    {one&1&2&3},
    {two& 4&6},
    {three& 1},
    {four&1&2&2}
    }

    end{document}





    share|improve this answer














    As egreg says, LaTeX starts the next cell before it realises that the table has finished. I realise that your real application is more complex, but a really efficient way to implement your MWE is using prg_replicate:nn from LaTeX3 to produce:



    enter image description here



    Here's the code:



    documentclass{article}
    usepackage{expl3}
    ExplSyntaxOn
    newcommandmytable[1]{%
    begin{tabular}{|c|*{3}{c|}}hline
    prg_replicate:nn {#1} {Number &1&2&3\hline}
    end{tabular}%
    }
    ExplSyntaxOff
    begin{document}

    mytable{4}

    end{document}


    Edit



    Given the comment below, here is a more general approach that might be closer to what is required. It produces



    enter image description here



    from the input



      mytable{
    {one &1&2&3},
    {two &4&6},
    {three&1},
    {four &1&2&2}
    }


    where one, two, ... are some random macros.



    The mytable macro accepts a comma separated list of table-rows. By default the table has four columns but this can be changes with an optional argument so that, for example, mytable[6]{...} gives a table with 6 columns. There is an obvious issue here with "dangling hlines. This is easy enough to fix but as the OP does not gives details as to what is actually required there is little doing this without more information. For the reasons explained in the booktabs manual I'd drop the vertical rules and use toprule and bottomrule for the top and bottom rules, this would fix this problem and produce:



    enter image description here



    Here's the code:



    documentclass{article}
    usepackage{expl3}
    usepackage{booktabs}

    ExplSyntaxOn
    clist_new:N l_table_clist
    newcommandmytable[2][4]{
    clist_set:Nn l_table_clist {#2}
    begin{tabular}{*{#1}{c}}toprule
    clist_use:Nn l_table_clist {\hline}
    \bottomrule
    end{tabular}%
    }
    ExplSyntaxOff

    begin{document}

    newcommandone{One}
    newcommandtwo{Two}
    newcommandthree{Three}
    newcommandfour{Four}
    mytable{
    {one&1&2&3},
    {two& 4&6},
    {three& 1},
    {four&1&2&2}
    }

    end{document}






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 20 at 3:51

























    answered Nov 19 at 10:31









    Andrew

    29.3k34178




    29.3k34178












    • Yes, my real application is more complex: number of columns is not fixed, and all cells are generated by commands, and every row is different. So I could not accept your answer.
      – Z.H.
      Nov 19 at 10:44










    • @Z.H. Without knowing what you want to do it is hard to suggest what you should do except provide a better MWE. I suspect that there are better tools that you could use. Above I have given another approach. You might also find posts like tex.stackexchange.com/questions/367979 helpful
      – Andrew
      Nov 19 at 19:30


















    • Yes, my real application is more complex: number of columns is not fixed, and all cells are generated by commands, and every row is different. So I could not accept your answer.
      – Z.H.
      Nov 19 at 10:44










    • @Z.H. Without knowing what you want to do it is hard to suggest what you should do except provide a better MWE. I suspect that there are better tools that you could use. Above I have given another approach. You might also find posts like tex.stackexchange.com/questions/367979 helpful
      – Andrew
      Nov 19 at 19:30
















    Yes, my real application is more complex: number of columns is not fixed, and all cells are generated by commands, and every row is different. So I could not accept your answer.
    – Z.H.
    Nov 19 at 10:44




    Yes, my real application is more complex: number of columns is not fixed, and all cells are generated by commands, and every row is different. So I could not accept your answer.
    – Z.H.
    Nov 19 at 10:44












    @Z.H. Without knowing what you want to do it is hard to suggest what you should do except provide a better MWE. I suspect that there are better tools that you could use. Above I have given another approach. You might also find posts like tex.stackexchange.com/questions/367979 helpful
    – Andrew
    Nov 19 at 19:30




    @Z.H. Without knowing what you want to do it is hard to suggest what you should do except provide a better MWE. I suspect that there are better tools that you could use. Above I have given another approach. You might also find posts like tex.stackexchange.com/questions/367979 helpful
    – Andrew
    Nov 19 at 19:30










    up vote
    3
    down vote













    My weekly (some say daily) xintFor exercise.



    documentclass[12pt]{article}

    usepackage{xinttools}

    newcommand{mytable}[1]{
    begin{tabular}{|c|*{3}{c|}}
    hline
    xintFor* ##1 in {xintSeq{1}{#1}}:
    {%
    Number & 1 & 2 & 3 \ hline
    }%
    end{tabular}%
    }

    begin{document}

    mytable{4}

    end{document}


    enter image description here






    share|improve this answer





















    • The idea here is that each row may be entirely decided by the value of ##1 which will be 1, then 2, then
      – jfbu
      Nov 19 at 22:12















    up vote
    3
    down vote













    My weekly (some say daily) xintFor exercise.



    documentclass[12pt]{article}

    usepackage{xinttools}

    newcommand{mytable}[1]{
    begin{tabular}{|c|*{3}{c|}}
    hline
    xintFor* ##1 in {xintSeq{1}{#1}}:
    {%
    Number & 1 & 2 & 3 \ hline
    }%
    end{tabular}%
    }

    begin{document}

    mytable{4}

    end{document}


    enter image description here






    share|improve this answer





















    • The idea here is that each row may be entirely decided by the value of ##1 which will be 1, then 2, then
      – jfbu
      Nov 19 at 22:12













    up vote
    3
    down vote










    up vote
    3
    down vote









    My weekly (some say daily) xintFor exercise.



    documentclass[12pt]{article}

    usepackage{xinttools}

    newcommand{mytable}[1]{
    begin{tabular}{|c|*{3}{c|}}
    hline
    xintFor* ##1 in {xintSeq{1}{#1}}:
    {%
    Number & 1 & 2 & 3 \ hline
    }%
    end{tabular}%
    }

    begin{document}

    mytable{4}

    end{document}


    enter image description here






    share|improve this answer












    My weekly (some say daily) xintFor exercise.



    documentclass[12pt]{article}

    usepackage{xinttools}

    newcommand{mytable}[1]{
    begin{tabular}{|c|*{3}{c|}}
    hline
    xintFor* ##1 in {xintSeq{1}{#1}}:
    {%
    Number & 1 & 2 & 3 \ hline
    }%
    end{tabular}%
    }

    begin{document}

    mytable{4}

    end{document}


    enter image description here







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 19 at 21:32









    jfbu

    44.5k65143




    44.5k65143












    • The idea here is that each row may be entirely decided by the value of ##1 which will be 1, then 2, then
      – jfbu
      Nov 19 at 22:12


















    • The idea here is that each row may be entirely decided by the value of ##1 which will be 1, then 2, then
      – jfbu
      Nov 19 at 22:12
















    The idea here is that each row may be entirely decided by the value of ##1 which will be 1, then 2, then
    – jfbu
    Nov 19 at 22:12




    The idea here is that each row may be entirely decided by the value of ##1 which will be 1, then 2, then
    – jfbu
    Nov 19 at 22:12










    up vote
    1
    down vote













    You can generate the row code outside of the tabular:



    enter image description here



    documentclass{article}

    usepackage{etoolbox}

    begin{document}

    newcounter{row}
    newcommand{myand}{&}

    makeatletter
    newcommand{mylines}[1]{%
    setcounter{row}{#1}%
    def@mylines{}%
    whileboolexpr{%
    test{ifnumless{0}{value{row}}}%
    }{%
    addtocounter{row}{-1}%
    xdef@mylines{@mylines hline
    Number myand 1 myand 2 myand 3 \
    }%
    }%
    }

    newcommand{mytable}[1]{%
    {lethlinerelax % Avoid expansion of hline in xdef
    let\relax % Avoid expansion of \ in xdef
    mylines{#1}}% This generates @mylines
    begin{tabular}{ | c | *{3}{c|} }
    @mylines hline % Print @mylines
    end{tabular}%
    }
    makeatother

    mytable{4}

    end{document}





    share|improve this answer

























      up vote
      1
      down vote













      You can generate the row code outside of the tabular:



      enter image description here



      documentclass{article}

      usepackage{etoolbox}

      begin{document}

      newcounter{row}
      newcommand{myand}{&}

      makeatletter
      newcommand{mylines}[1]{%
      setcounter{row}{#1}%
      def@mylines{}%
      whileboolexpr{%
      test{ifnumless{0}{value{row}}}%
      }{%
      addtocounter{row}{-1}%
      xdef@mylines{@mylines hline
      Number myand 1 myand 2 myand 3 \
      }%
      }%
      }

      newcommand{mytable}[1]{%
      {lethlinerelax % Avoid expansion of hline in xdef
      let\relax % Avoid expansion of \ in xdef
      mylines{#1}}% This generates @mylines
      begin{tabular}{ | c | *{3}{c|} }
      @mylines hline % Print @mylines
      end{tabular}%
      }
      makeatother

      mytable{4}

      end{document}





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        You can generate the row code outside of the tabular:



        enter image description here



        documentclass{article}

        usepackage{etoolbox}

        begin{document}

        newcounter{row}
        newcommand{myand}{&}

        makeatletter
        newcommand{mylines}[1]{%
        setcounter{row}{#1}%
        def@mylines{}%
        whileboolexpr{%
        test{ifnumless{0}{value{row}}}%
        }{%
        addtocounter{row}{-1}%
        xdef@mylines{@mylines hline
        Number myand 1 myand 2 myand 3 \
        }%
        }%
        }

        newcommand{mytable}[1]{%
        {lethlinerelax % Avoid expansion of hline in xdef
        let\relax % Avoid expansion of \ in xdef
        mylines{#1}}% This generates @mylines
        begin{tabular}{ | c | *{3}{c|} }
        @mylines hline % Print @mylines
        end{tabular}%
        }
        makeatother

        mytable{4}

        end{document}





        share|improve this answer












        You can generate the row code outside of the tabular:



        enter image description here



        documentclass{article}

        usepackage{etoolbox}

        begin{document}

        newcounter{row}
        newcommand{myand}{&}

        makeatletter
        newcommand{mylines}[1]{%
        setcounter{row}{#1}%
        def@mylines{}%
        whileboolexpr{%
        test{ifnumless{0}{value{row}}}%
        }{%
        addtocounter{row}{-1}%
        xdef@mylines{@mylines hline
        Number myand 1 myand 2 myand 3 \
        }%
        }%
        }

        newcommand{mytable}[1]{%
        {lethlinerelax % Avoid expansion of hline in xdef
        let\relax % Avoid expansion of \ in xdef
        mylines{#1}}% This generates @mylines
        begin{tabular}{ | c | *{3}{c|} }
        @mylines hline % Print @mylines
        end{tabular}%
        }
        makeatother

        mytable{4}

        end{document}






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 at 22:08









        Werner

        431k599501628




        431k599501628






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2ftex.stackexchange.com%2fquestions%2f460708%2fan-extra-row-in-the-table-generated-by-whileboolexpr-command%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 change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?