1

In a scenario where attempting to build multiple documents where each document will have a page with their own (long) table using common data. The following is a minimal example of the table definition:

\documentclass{article}
\usepackage{tabularray}
\begin{document}

\begin{longtblr}{
    colspec={m{100pt}X},
    rowhead=1,
    width=\textwidth,
}
    Key & Value \\
    first & desc1 \\
    second & desc2 \\
\end{longtblr}

\end{document}

I was hoping to use the \input command to define which rows a table would use, allowing a table definition in one document to use a different set of rows than another document. For example:

[test.tex]
\begin{longtblr}{
    colspec={m{100pt}X},
    rowhead=1,
    width=\textwidth,
}
    Key & Value \\
    \input{test-row1}
    \input{test-row2}
\end{longtblr}
[test-row1.tex]
second & desc1 \\

However, this approach will yield the following error:

! Misplaced alignment tab character &.

Is there a way to make this work?

A current workaround is to split each column into their own include file as follows, but hoping to avoid this:

    \input{test-r2c1} & \input{test-r2c2} \\
3

2 Answers 2

2

Using David Carlisle's "horrible hack" in the linked question above, we have (also read the comment in the code to understand what it does)

%! TEX program = pdflatex
\documentclass{article}
\usepackage{tabularray}
\begin{document}


\ExplSyntaxOn
% read the file content
\file_get:nnN {test-row1} {\ExplSyntaxOff} \testrowone
\ExplSyntaxOff

\begin{longtblr}[expand=\expandafter]{
    colspec={m{100pt}X},
    rowhead=1,
    width=\textwidth
}
    Key & Value \\
    first & desc1 \\
    \expandafter\empty\testrowone
    %\expandafter\empty\testrowtwo, etc.
\end{longtblr}

\end{document}
5
  • It's a bit ugly however, since the "read file content" part is separated from the actual include part.
    – user202729
    Apr 20, 2022 at 18:06
  • Also remind me to write documentation for my other package (which is a bit applicable here) soon...
    – user202729
    Apr 20, 2022 at 18:07
  • Thanks, this works great for my use case. A lot of new commands (to me) to research.
    – jdknight
    Apr 21, 2022 at 0:39
  • I had a similar problem here that was a bit frustrating since \input rows worked fine in the past without latex3 hacks.
    – Fran
    Apr 21, 2022 at 0:59
  • If you are not at the start of the project, and you handle R, consider not scatter your row data in many files but one or few databases that can be managed to extract subsets or merge data in many ways and can be showed as LaTeX tables. An example here that show as only some of the 32 rows and some of the 11 variables of mtcars data frame.
    – Fran
    Apr 21, 2022 at 1:37
3

With the latest functional version 2022G (2022-05-22) and tabularray version 2022B (2022-06-01), it is easy to input files inside tables.

There is a new functional library and outer spec evaluate in tabularray package. With this library, you can make tabularray evaluate every occurrence of a specified protected function (which must be defined with \prgNewFunction) and replace it with the return value before splitting table body:

\documentclass{article}

\usepackage{tabularray}
\UseTblrLibrary{functional}

\begin{filecontents*}[overwrite]{test1.tex}
first & desc1 \\
\end{filecontents*}

\begin{filecontents*}[overwrite]{test2.tex}
second & desc2 \\
\end{filecontents*}

\begin{document}

\begin{tblr}[evaluate=\fileInput]{hlines}
  Key & Value \\
  \fileInput{test1}
  \fileInput{test2}
\end{tblr}

\end{document}

In the example \fileInput is a predefined function in functional package.

enter image description here

In general, it is possible to generate the whole table body with a function with this library.

9
  • Not really related, but about the functional library... I think its main advantage is not "more intuitive for users", but that you can nest unexpandable function without clunky temporary variables.
    – user202729
    Apr 22, 2022 at 4:44
  • As long as you need to use the library to do something other than as a normal programming language (e.g. passing the result of some computation into another command from another library) you need to know somewhat of how TeX expansion works etc. for now I think it would be better to explain (in the documentation) how to mix functional library with normal expl3 code.
    – user202729
    Apr 22, 2022 at 4:49
  • (also when will you implement expand supporting multiple arguments)
    – user202729
    Apr 22, 2022 at 4:52
  • 1
    @user202729 I will implement the multiple-expansion feature eventually. But my todo list is quite long. So I can not tell you when to do it.
    – L.J.R.
    Apr 22, 2022 at 13:14
  • 1
    Yes, I think you need to deal with some EOF marker (tex.stackexchange.com/questions/516031/… and linked post) (didn't test however)
    – user202729
    Jun 4, 2022 at 5:25

You must log in to answer this question.

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