Tables – How to Add a Caption to an External Table in LaTeX

captionsinputtables

I have an external table that is is produced by a statistics package Stata like this:

sysuse auto
estimates clear
eststo, title("OLS"): reg price mpg
esttab using "~/Desktop/table.tex", replace title(My OLS Model)

The file table.tex looks like this:

\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{My OLS Model}
\begin{tabular}{l*{1}{c}}
\hline\hline
            &\multicolumn{1}{c}{(1)}\\
            &\multicolumn{1}{c}{price}\\
\hline
mpg         &      -238.9\sym{***}\\
            &     (-4.50)         \\
[1em]
\_cons      &     11253.1\sym{***}\\
            &      (9.61)         \\
\hline
\(N\)       &          74         \\
\hline\hline
\multicolumn{2}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{2}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
\end{table}

I input it into a LaTeX main document like this:

\documentclass{article}
\usepackage{booktabs}
\begin{document}
\input{table.tex}
\end{document}

I have two questions:

  1. Is it possible to add a note at the bottom of this table in the main document that could be edited in the main file (rather than modifying table.tex file)?
  2. Is it possible to overwrite the caption from the main document?

Best Answer

You're not using a \label inside the table to reference it, nor are you using the optional argument to \caption for a different LoT entry. So, we can just redefine \caption to do whatever you want, including supplying a new caption.

Additionally, we use etoolbox to patch \end{tabular} for the insertion of arbitrary content via \AfterEndEnvironment{tabular}:

enter image description here

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{table.tex}
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{My OLS Model}
\begin{tabular}{l*{1}{c}}
\hline\hline
            &\multicolumn{1}{c}{(1)}\\
            &\multicolumn{1}{c}{price}\\
\hline
mpg         &      -238.9\sym{***}\\
            &     (-4.50)         \\
[1em]
\_cons      &     11253.1\sym{***}\\
            &      (9.61)         \\
\hline
\(N\)       &          74         \\
\hline\hline
\multicolumn{2}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{2}{l}{\footnotesize \sym{*} \(p<0.05\), \sym{**} \(p<0.01\), \sym{***} \(p<0.001\)}\\
\end{tabular}
\end{table}
\end{filecontents*}

\usepackage{booktabs,etoolbox}

\AfterEndEnvironment{tabular}{\tabularendstuff}
\newcommand{\tabularendstuff}{}

\begin{document}

% Update the \caption
\begingroup
\let\oldcaption\caption
\renewcommand{\caption}[1]{\oldcaption{A new caption}}
\renewcommand{\tabularendstuff}{\par {\footnotesize This is some arbitrary stuff added after \texttt{tabular}.}}
\input{table.tex}
\endgroup

% Original table
\input{table.tex}

\end{document}

All changes are grouped to limit their scope to the current table/tabular.