I would like to include a table stored an external tex file in a latex document via \input inside a \begin{tabular}...\end{tabular} environment. This way the column alignment definitions and everything else apart from the column headers and the table cells is located in the main tex file. At the same time I can still externally update the table numbers by replacing the external file.
However, I get a multitude of error messages when trying this in Overleaf, even in a minimal working example (MWE). This MWE is located under https://www.overleaf.com/read/qqhkdjmtrjsf and reproduced below.
The MWE has 2 (column types 'r' and 'S') x 2 (a row before \hline command or not) x 3 (table in main tex, \input table cells inside tabular, \input whole table in main tex) = 12 cases, 9 of which work as expected. For the other 3 cases, the \hline is not produced correctly and the word "height" appears in the table. Several compilation errors are generated.
In the MWE, the 9 working cases and the first of the 3 problem cases are uncommented, the rest are commented out. Uncomment them (comment out the other problem cases) and recompile to check the compilation errors. Compilation logs are in the folder "logs", compiled pdfs in the folder "pdfs"
Can anybody explain to me why \input does not work inside the tabular environment in the MWE? What is going on? I would not have expected this behavior, based on When should I use \input vs. \include?. Quote:
"\input{filename} imports the commands from filename.tex into the target file; it's equivalent to typing all the commands from filename.tex right into the current file where the \input line is."
"\input is a more lower level macro which simply inputs the content of the given file like it was copy&pasted there manually."
Is there a simple way to fix these issues and still have the external file only contain the table cells?
Update:
The compilation errors can be avoided by defining \expandableinput in the document preamble as suggested in https://tex.stackexchange.com/a/583939/113493 and replacing \input commands by \expandableinput:
\ExplSyntaxOn#
\cs_new:Npn \expandableinput #1
{ \use:c { @@input } { \file_full_name:n {#1} } }
\ExplSyntaxOff
Minimal working example:
\documentclass{article}
\usepackage{graphicx}
\usepackage{txfonts}
\usepackage{siunitx}
\usepackage{array}
\sisetup{detect-weight=true, detect-family=true}
\usepackage[]{hyperref}
\begin{document}
% --------------------------
% CASE 1: works as expected
% --------------------------
\begin{table}
\caption{A test table.}
\label{table.test}
\centering
\begin{tabular}{r r r}
Col1 & Col2 & Col3 \
\hline
Num1 & Num2 & Num3 \
\end{tabular}
\end{table}
% --------------------------
% CASE 2: works as expected
% --------------------------
\begin{table}
\caption{A test table.}
\label{table.test}
\centering
\begin{tabular}{S r r}
Col1 & Col2 & Col3 \
\hline
Num1 & Num2 & Num3 \
\end{tabular}
\end{table}
% --------------------------
% CASE 3: works as expected
% --------------------------
\begin{table}
\caption{A test table.}
\label{table.test}
\centering
\begin{tabular}{r r r}
\hline
Num1 & Num2 & Num3 \
\end{tabular}
\end{table}
% --------------------------
% CASE 4: works as expected
% --------------------------
\begin{table}
\caption{A test table.}
\label{table.test}
\centering
\begin{tabular}{S r r}
\hline
Num1 & Num2 & Num3 \
\end{tabular}
\end{table}
% --------------------------
% CASE 5: works as expected
% --------------------------
\input{tables/table_test1a.tex}
% --------------------------
% CASE 6: works as expected
% --------------------------
\input{tables/table_test1b.tex}
% --------------------------
% CASE 7: works as expected
% --------------------------
\input{tables/table_test1c.tex}
% --------------------------
% CASE 8: works as expected
% --------------------------
\input{tables/table_test1d.tex}
% --------------------------
% CASE 9: works as expected
% --------------------------
\begin{table}
\caption{A test table.}
\label{table.test}
\centering
\begin{tabular}{r r r}
\input{tables/table_test2a.tex}
\end{tabular}
\end{table}
% --------------------------
% CASE 10: does NOT work as expected:
% No horizontal line (\hline), but word "height" appears right before "Num1".
% The error types listed below appear (in total 14 errors).
%
% Use of @@array doesn't match its definition.
% Runaway argument?
% Misplaced alignment tab character &.
% Misplaced \cr.
% Misplaced \noalign.
% You can't use `\hrule' here except with leaders.
% Missing number, treated as zero.
% Illegal unit of measure (pt inserted).
% Missing control sequence inserted.
% Package siunitx Error: Invalid number '='.
% Missing } inserted.
% --------------------------
\begin{table}
\caption{A test table.}
\label{table.test}
\centering
\begin{tabular}{S r r}
\input{tables/table_test2a.tex}
\end{tabular}
\end{table}
% --------------------------
% CASE 11: does NOT work as expected:
% No horizontal line (\hline), but word "height" appears right before "Num1".
% The error types listed below appear (in total 4 errors).
%
% Misplaced \noalign.
% You can't use `\hrule' here except with leaders.
% Missing number, treated as zero.
% Illegal unit of measure (pt inserted).
% --------------------------
% \begin{table}
% \caption{A test table.}
% \label{table.test}
% \centering
% \begin{tabular}{r r r}
% \input{tables/table_test2b.tex}
% \end{tabular}
% \end{table}
% --------------------------
% CASE 12: does NOT work as expected:
% No horizontal line (\hline), but word "height" appears right before "Num1".
% The error types listed below appear (in total 12 errors).
%
% Use of @@array doesn't match its definition.
% Runaway argument?
% Misplaced \noalign.
% You can't use `\hrule' here except with leaders.
% Missing number, treated as zero.
% Illegal unit of measure (pt inserted).
% Misplaced alignment tab character &.
% Misplaced \cr.
% Missing control sequence inserted.
% Package siunitx Error: Invalid number '='.
% Missing } inserted.
% --------------------------
% \begin{table}
% \caption{A test table.}
% \label{table.test}
% \centering
% \begin{tabular}{S r r}
% \input{tables/table_test2b.tex}
% \end{tabular}
% \end{table}
\end{document}
% TABLE TEX FILES:
% table_1a.tex:
\begin{table}
\caption{A test table}
\label{table.test}
\centering
\begin{tabular}{r r r}
Col1 & Col2 & Col3 \
\hline
Num1 & Num2 & Num3 \
\end{tabular}
\end{table}
% table_1b.tex:
\begin{table}
\caption{A test table}
\label{table.test}
\centering
\begin{tabular}{S r r}
Col1 & Col2 & Col3 \
\hline
Num1 & Num2 & Num3 \
\end{tabular}
\end{table}
% table_1c.tex:
\begin{table}
\caption{A test table}
\label{table.test}
\centering
\begin{tabular}{r r r}
\hline
Num1 & Num2 & Num3 \
\end{tabular}
\end{table}
% table_1d.tex:
\begin{table}
\caption{A test table}
\label{table.test}
\centering
\begin{tabular}{S r r}
\hline
Num1 & Num2 & Num3 \
\end{tabular}
\end{table}
% table_2a.tex:
Col1 & Col2 & Col3 \
\hline
Num1 & Num2 & Num3 \
% table_2b.tex:
\hline
Num1 & Num2 & Num3 \
inputand not be able to compile it locally is not good idea (you not profit anything) . You may consider to usestandalone. for developing table and than insert it by use of its mechanism for this. – Zarko Dec 10 '21 at 12:12