Re: input Datei in tabular
"Heiko Oberdiek" (via tex-d-l Mailing List) <[email protected]>
| Newsgroups | gmane.comp.tex.german |
|---|---|
| Message-ID | <[email protected]> |
Hallo,
[Das Folgende in Englisch; zu spät gemerkt, dass Deutsch hier passender
gewesen wäre.]
On 2023-02-21 20:24, fi (via tex-d-l Mailing List) wrote:
>
> oft habe ich tabellierte Daten, die aus unterschiedlichen Quellen
> stammen und in einfachen Dateien liegen. Ein vereinfachtes Beispiel
> zeigt die Datei "dat.tex":
>
> 1 & 2 & 3 \\
> 42 & 13 & 5 \\
>
> Diese Datei soll von einem TeX-File per \input eingelesen werden.
>
> \documentclass{article}
> \begin{document}
> \begin{tabular}{|r|r|r|}
> A & B & C \\
> \hline
> \input{dat}
> \hline
> \end{tabular}
> \end{document}
LaTeX's redefinition of \input is not expandable. Thus, there is some
left-over stuff of \input after the latest \\ in dat.tex, starting
a new row. Then, \hline complains, because it wants to be at the start
of a row.
Sometimes, it helps to let \input end in a cell, where some additional
non-expandable stuff does not matter:
Remove the latest \\ in dat.tex and insert it after \input:
\input{dat}\\
But this trick will not always work, e.g. S columns of siunitx will
break, because they need a special parsing of the cell.
Package catchfile can help to read the file contents in a macro:
\usepackage{catchfile}
\CatchFileDef{\TableBody}{dat}{}
[...]
\hline
\TableBody
\hline
[...]
If the table body contains category code trickery (\verb, ...),
then it becomes more complicate:
In the last argument of \CatchFileDef, the category codes should be
setup to verbatim mode. The macro is then read in eTeX's \scantokens:
\CatchFileDef{\TableBody}{dat}{\csname @sanitize\endcsname}% as a start
[...]
\hline
\scantokens\expandafter{\TableBody}
\hline
[...]
Yours sincerely
Heiko
--
FAQ: http://texfragen.de/
Durchsuchbares Archiv: https://www.listserv.dfn.de/sympa/arc/tex-d-l
Unsubscribe/Verwaltung: https://www.listserv.dfn.de/sympa/info/tex-d-l