Fit a table within \textwidth in Latex - latex

I have a table that is wider than the \textwidth of my document. (I don't want to change the \textwidth.)
I want to force the table to be fitted in \textwidth by wrapping the last (rightest column) column's text.
This is my code for my table:
\begin{table*}[!t]
\caption{Extracted Features used in Meta-Learning} \label{sample-features}
\label{tbl:features}
\begin{center}
\begin{tabular*}{0.95\textwidth,center}{|l||l|}
{\textbf{FEATURE}} &{\textbf{DESCRIPTION}} \\
\hline \\
n.examples & Number of examples \\
n.attrs & Number of attributes \\
prop.symbolic.attrs & Proportion of symbolic attributes \\
prop.missing.values & Proportion of missing values \\
class.entropy & Class entropy \\
avg.mutual.information & Average mutual information \\
prop.h.outlier & Proportion of continuous attributes with outliers \\
avg.attr.entropy & Average attribute entropy \\
avg.symb.pair.mutual.infor & Average mutual information between pairs of symbolic attributes \\
avg.abs.attr.correlation & Average absolute correlation between continuous attributes \\
avg.skewness & Mean skewness of attributes \\
avg.abs.skewness & Mean absolute skewness of attributes \\
avg.kurtosis & Mean kurtosis of attributes \\
canonical.correlation.best.linear.combination & Canonical correlation of the best linear combination of attributes to distinguish between classes \\
relative.prop.best.linear.combination & Proportion of the total discrimination power explained by the best linear combination \\
\end{tabular*}
\end{center}
\end{table*}
The result of this code is the following document:
The part in the right side of the red line (added by myself) should be wrapped.
Any help?

Here is an answer. I hope it can help you:
Using package tabularx as follows:
\usepackage{tabularx,booktabs}
.
.
.
\begin{table*}[!t]
\caption{Caption}
\label{tbl:tbl}
\centering
\begin{tabularx}{\textwidth}{X X}
\toprule
Item 1 & Item 2 \\
\midrule
Item 1 description & Item 2 description \\
.
.
.
\bottomrule
\end{tabularx}
\end{table*}
In my case the solution is as follows:
\begin{table*}[!t]
\caption{Extracted Features used in Meta-Learning} \label{sample-features}
\label{tbl:features}
\centering
\begin{tabularx}{0.95\textwidth}{|X||X|}
\toprule
{\textbf{FEATURE}} &{\textbf{DESCRIPTION}} \\
\midrule
n.examples & Number of examples \\
n.attrs & Number of attributes \\
prop.symbolic.attrs & Proportion of symbolic attributes \\
prop.missing.values & Proportion of missing values \\
class.entropy & Class entropy \\
avg.mutual.information & Average mutual information \\
prop.h.outlier & Proportion of continuous attributes with outliers \\
avg.attr.entropy & Average attribute entropy \\
avg.symb.pair.mutual.infor & Average mutual information between pairs of symbolic attributes \\
avg.abs.attr.correlation & Average absolute correlation between continuous attributes \\
avg.skewness & Mean skewness of attributes \\
avg.abs.skewness & Mean absolute skewness of attributes \\
avg.kurtosis & Mean kurtosis of attributes \\
canonical.correlation.best.linear.combination & Canonical correlation of the best linear combination of attributes to distinguish between classes \\
relative.prop.best.linear.combination & Proportion of the total discrimination power explained by the best linear combination \\
\bottomrule
\end{tabularx}
\end{table*}

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 insert a picture into a table, spanning multiple rows in latex?

I'm trying to create a table in Latex for a risk assessment for a project. Currently I have the table itself, however I want to insert a picture in the empty white space, spanning multiple rows. When we insert the picture at the moment, it however wont span the rows, and will leave a large white space beneath the illustraion. How can I change it so the illustration does not do so?
The code for the table is as the following:
\usepackage{multirow}
\usepackage[table,xcdraw]{xcolor}
\usepackage{graphicx}
\begin{table}[]
\centering
\begin{tabular}{|ccccc|}
\hline
\rowcolor[HTML]{FFCC67}
\multicolumn{3}{|l|}{\cellcolor[HTML]{FFCC67}{\color[HTML]{000000} Hazard identification}} & \multicolumn{1}{r|}{\cellcolor[HTML]{FFCC67}{\color[HTML]{000000} Hazard nr.}} & {\color[HTML]{000000} 1} \\ \hline
\multicolumn{1}{|l|}{Title} & \multicolumn{2}{l|}{Part picking} & \multicolumn{2}{l|}{} \\ \cline{1-3}
\multicolumn{1}{|l}|}{Taget} & \multicolumn{2}{l|}{Hand} & \multicolumn{2}{l|}{} \\ \cline{1-3}
\multicolumn{1}{|l|}{Activity} & \multicolumn{2}{l|}{Normal operation} & \multicolumn{2}{l|}{} \\ \cline{1-3}
\multicolumn{1}{|l|}{Task} & \multicolumn{2}{l|}{Part gasping} & \multicolumn{2}{l|}{} \\ \cline{1-3}
\multicolumn{1}{|l|}{Hazard Type} & \multicolumn{2}{l|}{Hand crushing} & \multicolumn{2}{l|}{} \\ \hline
\multicolumn{1}{|l|}{Description} & \multicolumn{4}{p{12cm}|}{To get the robot manipulator to pick up the torsion spring it has to travel to at location of the coil of the spring there it shall go down and insert the end-effector into the coil of the spring. There is a possibility that the human operator could have there hand on top of the coil then the robot has to insert the end-effector, the hand could be crouched between the end-effector and the coil. The robot will drive with the speed XX $mm/s$ and the robots torque sensor has the limits of XX $N$. The area of impact is the bottom of the end-effector there is XX $cm^2$. The force limit on the to the the hand is 200 N/cm2 and 0.49 J of energy } \\ \hline
\multicolumn{1}{|l|}{References} & \multicolumn{4}{l|}{\ref{sup:DS/ISO/TS15066} \ref{sup:ISO10218-2}} \\ \hline
\rowcolor[HTML]{FFCC67}
\multicolumn{5}{|l|}{\cellcolor[HTML]{FFCC67}Risk estimation} \\ \hline
\multicolumn{1}{|l|}{Degree of Possible Harm:} & \multicolumn{2}{l|}{0.5} & \multicolumn{1}{p{4.5cm}|}{Probability of occurrence of Hazardous Event:} & 1.25\\ \hline
\multicolumn{1}{|l|}{Possibility of Avoidance:} & \multicolumn{2}{l|}{0.75} & \multicolumn{1}{l|}{Frequency of exposure:} & 4 \\ \hline
\rowcolor[HTML]{FFCC67}
\multicolumn{5}{|l|}{\cellcolor[HTML]{FFCC67}Risk Evaluation} \\ \hline
\multicolumn{1}{|l|}{Pliz Hazard Rating (PHR):} & \multicolumn{2}{l|}{1.875} & \multicolumn{1}{l|}{Summary level} & \cellcolor[HTML]{32CB00} Negligible Risk \\ \hline
\rowcolor[HTML]{FFCC67}
\multicolumn{5}{|l|}{\cellcolor[HTML]{FFCC67}Risk reduction} \\ \hline
\multicolumn{5}{|p{\linewidth}|}{Not needed, because of the low impact damage and the speed of the robot makes it possible for the operator to avoid the robot. Therefor it is not very likely that the hazard will happen but the operator will still be exposed to the hazard fairly frequent.} \\ \hline
\end{tabular}
\end{table}
Which gives the following output:
table

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}

Latex table moves out of section and into the next page

I am making a journal paper with 2 columns. But I need table for the entire column. Using the below code table is pushed out of the comparatively analysis section into a new page.
\section{Comparatively Analysis}
\hfil\break
\begin{table*}
\centerline {TABLE 1}
\begin{center}
\begin{tabular} {|c|c|c|c|c|}
\hline
\multicolumn{5} { | c | }{Comparison Table}\\
\hline
Paper & Dataset & Approach & Model & Evaluation \\
\hline
Mary[7] & Air Pollution & Statistical & ST Correlation & RMSE 3.54 for 15\% missing data\\
\hline
Zhang[10] & Water Quality & Deep Learning & Seq2Seq & RMSE 3.29 \\
\hline
Turabieh[11] & Mammographic Mass, Hepatitis & Soft Computing & ANFIS-GA,ANFIS-PSO& Accuracy 85.2\% \\
\hline
Verma[12] & MIT-BIH ECG & Deep Learning & LSTM & 5-step RMSE 0.1 and 10-step RMSE 0.07 \\
\hline
Al-Milli[13] & Mammographic Mass & Deep Learning & Jordan RNN & Accuracy 84.27\% \\
\hline
\end{tabular}
\end{center}
\end{table*}
\hfil\break
\section{Conclusion}
Due to improper internet communications
TABLE OUT OF SECTION
NEW PAGE TABLE PUSHED
The main issue is that the table is too large. You have to use resizebox method from graphicx package. Here I provide a slightly better-looking table from the tabu package; this format is widely used in academia and in textbooks.
\usepackage{tabu, makecell, graphicx, booktabs}
%%Begin document (...)
\section{Comparatively}
\hfil\break
\begin{center}
\tabulinesep=1.6mm %%height between lines
\resizebox{1.1\columnwidth}{!}{ %% 1.1 is the scale factor of the column width
\begin{tabu}{ccccc}
\toprule
\toprule
\multicolumn{5}{c}{\ \ \ Comparison Table}\\
\multicolumn{1}{c}{\textit{Paper}}&\multicolumn{1}{c}
{\textit{Dataset}}&\multicolumn{1}{c}{\textit{Approach}}&\multicolumn{1}{c}
{Model}&\multicolumn{1}{c}{\textit{ Evaluation}}\\
\cmidrule(lr){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4}
\cmidrule(lr){5-5}
Mary$[7]$ & Air Pollution & Statistical & ST Correlation & \makecell{RMSE $3.54$ for \\ $15\%$ missing data } \\
Zhang$[10]$ & Water Quality & Deep Learning & Seq$2$Seq & RMSE $3.29$ \\
Turabieh$[11]$ & \makecell{Mammographic mass \\ Hepatitis} & Soft Computing & \makecell{ANFIS-GA,\\ ANGIS-PSO} & Accuracy $85.2\%$ \\
\bottomrule
\bottomrule
\end{tabu}
}
\end{center}
{ \footnotesize\textbf{Table 1:} Table description }
\hfil\break
\section{Conclusion}
Due to improper internet communications
Change the `scale factor back to 1.0 if the table seems to overlap to much on the right side. If not keep it at 1.1 for better visibility.

Creating side-by-side summary statistics table using esttab

I am trying to create a summary stats table with different categorical variables.
This is what I have managed to achieve with the community-contributed command esttab:
The code I have used is the following:
estpost tabstat diff_32 diff_43 diff_54 diff_42 diff_52 diff_53, ///
by(es) stat(mean sd) nototal columns(stat)
esttab . using "$tables/25_trial", replace cells(mean sd) tex label nogaps
However, what I need is for the columns to be side-by-side such that my mean is the main cell, with standard deviation as auxiliary. In other words, each column would contain one categorical variable, with mean and standard deviation in the same cell.
How can I produce the desired output?
Consider the following toy example:
sysuse auto, clear
estpost tabstat price weight mpg, by(foreign) stat(mean sd) nototal columns(stat)
esttab ., cells(mean sd) label nogaps tex
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{1}{c}}
\hline\hline
&\multicolumn{1}{c}{(1)}\\
&\multicolumn{1}{c}{}\\
& mean/sd\\
\hline
Domestic & \\
Price & 6072.423\\
& 3097.104\\
Weight (lbs.) & 3317.115\\
& 695.3637\\
Mileage (mpg) & 19.82692\\
& 4.743297\\
\hline
Foreign & \\
Price & 6384.682\\
& 2621.915\\
Weight (lbs.) & 2315.909\\
& 433.0035\\
Mileage (mpg) & 24.77273\\
& 6.611187\\
\hline
Observations & 74\\
\hline\hline
\end{tabular}
}
One has to merely use the unstack option in esttab to get the desired output:
esttab ., cells(mean sd) label nogaps tex unstack
{
\def\sym#1{\ifmmode^{#1}\else\(^{#1}\)\fi}
\begin{tabular}{l*{2}{c}}
\hline\hline
&\multicolumn{2}{c}{(1)} \\
&\multicolumn{2}{c}{} \\
& Domestic& Foreign\\
& mean/sd& mean/sd\\
\hline
Price & 6072.423& 6384.682\\
& 3097.104& 2621.915\\
Weight (lbs.) & 3317.115& 2315.909\\
& 695.3637& 433.0035\\
Mileage (mpg) & 19.82692& 24.77273\\
& 4.743297& 6.611187\\
\hline
Observations & 74& \\
\hline\hline
\end{tabular}
}

Resources