3
\documentclass{article}
\usepackage{tabularray}
\begin{document}
\ExplSyntaxOn
\newcommand{\Repeat}[1]{\int_step_inline:nnn {1}{#1}{##1 A}} % Need change A to &
\ExplSyntaxOff
\begin{tblr}[expand={\Repeat}]{hlines,vlines}
\Repeat{6} a & b & c \\
\end{tblr}
\end{document}

I have a requirement. I hope to define a command that can take one parameter and generate the specified column. I want to use tabularray instead of others. Is there any solution that can allow the current code to run? What I want to achieve is in the comment.

3
  • 1
    You'll need to define your \Repeat fully expandable instead, so you can't use \int_step_inline:nnn (or you'll need to define your command to build the table body before tabularray scans it). Commented 20 hours ago
  • @Skillmon Is there any way to achieve this? My requirement is to input a number and then generate the corresponding number of columns. Commented 19 hours ago
  • Of course this is possible, see my answer :) Commented 19 hours ago

2 Answers 2

3

Since the original goal is just to turn \Repeat{6} into 1&2&3&4&5&6& we can also use a more lightweight solution. My other answer defines an expandable general \int_step_inline:nnnn like function. This answer only directly creates your output with a minimal definition.

Same reason for expand={\expanded} as in the other answer.

\documentclass{article}
\usepackage{tabularray}

\ExplSyntaxOn
\NewExpandableDocumentCommand \Repeat { m }
  { \int_step_tokens:nn {#1} { \use_ii_i:nn {&} } }
\ExplSyntaxOff

\begin{document}
\begin{tblr}[expand={\expanded}]{hlines,vlines}
  \expanded{\Repeat{6}} a & b & c \\
\end{tblr}
\end{document}

enter image description here

1
  • Thank you very much for your answer, I have learned a lot! Commented 18 hours ago
3

\begin{advertisement}[package={etl, expkv-cs}, author=myself]

The following defines a fully expandable variant of \ist_step_inline:nnnn based on \int_step_tokens:nnnn and \etl_replace_all_deep:nnn. The macro is called \intstep and takes an optional followed by two mandatory arguments.

The optional argument accepts the keys (expandable key=value interface powered by expkv-cs)

  • start being the first integer value (default 1),
  • step the step size (default 1), and
  • replace which tokens to replace in the second mandatory argument with the current count (default #).

The first mandatory argument is the final value, and the second mandatory argument is the text that should be repeated.

Caveat: Since this is not using a temporary definition the replace text doesn't abide by TeX's #-doubling rule. As a result \intstep{3}{##1} would result in 111221331, but \intstep[replace=#1]{3}{#1} would result in the expected 123. The #-doubling still needs to be respected if you want to replace # and are inside the definition of another macro.

Problem: the expand= key of tabularray does not fully expand the macro given as its argument, but only one step of expansion. For that reason we instead expand \expanded which then in turn will fully expand \Repeat.

\documentclass{article}
\usepackage{tabularray}
\usepackage{etl}
\usepackage{expkv-cs}

\ExplSyntaxOn
\NewExpandableDocumentCommand \intstep { O{} m m }
  { \intstepKV{#1}{#2}{#3} }
\ekvcHashAndForward\intstepKV\intstepDO
  {
     start   = 1
    ,step    = 1
    ,replace = #
  }
\cs_generate_variant:Nn \int_step_tokens:nnnn { nnne }
\newcommand\intstepDO[3]
  {
    \int_step_tokens:nnne
      { \ekvcValue { start } {#1} }
      { \ekvcValue { step }  {#1} }
      {#2}
      {
        \exp_not:N \etl_replace_all_deep:nnn
          { \exp_not:n {#3} }
          { \ekvcValue {replace} {#1} }
      }
  }

\ExplSyntaxOff

\newcommand\Repeat[1]{\intstep{#1}{##&}}

\begin{document}
\begin{tblr}[expand={\expanded}]{hlines,vlines}
  \expanded{\Repeat{6}} a & b & c \\
\end{tblr}
\end{document}

enter image description here

\end{advertisement}

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.