R markdown running tex file error regarding toprule

I have the following chunk of codes in markdown file

---
title: "Ag Productivity"
author: "Abdullah Mamun"
date: '2022-06-23'
output: 
 pdf_document:
  includes:
   in_header: "table1.tex"
---

and in .tex file I have this

\begin{document}
\begin{table}
\centering
\begin{tabular}[t]{lccc}
  \toprule
  & Model 1 & Model 2 & Model 3\\
\midrule
lag(YGrowth, 1) & \num{1.000}(\num{0.000})*** & \num{-0.347}(\num{0.030})*** & \num{-0.436}(\num{0.068})***\\
lag(Inputs, 1) & \num{0.000}(\num{0.000}) & \num{-0.100}(\num{0.127}) & \num{2.321}(\num{1.078})*\\
lag(GDPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.270}(\num{0.069})*** & \num{0.341}(\num{0.531})\\
lag(lnPDENS, 1) & \num{0.000}(\num{0.000}) & \num{-0.124}(\num{0.031})*** & \num{0.158}(\num{0.076})*\\
lag(GRRAT, 1) & \num{0.000}(\num{0.000}) & \num{-0.280}(\num{0.095})** & \num{0.858}(\num{0.378})*\\
lag(EMPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.317}(\num{0.138})* & \num{0.708}(\num{1.571})\\
\midrule
Num.Obs. & \num{1070} & \num{1025} & \\
R2 & \num{1.000} & \num{0.141} & \\
R2 Adj. & \num{1.000} & \num{0.076} & \\
AIC & \num{-79950.8} & \num{-2904.1} & \\
BIC & \num{-79781.6} & \num{-2911.2} & \\
RMSE & \num{0.00} & \num{0.06} & \\
\bottomrule
\end{tabular}
\end{table}

I get this error message:

! Undefined control sequence. l.106 \toprule

Appreciate your help.

>Solution :

  • the table does not belong in the header. You should move it to the body of the document. Even though you try to force the body to start early with \begin{document}, this won’t work because rmarkdown will include preamble-only stuff after this

  • if you want to use special macros like \toprule or \num you must load the package which provide them

  • your table is missing a floating specifier that tells latex where the table can be placed, I suggest [htbp

rmarkdown file:

---
title: "Ag Productivity"
author: "Abdullah Mamun"
date: '2022-06-23'
output: 
 pdf_document:
   keep_tex: true
header-includes:
  - \usepackage{booktabs}
  - \usepackage{siunitx}
---

\include{table1}

table1.tex:

\begin{table}[htbp]
\centering
\begin{tabular}[t]{lccc}
  \toprule
  & Model 1 & Model 2 & Model 3\\
\midrule
lag(YGrowth, 1) & \num{1.000}(\num{0.000})*** & \num{-0.347}(\num{0.030})*** & \num{-0.436}(\num{0.068})***\\
lag(Inputs, 1) & \num{0.000}(\num{0.000}) & \num{-0.100}(\num{0.127}) & \num{2.321}(\num{1.078})*\\
lag(GDPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.270}(\num{0.069})*** & \num{0.341}(\num{0.531})\\
lag(lnPDENS, 1) & \num{0.000}(\num{0.000}) & \num{-0.124}(\num{0.031})*** & \num{0.158}(\num{0.076})*\\
lag(GRRAT, 1) & \num{0.000}(\num{0.000}) & \num{-0.280}(\num{0.095})** & \num{0.858}(\num{0.378})*\\
lag(EMPGR, 1) & \num{0.000}(\num{0.000})* & \num{0.317}(\num{0.138})* & \num{0.708}(\num{1.571})\\
\midrule
Num.Obs. & \num{1070} & \num{1025} & \\
R2 & \num{1.000} & \num{0.141} & \\
R2 Adj. & \num{1.000} & \num{0.076} & \\
AIC & \num{-79950.8} & \num{-2904.1} & \\
BIC & \num{-79781.6} & \num{-2911.2} & \\
RMSE & \num{0.00} & \num{0.06} & \\
\bottomrule
\end{tabular}
\end{table}

Leave a Reply