I want to read/input the body of a table from a separate file. I have mostly solved this problem using this question.
But when I try to read a table with multicolumn from a separate file, it failed.
The following is an example.
%main.tex
\documentclass{article}
\usepackage{filecontents,catchfile}
\begin{filecontents*}{table.tex}
%table.tex
\hline
A & B & C & D \\ \hline
E & \multicolumn{3}{l|}{This is generated by excel2latex macro. I want to let it auto wrap according to the width of (B+C+D) } \\ \hline
\end{filecontents*}
\begin{document}
Table test.
\section{Insert a full table}
\begin{tabular}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
\hline
A & B & C & D \\ \hline
E & \multicolumn{3}{p{6cm}|}{This is generated by excel2latex macro. I want to let it auto wrap according to the width of (B+C+D) } \\ \hline
\end{tabular}%
\section{ Input the body of table from a separate file}
\CatchFileDef{\mytable}{table.tex}{}% table.tex > \mytable
\begin{tabular}{|p{2cm}|p{2cm}|p{2cm}|p{2cm}|}
\mytable
\end{tabular}%
\end{document}
I just don't want to manually define the width in the body of table because it is generated automatically.
Best Answer
Assume that columns B, C, and D are of the
p
type with usable widths given by the length parameters\colB
,\colC
, and\colD
. In your example code, these lengths are equal to2cm
each. The total widths of columns B, C, and D are, however, not equal to2cm
. Instead, each column's total width equals2cm+2\tabcolsep
, where\tabcolsep
is the parameter that governs the amount of whitespace inserted to the left and right of a column. (In the standard LaTeX document classes, the default value of this parameter is6pt
.)The combined width of columns B, C, and D -- not counting the widths of the vertical bars to the left of B and to the right of D -- is
(I'm assuming, implicitly, that the
array
package is being loaded. If that's not the case, then one should omit the2\arrayrulewidth
term.) You can probably guess that2\arrayrulewidth
represents the combined widths of the two interior vertical bars -- those separating B from C and C from D. The default value of this parameter is0.4pt
in the standard document classes.The usable width of the three columns is a bit smaller, since we mustn't impose on
\tabcolsep
at the left-hand edge of B and at the right-hand edge of D. It is thus given byOne can create a length variable called, say,
\combinedlength
and set its value to the expression above. The contents of the external filetable.tex
would thus be given byThe full MWE: