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} \\
jdknight
  • 113

2 Answers2

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.

L.J.R.
  • 10,932
  • 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 '22 at 04: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 '22 at 04:49
  • (also when will you implement expand supporting multiple arguments) – user202729 Apr 22 '22 at 04:52
  • @user202729 Now you can expand multiple macros by writing evaluate=\TlUse and putting \TlUse before each macro. – L.J.R. Apr 22 '22 at 04:58
  • Actually that would only allow expanding "macro which takes no argument"... either way I guess evaluate=Expand with functional package should be able to do most; but it's somewhat nontrivial to come up with so would be better to include some example in documentation (or link to questions to TeX.SE?) // For the other point, in retrospect \TlSet can be used to transport the data to expl3, except for the limitation that the first character must be l or g; but then this is nontrivial as well (I think unless the user is already familiar with expl3 it's harded it to come up with this one). – user202729 Apr 22 '22 at 12:02
  • 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 '22 at 13:14
  • @user202729 The data types are the same in both functional and expl3 (only functions are different). \TlSet can handle macro names with any leading characters, but this is still undocumented (you may read the code if you are interested). – L.J.R. Apr 22 '22 at 13:18
  • @user202729 It is a pity that I can not expand \@@input inside tabularray tables, although \@@input is said to be expandable. – L.J.R. Jun 04 '22 at 02:14
  • 1
    Yes, I think you need to deal with some EOF marker (https://tex.stackexchange.com/questions/516031/why-is-everyeof-noexpand-needed-to-avoid-file-ended-while-scanning-definitio and linked post) (didn't test however) – user202729 Jun 04 '22 at 05:25
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}

user202729
  • 7,143
  • It's a bit ugly however, since the "read file content" part is separated from the actual include part. – user202729 Apr 20 '22 at 18:06
  • Also remind me to write documentation for my other package (which is a bit applicable here) soon... – user202729 Apr 20 '22 at 18:07
  • Thanks, this works great for my use case. A lot of new commands (to me) to research. – jdknight Apr 21 '22 at 00: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 '22 at 00: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 '22 at 01:37