Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I started learning LaTeX3 a few weeks ago and I'm currently playing around with tables. Here is my code:

\documentclass{article}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new_protected:Npn \juhu_tablerow:n #1
{
  \int_new:N \l_row_count_int
  \int_set:Nn \l_row_count_int { 1 }
  \prg_replicate:nn {#1}
  {
    \int_use:N \l_row_count_int .~row \\
    \int_incr:N \l_row_count_int
  }
}
\cs_new_eq:NN \tablerow \juhu_tablerow:n
\ExplSyntaxOff

\begin{document}

\begin{tabular}{c}
\tablerow{3}
\end{tabular}

\end{document}

The output of this code is:

1. row
1. row
1. row

The desired output should be like this:

1. row
2. row
3. row

When I replace the tabular environment e.g. with a center environment, I get the expected output.

Now, if I replace the \\ in \int_use:N \l_row_count_int .~row \\ with ,~, \int_incr:N seems to work again even inside the tabular environment, and the output is:

1. row, 2. row, 3. row, 

So, my question is, why increasing the integer doesn't work inside a tabular environment when using multiple rows, and what can I do to get the desired result?

share|improve this question
3  
I think you should change the scope assignment from local to global. Then things work. :) Edit: Oh, and move the integer declaration outside your command too. :) –  Paulo Cereda Mar 8 at 18:36
2  
Also, welcome to TeX.SX! –  Sean Allred Mar 8 at 18:37
    
@PauloCereda sounds like a draft for an answer :) –  cgnieder Mar 8 at 18:49
    
@PauloCereda Your comment solved my problem. Thank you! If you make it an answer I'll accept it. –  JuHu Mar 8 at 18:52

1 Answer 1

up vote 6 down vote accepted

From my humble comment to an answer. :)

You were very close to a solution! The culprit here is the scope: those assignments are happening locally; let us make them global.

Before we proceed, we must move the integer declaration outside your command definition, otherwise an error will be raised in subsequent calls to \tablerow. Now, a little coding convention, from

\int_new:N \l_row_count_int

to

\int_new:N \g_row_count_int

since our counter became global. :) Now, a couple of replacements (thanks to egreg for the xparse suggestion!):

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn

\int_new:N \g_row_count_int

\cs_new_protected:Npn \juhu_tablerow:n #1
{
  \int_gset:Nn \g_row_count_int { 1 }
  \prg_replicate:nn {#1}
  {
    \int_use:N \g_row_count_int .~row \\
    \int_gincr:N \g_row_count_int
  }
}

\NewDocumentCommand{ \tablerow }{ m }{
    \juhu_tablerow:n{#1}
}
\ExplSyntaxOff

\begin{document}

\begin{tabular}{c}
\tablerow{3}
\end{tabular}

\end{document}

And everything should be working just fine. :)

You could also go with \g_tmpa_int which is a scratch integer for global assignment and save a couple of bucks. :)

share|improve this answer
2  
I'd just replace the \cs_new_eq:NN line with \NewDocumentCommand{\tablerow}{m}{\juhu_tablerow:n{#1}} (loading xparse, of course). –  egreg Mar 8 at 19:18
    
@egreg: thank you, updated. :) –  Paulo Cereda Mar 8 at 19:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.