4

Why new command, defined for example as

\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c}{#2}}

doesn't work in tblr defined in the tabularray˙table on the same way as if it is directly inserted \multicolumn cells (or as it works in tabular and other "classic" tables' environments). For example:

\documentclass{article}
\usepackage{tabularray}
\NewExpandableDocumentCommand\mcc{O{1}m}{\multicolumn{#1}{c}{#2}}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tblr}
\setlength\PreviewBorder{1em}


\begin{document}

\begin{tblr}{
 colspec = {|*{4}{c|} },
hlines
            }
\multicolumn{3}{c|}{abcde}  & \mcc[3]{fghij}    \\
1 & 2 & 3                   & 1 & 2 & 3         \\
\end{tblr}

\end{document}

enter image description here

Do I miss something or tabularray so far doesn't support such shortcuts? I use tabularray Version 2021K (2021-06-05).

Edit: Solution as is provided autoe of package, doesn't work as (I) expected. The following MWE works:

\documentclass{article}
\usepackage{tabularray}
\NewTableCommand{\mcc}[1][1]{\multicolumn{#1}{c}}

\begin{document}

\begin{tblr}{
 hlines, vlines, colspec = {*{6}{c}},
}
 \multicolumn{3}{c|}{abcde}  & \mcc[3]{fghij}  \\
 1 & 2 & 3                   & 1 & 2 & 3     \\
\end{tblr}

\end{document}

Bot use new command ain all instances of \multicolumns doesn't:

\documentclass{article}
\usepackage{tabularray}
\NewTableCommand{\mcc}[1][1]{\multicolumn{#1}{c}}

\begin{document}

\begin{tblr}{
 hlines, vlines, colspec = {*{6}{c}},
}
 \mcc[3]{abcde}  & \mcc[3]{fghij}  \\
 1 & 2 & 3                   & 1 & 2 & 3     \\
\end{tblr}

\end{document}

enter image description here

Do I miss something (again)?

3
  • 2
    The package explicitly looks for \multicolumn or \multirow in order to replace them with its own version.
    – egreg
    Jul 1, 2021 at 12:39
  • @egreg, thank you for info. That is pity. In cases where of complex tables with extensive use of \multicolumn cells showed shorthand are very convenient in table settings.
    – Zarko
    Jul 1, 2021 at 12:45
  • 1
    @Zarko I think the 'native' command here is \SetCell, which I guess one could 'abbreviate'. However, one always has this issue when doing anything verbatim-like, which includes 'read the content and modify'.
    – Joseph Wright
    Jul 1, 2021 at 12:52

1 Answer 1

8

New Answer on 2021-07-02

(Also in https://github.com/lvjr/tabularray/issues/28)

Tabularray package tried its best to support legacy \multicolumn and \multirow commands by doing some dirty hack. But from this question, it turns out that it's impossible to give full support for these two commands, since these two commands conflict with the design of tabularray package.

Therefore I have decided to mark \multicolumn and \multirow as obsolete, and will remove them in version 2022. To make multicolumn or multirow cells, it's better to use \SetCell command in old interfaces, or cell{i}{j} in new interfaces.

Also, for backwards compatibility, I will include the last 2021 version of tabularray as tabularray-2021.sty in future releases for many years. Users could still use it for their old documents by \usepackage{tabularray-2021}.

For your question, it's recommended to write it using new interfaces like the following:

\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{
 hlines, vlines, colspec = {*{6}{c}},
 cell{1}{1,4} = {c=3}{c},
}
 abcde &   &   & fghij &   &   \\
 1     & 2 & 3 & 1     & 2 & 3 \\
\end{tblr}
\end{document}

enter image description here

For another question in the comment, it's recommended to write it using new interfaces like the following:

\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{
  colspec = {|c|*{4}{*{3}{c}|}},
  row{1-3} = {font=\bfseries},
  cell{1}{1} = {r=3}{c},
  cell{1}{2,8} = {c=6}{c},
  cell{2}{2,5,8,11} = {c=3}{c},
}
\hline
  M1 vs. \$  & 5fold &    &    &      &    &    & 10fold &    &    &      &    &    \\
\cline{2-13}
             & Err1  &    &    & Err1 &    &    & Err1   &    &    & Err2 &    &    \\
\cline{2-13} 
             & C1    & C2 & C3 & C1   & C2 & C3 & C1     & C2 & C3 &  C1  & C2 & C3 \\ 
\hline
  M2         & 13    &  7 &  0 & 15   &  5 &  0 & 0      & 20 &  0 &   0  & 20 &  0 \\
  M3         & 11    &  3 &  6 & 12   &  3 &  5 & 3      & 13 &  4 &   0  & 16 &  4 \\
  M4         & 10    & 10 &  0 & 10   & 10 &  0 & 4      & 16 &  0 &   0  & 20 &  0 \\  
\hline
Total        & 34    & 20 &  6 & 37   & 18 &  5 & 7      & 49 &  4 &   0  & 56 &  4 \\  
\hline
\end{tblr}
\end{document}

enter image description here


Old Answer on 2021-07-01

From the manual: "All commands which change the specifications of tables must be defined with \NewTableCommand".

Also tabularray package internally treats \multicolumn and \multirow as commands with only two mandatory arguments.

\documentclass{article}

\usepackage{tabularray}
\NewTableCommand{\mcc}[1][1]{\multicolumn{#1}{c}}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tblr}
\setlength\PreviewBorder{1em}

\begin{document}

\begin{tblr}{
 hlines, vlines, colspec = {*{6}{c}},
}
 \multicolumn{3}{c|}{abcde}  & \mcc[3]{fghij}  \\
 1 & 2 & 3                   & 1 & 2 & 3     \\
\end{tblr}

\end{document}

enter image description here

13
  • 1
    Wouldn't it be best to use \NewTableCommand{\mcc}[2][1]{\multicolumn{#1}{c}#2} such that there are no 'extra' braces?
    – Joseph Wright
    Jul 1, 2021 at 12:55
  • Thank you very much for explanation. I will be nice, if in documentation be explained syntax of \NewTableCommand and added example for its use in case of defining shorthand for \multicolumn .
    – Zarko
    Jul 1, 2021 at 13:07
  • @JosephWright It's impossible. Since we need to extract table commands from each cell text according to the argument number of the table commands in tabularray package.
    – L.J.R.
    Jul 1, 2021 at 13:09
  • @L.J.R. I'm confused: \multicolumn has three arguments, and if we don't remove the braces from the final one, we (at least) force TeX to create an unnecessary group, or in math mode potentially mess up spacing.
    – Joseph Wright
    Jul 1, 2021 at 13:12
  • @Zarko There is an example for NewTableCommand in the manual. But yes, there are still lots of things to be added to the manual.
    – L.J.R.
    Jul 1, 2021 at 13:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .