3

How to fix the following error, so that the table saved in \MyTable can be read by tabularray?

./tabularrayfilecatch.tex:29: Misplaced alignment tab character &. \MyTable ->1 & 2 \ 3 & 4 \ 5 & 6 \ 7 & 8 \ 9 & 10 \ 11 & 12 \ 13 & 14 ... l.29 \end {tblr}

\begin{filecontents*}{mytable.tex}
    1  &   2  \\
    3  &   4  \\
    5  &   6  \\
    7  &   8  \\
    9  &  10  \\
    11  &  12  \\
    13  &  14  \\
    15  &  16  \\
    17  &  18  \\
    19  &  20  \\
\end{filecontents*}

\documentclass{article} \usepackage[paperheight=75mm]{geometry} \usepackage{xcolor, catchfile} \usepackage{tabularray} \UseTblrLibrary{booktabs,siunitx} \begin{document}

\CatchFileDef{\MyTable}{mytable.tex}{}

\begin{tblr}[ long ]{ colspec = {X S} }
    \toprule[1.5pt]
    One & {{{Two}}}  \\
    \midrule
    \MyTable
    \bottomrule[1.5pt]
\end{tblr}

\end{document}

Diaa
  • 9,599
  • catchfile has no role here. Also \newcommand{\MyTable}{1\\2} would result in an error. The documentation says to use \NewTableCommand{\MyTable}{...}, but it fails as well. – egreg Jul 09 '21 at 09:55
  • @egreg \NewTableCommand is for defining commands which only change the styles of the table. – L.J.R. Jul 15 '21 at 03:42

3 Answers3

3

The tblr wants to see \MyTable expanded (in order to see the & tabs and \\ linebreaks). So I save the introduction of the tblr environment in \tmp and then \expandafter\tmp\MyTable...

\begin{filecontents*}{mytable.tex}
    1  &   2  \\
    3  &   4  \\
    5  &   6  \\
    7  &   8  \\
    9  &  10  \\
    11  &  12  \\
    13  &  14  \\
    15  &  16  \\
    17  &  18  \\
    19  &  20  \\
\end{filecontents*}

\documentclass{article} \usepackage[paperheight=75mm]{geometry} \usepackage{xcolor, catchfile} \usepackage{tabularray} \UseTblrLibrary{booktabs,siunitx} \begin{document}

\CatchFileDef{\MyTable}{mytable.tex}{}

\def\tmp{\begin{tblr}[ long ]{ colspec = {X S} }
    \toprule[1.5pt]
    One & {{{Two}}}  \\
    \midrule}
    \expandafter\tmp\MyTable
    \bottomrule[1.5pt]
\end{tblr}

\end{document}

enter image description here

2

As Steven B. Segletes already said:

The tblr-environment wants to "see" \MyTable expanded.

You can achieve this as shown by Steven B. Segletes via temporary macros.

Another approach is having two macro-arguments and expanding the first macro-argument before exchanging it with the second one:

\begin{filecontents*}{mytable.tex}
    1  &   2  \\
    3  &   4  \\
    5  &   6  \\
    7  &   8  \\
    9  &  10  \\
    11  &  12  \\
    13  &  14  \\
    15  &  16  \\
    17  &  18  \\
    19  &  20  \\
\end{filecontents*}

\documentclass{article} \usepackage[paperheight=75mm]{geometry} \usepackage{xcolor, catchfile} \usepackage{tabularray} \UseTblrLibrary{booktabs,siunitx}

\newcommand\Exchange[2]{#2#1}

\begin{document}

\CatchFileDef{\MyTable}{mytable.tex}{}

\expandafter\Exchange\expandafter{\MyTable}{% \begin{tblr}[ long ]{ colspec = {X S} }% \toprule[1.5pt]% One & {{{Two}}} \% \midrule }% \bottomrule[1.5pt] \end{tblr}

\end{document}

enter image description here

Ulrich Diez
  • 28,770
2

With version 2021M (2021-08-01) of tabularray package, you can use expand option to expand every occurrence of a specified macro once:

\begin{longtblr}[expand=\MyTable]{ colspec = {X S} }
     \toprule[1.5pt]
        One & {{{Two}}}  \\
     \midrule
        \MyTable
     \bottomrule[1.5pt]
\end{longtblr}
L.J.R.
  • 10,932