RMarkdown Knitr Won't Render LaTeX tabular table in pdf - latex

I'm trying to format a table using latex notation in RMarkdown. My code is the following:
\begin{table}[h!]
\center
\begin{tabular}{l|c|c}
\hline
Model & Coefficient & \\
Predictors & Value $ p-value\\
\hline
Intercept & -1.716 & 0.022\\
Retire & 0.197 & 0.020\\
Age & -0.015 & 0.020\\
Health Status & 0.312 & <0.001\\
Income & 0.002 & 0.002\\
Years of Education & 0.114 & <0.001\\
Married & 0.579 & <0.001\\
Hispanic & -0.810 & <0.001\\
\hline
\end{tabular}
\end{table}
For some reason though, when I go to knit the document together, I get the following error:
You may need to add $ $ around a certain inline R expression `r ` in draft.Rmd (see the above hint). See https://github.com/rstudio/rmarkdown/issues/385 for more info.
Error: LaTeX failed to compile draft.tex. See https://yihui.org/tinytex/r/#debugging for debugging tips. See draft.log for more info.
Execution halted
When I remove this chunk, it knits together fine (including other tables I have put together using this method) and I can't figure out what is going on with this code. Any help would be great!

Two problems:
you have one $ instead of a &
it is \centering or \begin{center}...\end{center}, but not \center (this kind of works only by accident)
Furthermore, I suggest you take a look at the siunitx package to get correct minus/< signs and proper alignment of the cells. Also the booktabs package might be worth a look to get a good looking table.
\documentclass{article}
\begin{document}
\begin{table}[h!]
\centering
\begin{tabular}{l|c|c}
\hline
Model & Coefficient & \\
Predictors & Value & p-value\\
\hline
Intercept & -1.716 & 0.022\\
Retire & 0.197 & 0.020\\
Age & -0.015 & 0.020\\
Health Status & 0.312 & <0.001\\
Income & 0.002 & 0.002\\
Years of Education & 0.114 & <0.001\\
Married & 0.579 & <0.001\\
Hispanic & -0.810 & <0.001\\
\hline
\end{tabular}
\end{table}
\end{document}

Related

Stata - Esttab to Latex Decimal Alignment

I am trying to output a LaTex file using Stata's esttab command. I cannot get the decimals to align, nor can I get the nice formatting from the "booktabs" option to work. I also would like to increase the width of the columns. I have been tinkering with this for hours and have had no luck. Below is my Stata code and the LaTex code that I am running through Overleaf. Any help would be greatly appreciated!
Here is my Stata code:
#delimit ;
esttab r1 r2 r3 r4 using "C:\Users\user\Dropbox\Private
Code\Code\STATA\latex\table1.tex", replace
b(3) nomtitle label star(* 0.10 ** 0.05 *** 0.01)
booktabs alignment(D{.}{.}{-1})
title(Placeholder)
s(N DAY FFE FIRM_QUARTER r2_a,
fmt(%9.0fc 0 0 0 3)
label("Observations" "Day FE" "Firm FE" "Firm-Quarter FE" "Adj. R-Sq{\super 2}"))
compress nocons;
Here is the LaTex code it spits out:
\begin{table}[htbp]\centering
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\caption{Placeholder}
\begin{tabular}{l*{4}{D{.}{.}{-1}}}
\toprule
&\multicolumn{1}{c}{(1)} &\multicolumn{1}{c}{(2)} &\multicolumn{1}
{c}{(3)} &\multicolumn{1}{c}{(4)} \\
\midrule
VAR1 & 0.007\sym{***}& 0.007\sym{***}& 0.006\sym{***}& 0.007\sym{***}\\
& (8.55) & (7.82) & (7.67) & (8.54) \\
\addlinespace
VAR2 & 0.132\sym{***}& 0.131\sym{***}& 0.131\sym{***}& 0.132\sym{***}\\
& (20.35) & (20.15) & (20.15) & (20.35) \\
\midrule
Observations &1,315,077 &1,315,077 &1,315,077 &1,315,077
\\
Day FE & No & Yes & Yes & No \\
Firm FE & Yes & No & Yes & No \\
Firm-Quarter FE & No & No & No & Yes \\
Adj. R-Sq{\super 2}& 0.045 & 0.027 & 0.046 & 0.044
\\
\bottomrule
\multicolumn{5}{l}{\footnotesize \textit{t} statistics in parentheses}\\
\multicolumn{5}{l}{\footnotesize \sym{*} \(p<0.10\), \sym{**} \(p<0.05\), \sym{***} \(
p<0.01\)}\\
\end{tabular}
\end{table}
Since you haven't provided the full code, I can't see what would your output be. When I add \documentclass{article} with missing packages in a preamble, I get very wide table which does not fit a page. Also, I don't know whether the code is automatically generated or created/altered by you.
Assuming the latter, I would do the following. Since you add footnotes in the table, I would enclose tabular within threeparttable, which splits the area for table into three parts: top for captions, middle for a table definition e.g. tabular, and bottom part for footnotes. It also adds a convenient macro \tnote{}.
Secondly, dcolumn causes big numbers to be pushed way too much to the left. You should let dcolumn suppress their alignment. I personally don't know how, although I assume there is a way.
I prefer siunitx for formatting numbers in tables. However siunitx needs a bit more work. It automatically parses numbers in cells and any non-number content generates errors. Therefore, non-numbers need to be enclosed within {...} which instructs siunitx to leave the content intact. It also forces numbers not to be formatted, aligned etc., which is what I did for the big numbers you have in the table. There's also option to format thousands and millions with any separator of your choice. For instance you could use one of the macros: \,, :, or \; for thin, mid or thick space, respectively instead of ,.
Below is my suggestion for your table based on siunitx and threeparttable, so you can compare it with your approach.
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{threeparttable}
\newcommand\super[1]{\textsuperscript{#1}}
\sisetup{
group-separator={,},
table-format=1.3,
table-align-text-after = false,
}
\begin{document}
\begin{table}[htbp]
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\centering
\begin{threeparttable}
\caption{Placeholder}
\begin{tabular}{l *{4}{S}}
\toprule
& \multicolumn{1}{c}{(1)}
& \multicolumn{1}{c}{(2)}
& \multicolumn{1}{c}{(3)}
& \multicolumn{1}{c}{(4)} \\
\midrule
{VAR1} & 0.007\tnote{***} & 0.007\tnote{***} & 0.006\tnote{***} & 0.007\tnote{***} \\
& {(8.55)} & {(7.82)} & {(7.67)} & {(8.54)} \\
\addlinespace
VAR2 & 0.132\tnote{***} & 0.131\tnote{***} & 0.131\tnote{***} & 0.132\tnote{***} \\
& {(20.35)} & {(20.15)} & {(20.15)} & {(20.35)} \\
\midrule
Observations
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}}
& \multicolumn{1}{c}{\num{1315077}} \\
Day FE & {No} & {Yes} & {Yes} & {No} \\
Firm FE & {Yes} & {No} & {Yes} & {No} \\
Firm-Quarter FE & {No} & {No} & {No} & {Yes} \\
Adj. R-Sq{\super2} & 0.045 & 0.027 & 0.046 & 0.044 \\
\bottomrule
\end{tabular}
\par\(t\) statistics in parentheses
\par\tnote{*} \(p < 0.10\),\quad\tnote{**} \(p < 0.05\),\quad\tnote{***} \(p < 0.01\)
\end{threeparttable}
\end{table}
\end{document}
EDIT. I also enclosed statistics in{...} because siunitx does not recognised brackets as part of a number.

How to join the vertical lines while adding newline to table cells

My code is as follows :
\begin{table*}[!htbp]
\centering
\begin{tabular}{ |c|c|c|c|c|c|c| }
\hline
Models & Architecture & Val & Test Acc (FF) & Test Acc (BD) & Test Acc\\ \hline
VisualBert & Single Cross-Modal Transformer & 51.0\% & 50.8\% & 51.1\% & 50.5\% \\\\ \hline
VilBert & One Single Modal Transformer \\ & (Language) \\ & + one cross-modal transformer \\ & (with restricted attention pattern) & 51.2\% & 50.9\% & 51.2\% & 52.6\% \\\\ \hline
LXMERT & Two Single Modal Transformer \\ & (Vision and Language) \\ & + one cross-modal transformer \\ & (with restricted attention pattern) & 53.8\% & 52.2\% & 51.0\%& 52.9\% \\\\ \hline
Unicoder-VL & Single Cross-Modal Transformer & 53.8\% & 52.2\% & 51.0\%& 52.9\% \\\\ \hline
CLIP & Base Model: ResNet50 + \\ & masked self-attention transformer \\ & or \\ & ViT + text transformer & 53.8\% & 52.2\% & 51.0\%& 52.9\% \\\\ \hline
SLIP & ViT/B-16 and L-16 + \\ & text transformer from CLIP & 53.8\% & 52.2\% & 51.0\%& 52.9\% \\\\\hline
\end{tabular}
\caption{Model performance on Bongard LOGO on a reduced resolution. The test accuracy is reported on different dataset splits, including free-form shape test set (FF), basic shape test set (BA), combinatorial abstract shape test set (CM), and novel abstract shape test set (NV)}
\label{tab:baseline}
\end{table*}
But this is my output:
How do I complete the vertical lines and print the first column text as bottom aligned?
Please help! Thank you!
Preface:
Don't use vertical lines in tables.
(have a look at https://wiert.files.wordpress.com/2014/04/zy8dkpa.gif for some tips on professional looking tables)
\\ won't add line breaks to your cell, it will add completely new rows to your table and you have to make sure that each row of your table has the same number of cells. Don't just prematurely finish the row with \\, instead add empty cells if necessary:
\documentclass{article}
\begin{document}
\begin{table*}[!htbp]
\centering
\begin{tabular}{ |c|c|c|c|c|c|c| }
\hline
Models & Architecture & Val & Test Acc (FF) & Test Acc (BD) & Test Acc\\\hline
VisualBert & Single Cross-Modal Transformer & 51.0\% & 50.8\% & 51.1\% & 50.5\% \\ \hline
& One Single Modal Transformer &&&& \\ & (Language) &&&& \\ & + one cross-modal transformer &&&& \\ VilBert & (with restricted attention pattern) & 51.2\% & 50.9\% & 51.2\% & 52.6\% \\ \hline
\end{tabular}
\caption{Model performance on Bongard LOGO on a reduced resolution. The test accuracy is reported on different dataset splits, including free-form shape test set (FF), basic shape test set (BA), combinatorial abstract shape test set (CM), and novel abstract shape test set (NV)}
\label{tab:baseline}
\end{table*}
\end{document}
That being said, manually adding line breaks seems unnecessarily tedious. I suggest to let latex do the line breaks for you. This is particularly easy with the tabularray package:
\documentclass{article}
\usepackage{tabularray}
\usepackage[hmargin=2cm]{geometry}
\begin{document}
\begin{table*}[!htbp]
\centering
\begin{tblr}{ |c|X[valign=b,halign=c]|c|c|c|c|c| }
\hline
Models & Architecture & Val & Test Acc (FF) & Test Acc (BD) & Test Acc\\\hline
VisualBert & Single Cross-Modal Transformer & 51.0\% & 50.8\% & 51.1\% & 50.5\% \\ \hline
VilBert & One Single Modal Transformer (Language) + one cross-modal transformer (with restricted attention pattern) & 51.2\% & 50.9\% & 51.2\% & 52.6\% \\ \hline
\end{tblr}
\caption{Model performance on Bongard LOGO on a reduced resolution. The test accuracy is reported on different dataset splits, including free-form shape test set (FF), basic shape test set (BA), combinatorial abstract shape test set (CM), and novel abstract shape test set (NV)}
\label{tab:baseline}
\end{table*}
\end{document}

Making self-adjusting table in latex

I am trying to create self-adjusting table in latex. THis is going to be a big table so I need text to go to the next line if not enough space but I am not able to do this. I need to have 5 columns.
Below is the image of what I have managed but "description" is now going into the next cell. I'd appreciate if someone could tell me where I am going wrong. I have spent so much time on this
Code I am using,
\begin{table}[h]
\begin{tabularx}{\textwidth}{|l|X|X|X|X|}
\hline
Author & Clustering Technique & Dataset & Description & Industry\\
\hline
\citeauthor{shen2009study} & Quantiles & Customer and Transaction Department store & RFM; Customer Lifetime Value; Target Marketing; Data Mining
& Retail Store \\
\hline
\citeauthor{aggelis2005customer} & Quantiles & E-Banking Dataset & Data Mining; e-banking; RFM analysis & Banking\\
\hline
\end{tabularx}
\end{table}
EDIT: I also need it either to fit on one page or be able to continue in the next page.
Very belated answer...
To adjust the width of the column I have used p{.1\textwidth}, this is paragraph alignment with width of the column equal to 0.1 times the width of the text body (or other fraction of it).
For the table to continue in next page(s), if necessary, I have used the excellent environment longtable from the package with the same name.
\documentclass{article}
\usepackage{longtable,natbib}
\begin{document}
\begin{longtable}{|p{.1\textwidth}|p{.15\textwidth}|p{.25\textwidth}|p{.3\textwidth}|p{.15\textwidth}|}
%\caption[Short caption]{Full caption}\label{longt}\\
\hline
% content of first header
Author & Clustering Technique & Dataset & Description & Industry\\
\hline
\endfirsthead
% content of following headers if the table continues in other page(s)
Author & Clustering Technique & Dataset & Description & Industry\\
\hline
\endhead
% content of all footers
\hline
\endfoot
% content of last footer
\hline
\endlastfoot
\citeauthor{shen2009study} & Quantiles & Customer and Transaction Department store & RFM; Customer Lifetime Value; Target Marketing; Data Mining & Retail Store\\
\hline
\citeauthor{aggelis2005customer} & Quantiles & E-Banking Dataset & Data Mining; e-banking; RFM analysis & Banking\\
\end{longtable}
\end{document}
The output of the code above:
To avoid intruding into the next cell, you can help latex by suggesting possible hyphenation points for description
\documentclass{book}
\usepackage{tabularx}
\begin{document}
\begin{table}[h]
\begin{tabularx}{\textwidth}{|l|X|X|X|X|}
\hline
Author & Clustering Technique & Dataset & De\-scrip\-tion & Industry\\
\hline
xxx & Quantiles & Customer and Transaction Department store & RFM; Customer Lifetime Value; Target Marketing; Data Mining
& Retail Store \\
\hline
xxxxxx xxx xxxx xxxxxx xxx & Quantiles & E-Banking Dataset & Data Mining; e-banking; RFM analysis & Banking\\
\hline
\end{tabularx}
\end{table}
\end{document}
(if you need the table to allow page breaks, use the xltabular package instead)

I want to add two tables one after one in a section using latex. But, my second table is going to another section. What to do?

In my 2nd section I want to add two table. One table is in the right position, but the second table went to the next section. it was supposed to be in the previous section. How can I bring the second table in the right section?
Here is the code I used:
% table 3 begins
\begin{table}[h]
\centering
\caption{}
\label{my-label}
\begin{tabular}{cccc}
\hline
\textbf{Properties} & \textbf{Model 1} & \textbf{Model 2} & \textbf{Model 3} \\ \hline
\begin{tabular}[c]{#{}c#{}}Training\\ Window width\end{tabular} & 2 & 2 & 1 \\
\begin{tabular}[c]{#{}c#{}}Training\\ window step\end{tabular} & 1 & 1 & 1 \\
\begin{tabular}[c]{#{}c#{}}Test\\ window width\end{tabular} & 2 & 2 & 1 \\
Horizon & 5 & 10 & 15 \\
\begin{tabular}[c]{#{}c#{}}Cumulative\\ training\end{tabular} & No & No & No \\ \hline
\end{tabular}
\end{table}
\\
% table 4 begins
\begin{table}[!t]
\centering
\caption{}
\label{my-label}
\begin{tabular}{ccccc}
\hline
\textbf{Date} & \textbf{\begin{tabular}[c]{#{}c#{}}Actual Price\\ (USD)\end{tabular}} & \multicolumn{3}{c}{\textbf{Predicted Price}} \\ \hline
\textbf{} & \textbf{} & \textbf{Model 1} & \textbf{Model 1} & \textbf{Model 1} \\
13-01-15 & 89.30 & 89.44 & 87.90 & 91.71 \\
07-11-14 & 78.76 & 77.63 & 77.72 & 78.08 \\
29-07-14 & 75.44 & 75.00 & 76.43 & 77.59 \\
04-06-14 & 77.12 & 76.26 & 76.96 & 77.53 \\ \hline
\end{tabular}
\end{table}
In addition to Norman Gray:s answer, I might also add that there are several different methods to force your floats (tables/figures and so on) to stay within "their" sections.
One method would be to use the placeins package, as described in its documentation. From the documentation:
Placeins.sty keeps floats ‘in their place’, preventing them from
floating past a “\FloatBarrier” command into another section. To use
it, declare “\usepackage{placeins}” and insert “\FloatBarrier” at
places that floats should not move past, perhaps at every
“\section”.
...
If you specify “\usepackage[section]{placeins}”, then the “\section” command will be redefined with “\FloatBarrier” inserted at the beginning.
So in your case, try adding
\usepackage[section]{placeins}
in your document header.
It's not so much that LaTeX put the second table in the next section, but it put it on the next page that had room. LaTeX has a slightly intricate algorithm for working out how best to relocate tables and figures, but unfortunately its decisions can be rather ... unpredictable.
One solution is to accept that this is OK, and perhaps give the reader a clue by saying see table~\ref{my-label} on page~\pageref{my-label}, which helps the reader find the table. The varioref package can do this in a smarter way. I think, myself, that this is the best solution.
Another is to adjust the maximum fraction of a page that LaTeX will permit to have tables. A command like \renewcommand{\floatpagefraction}{0.75} will permit up to three quarters of the page to be floats – so only a quarter of the page text – before spilling on to a new page. This can quickly look ugly if the fraction is too large, however.
This is a very common problem, and searching online for ‘controlling latex floats’, or similar, should uncover lots of help. There should also be help on this topic elsewhere on Stackoverflow, or on the companion TeX site.

Length parbox LaTeX

I am trying to build a table on LaTeX with parbox. I have found how to do with parbox. However, the length between rows makes the text so pack. Do you know how to increase the length?
\documentclass{article}
\usepackage{multirow}
\begin{document}
{\raggedright
\vspace{3pt} \noindent
\begin{tabular}{|p{108pt}|p{223pt}|p{52pt}|}
\hline
\parbox{108pt}{\raggedright
Attribute
} & \parbox{223pt}{\raggedright
Description
} & \parbox{52pt}{\raggedright
Characteristic
} \\
\hline
\parbox{108pt}{\raggedright
Language
} & \parbox{223pt}{\raggedright
Programming language of the source code.
} & \parbox{52pt}{\raggedright \multirow{5}{*}{
Project
}} \\
\cline{1-3}
\parbox{108pt}{\raggedright
Team\_size
} & \parbox[70em]{223pt}{\raggedright
Number of active core team members during the last 3 months prior to creation.
} & \\
\cline{1-3}
\parbox[15em]{108pt}{\raggedright
Perc\_external\_contribs
} & \parbox{223pt}{\raggedright
Ratio of commits from external contributors over core team members in the last 3 months prior to creation of pull request.
} & \\
\cline{1-2}
\hline
\end{tabular}
\vspace{2pt}
\end{document}
Thank you for your help.
The tabular columns are already set using a paragraph specification, so there's no need to set each cell using a \parbox (of similar width) as well.
You might be interested in tabularx as it allows for a flexibly-width X-column:
\documentclass{article}
\usepackage{tabularx}% Loads the array package
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|>{\raggedright}p{108pt}|>{\raggedright}X|>{\raggedright\arraybackslash}p{62pt}|}
\hline
Attribute & Description & Characteristic \\
\hline
Language & Programming language of the source code. & Project \\
\hline
Team\_size & Number of active core team members during the last 3 months prior to creation. & \\
\hline
Perc\_external\_contribs & Ratio of commits from external contributors over core team
members in the last 3 months prior to creation of pull request. & \\
\hline
\end{tabularx}
\end{document}
If you wish to adjust the padding further, consider reading up on Column and row padding in tables.

Resources