Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read body of table from a separate file?

Tags:

input

latex

I want to read/input the body of a table from a separate file. But it failed. How can I do it. The below is an example

Main tex file: Main.tex

%main.tex
\documentclass{article}
\begin{document}

Table test.

1. Insert a full table

\begin{tabular}{|c|c|}
\hline
a & b \\
\hline
c & d \\
\hline
\end{tabular}

2. Input the body of table from a seperate file

\begin{tabular}{|c|c|} 
\input{table}
\end{tabular}

\end{document}

Table body file: table.tex

%table.tex
\hline
a & b \\
\hline
c & d \\
\hline 
like image 292
Jilong Yin Avatar asked Sep 19 '25 12:09

Jilong Yin


1 Answers

Capture the table contents in table.tex in a macro before processing it inside a tabular. For this, use the catchfile package:

enter image description here

%main.tex
\documentclass{article}
\usepackage{filecontents,catchfile}
\begin{filecontents*}{table.tex}
%table.tex
\hline
a & b \\
\hline
c & d \\
\hline 
\end{filecontents*}

\begin{document}

Table test.

1. Insert a full table

\begin{tabular}{|c|c|}
  \hline
  a & b \\
  \hline
  c & d \\
  \hline
\end{tabular}

2. Input the body of table from a seperate file

\CatchFileDef{\mytable}{table.tex}{}% table.tex > \mytable
\begin{tabular}{|c|c|} 
  \mytable
\end{tabular}

\end{document}
like image 190
Werner Avatar answered Sep 22 '25 06:09

Werner