table float in 2 columns environment - latex

I want to put a basic table into a 2-columns document.
I saw that instead of:
\begin{table}
enter code here
\end{table}
we should use:
\begin{table*}
enter code here
\end{table*}
but it is not working.
If anyone has an idea, thanks in advance! Below a minimal example.
\documentclass[10pt]{article}
\usepackage{lipsum}% fake text
\usepackage{multicol}% enable writing in 2 columns
\setlength{\columnsep}{7mm}% separation between the two columns
\setlength{\columnseprule}{1pt}% width of the separation lign(comment if not wanted)
\begin{document}
\begin{multicols}{2}
\lipsum[1]
\begin{table*}[t]
\begin{tabular}{cc}
A & b\\
c & d
\end{tabular}
\end{table*}
\end{multicols}
\end{document}

It seems that the document class is important.
If I change
article -> scrartcl
I can use the
\captionof{table}{my caption}
without using the table environment.
But I don't know if it is the right way to do it.

Floats and marginpars are not allowed inside multicols environment. Also, table* would be page wide. If you comment the table* environment, you get the tabular in the second column:
\documentclass[10pt]{article}
\usepackage{lipsum}% fake text
\usepackage{multicol}% enable writing in 2 columns
\setlength{\columnsep}{7mm}% separation between the two columns
\setlength{\columnseprule}{1pt}% width of the separation lign(comment if not wanted)
\begin{document}
\begin{multicols}{2}
\lipsum[1]
%\begin{table*}[t]
\begin{tabular}{cc}
A & b\\
c & d
\end{tabular}
%\end{table*}
\end{multicols}
\end{document}
Rather, you can nest the tabular inside a center environment and, if you need, use the package caption to label and reference your tabular although it's not a float.

Related

Problems in formatting table and closing tables "boxes" in latex

I am having problems in adjusting the format for a table in LaTex. The code is the following and the image depicts how the table comes out after compiling:
\begin{table}[H]
\centering
\renewcommand{\arraystretch}{1.2}
\resizebox{\columnwidth}{!}{
\begin{tabular}{|p{2cm}|c|c|c|}
\hline
{\textbf{Labels}} & {\textbf{Precision}} & {\textbf{Recall}} & {\textbf{F1-Score}}
\\
% \hline
\cline{2-9}
% \textbf{Inactive Modes} & \textbf{Description}\\
%\hhline{~--}
{Not Misogynous} & $37.49\%\pm1.91\%$ & $46.13\%$ & $36.15\%\pm2.61\%$ & \\ \hline
{Not Misogynous} & $37.49\%\pm1.91\%$ & $46.13\%$ & $36.15\%\pm2.61\%$ & \\ \hline
\end{tabular}
}
\caption{BERTweet binary task}
\label{table:bert_binary}
\end{table}
I don't understand how to add the necessary line to "close" the boxes around the table
You have a few issues in your code.
You specify 4 columns p{}ccc while a content of tabular body has 5 columns (with the extra ending &). This is why the lines are discontinued.
Numbers X and Y in the argument of \cline{X-Y} must not exceed the available number of columns. You define 4 and trying to draw horizontal rule between 2 and 9. You probably get the error: Extra alignment tab has been changed to \cr.
You should avoid inserting empty line in tables. Most of the times LaTeX do not accept \par in table environments, which is converted from empty lines. It may work in modern packages that use more recent advances in LaTeX.
I also wonder why you enclose cells inside {...}. The code works without them. Is there any particular reason? Perhaps in the main code you load siunitx and apply S-type column. Then, you do have to tell siunitx which cells are non-numeric by wrapping cells inside braces. Otherwise, siunitx issues error!
Here's my suggestion for the table:
I defined columns which accept math expressions without extra $...$
The main values and uncertainties are split in columns to improve formatting and spacing
booktabs provides improved rules that arguably improved presentation
Default gap between the table and its caption seems too large, so I slightly reduced it (requires caption package)
I keep captions of tables at the top and captions of figures at the bottom but this is again a personal preference.
One of the rules I usually follow is to avoid repeated information in tables. The % in your case is repeated everywhere. You could remove it and add annotation that values in all three columns are percentages.
Here's the table
and the code
\documentclass{article}
\usepackage{array}
\usepackage{booktabs}
\usepackage{caption}
\captionsetup[table]{position=bottom,skip=3pt}
\newcolumntype{R}{>{\(}r<{\)}}
\newcolumntype{L}{>{\(}r<{\)}}
\begin{document}
\begin{table}[tbh]
\centering
\renewcommand{\arraystretch}{1.2}
\caption{BERTweet binary task}
\label{table:bert_binary}
\begin{tabular}{p{2.5cm} R#{\;}L c R#{\;}L}
\toprule
\textbf{Labels}
& \multicolumn{2}{c}{\textbf{Precision}}
& \textbf{Recall}
& \multicolumn{2}{c}{\textbf{F1-Score}} \\
\midrule
Not Misogynous & 37.49 & \pm1.91 & 46.13 & 36.15 & \pm2.61 \\
Not Misogynous & 37.49 & \pm1.91 & 46.13 & 36.15 & \pm2.61 \\
\bottomrule
\multicolumn{6}{#{}l#{}}{\footnotesize All values in \%}
\end{tabular}
\end{table}
\end{document}
I think the previous answer to this question is very good and detailed, including many observations that is always useful to consider when doing tables in LaTeX. It also addresses you to avoid vertical lines in tables (consistently with the use of the package booktabs).
I add the following code and output just for the sake to remark the minimal edits necessary for your original code to reach a threshold look.
\documentclass{article}
\begin{document}
\renewcommand{\arraystretch}{1.2}
\begin{table}%[H]
\centering
\begin{tabular}{|p{3cm}|c|c|c|}
\hline
{\textbf{Labels}} & {\textbf{Precision}} & {\textbf{Recall}} & {\textbf{F1-Score}}\\
\hline
Not Misogynous & $37.49\%\pm1.91\%$ & $46.13\%$ & $36.15\%\pm2.61\%$\\
\hline
Not Misogynous & $37.49\%\pm1.91\%$ & $46.13\%$ & $36.15\%\pm2.61\%$\\
\hline
\end{tabular}
\caption{BERTweet binary task}
\label{table:bert_binary}
\end{table}
\end{document}

How to define the actual column width in two-column Latex document (excluding the space between the columns)?

I would like to redefine \columnwidth and set it to the value corresponding to (\textwidth-\columnsep)/2.
This is because I would like to use this redefined command inside tabular environment for controlling column width.
The problem with \columnwidth is that it disregards the space between columns, therefore it doesn't do the job.
Here's what I'm talking about. The following code compiles but gives an undesirable output.
\documentclass[twocolumn]{article}
\usepackage{lipsum}
\setlength{\columnsep}{2cm}
\newcommand\testcolwidth{(\textwidth-\columnsep)/2}
\begin{document}
\lipsum
\begin{table}[!h]
\begin{tabular}{p{.25\columnwidth}|p{.75\columnwidth}}
\hline
col 1 & col 2
\end{tabular}
\end{table}
\lipsum
\end{document}
This is what the output look like. It's undesirable because I don't want this line (and the table) to stretch into the space between the columns.
I have tried to define column width with the following line:
\newcommand\testcolwidth{(\textwidth-\columnsep)/2}
It results in an error when using \testcolwidth in place of \columnwidth. The error reads: Illegal unit of measure (pt inserted)
Any help? Thanks a lot.
The columnwidth already takes into account the space between the columns of the article, what is missing in your code is the tabcolsep which defines the space between the table columns. In your example, this space gets added four times, once at the front, once at the end and once on each site of the |. If you want to manually define your table like this, you need to know in advance how many columns it will have:
\documentclass[twocolumn]{article}
\usepackage{lipsum}
\setlength{\columnsep}{2cm}
\newcommand\testcolwidth{\dimexpr\columnwidth-4\tabcolsep}
\begin{document}
\lipsum
\begin{table}[!h]
\begin{tabular}{p{.25\testcolwidth}|p{.75\testcolwidth}}
\hline
col 1 & col 2
\end{tabular}
\end{table}
\lipsum
\end{document}
Much easier to let a package like tabularray do the job for you:
\documentclass[twocolumn]{article}
\usepackage{tabularray}
\usepackage{lipsum}
\begin{document}
\lipsum[2]
\begin{table}[!h]
\begin{tblr}{
colspec={X[1]|X[3]}
}
\hline
col 1 & col 2
\end{tblr}
\end{table}
\lipsum[2]
\end{document}

Is there any way to compress multiple lines in a single line in LaTeX?

I have constructed a big table in LaTeX, and since i am happy with it i was wondering if I can collapse it to a single line so that i dont have to scroll through the whole table code when i edit the rest of the document.
I was wondering if there is a way to compress it to a single line, like you can do in R of STATA, and then expand it if I want to edit the table.
You can create a separate file called, say, very_long_table.tex and paste the code of the table in it.
Then, in your .tex file you can \input this file.
For example:
very_long_table.tex
One & Other & One More \\
First & Second & Third
and your .tex file will be:
\documentclass{article}
\begin{document}
\begin{table}
\begin{tabular}{c c c}
\input{very_long_table}
\end{tabular}
\caption{A very, \emph{very} long table.}
\end{table}
\end{document}

Footnote in table* environment

How to add a footnote in table* environment? A solution is provided in the following link:
https://tex.stackexchange.com/questions/209802/footnote-in-table-environment
Here in the answer provide in the above link, the footnote appears along with the table notes. Is it possible for the footnote appear as the normal footnote in the footer page?
table* is typically associated with a two-column layout, and as a result delayed to not appear on the page it's called. We can use afterpage to delay execution of the \footnote so it'll appear on the same page as the table*:
\documentclass[twocolumn]{article}
\usepackage{lipsum,afterpage,refcount}
\newcommand{\setfootnotemark}{%
\refstepcounter{footnote}%
\footnotemark[\value{footnote}]}
\begin{document}
\lipsum[1-5]
\begin{table*}
\centering
\caption{A table caption}
\begin{tabular}{llll}
123\setfootnotemark\label{first} & 456 &
789\setfootnotemark\label{second} & abc \\
\end{tabular}
\afterpage{\footnotetext[\getrefnumber{first}]{First footnote.}
\footnotetext[\getrefnumber{second}]{Second footnote.}}
\end{table*}
\lipsum[6-10]
\end{document}

Getting two tables in LaTeX to have the same (right-aligned) column width

I have two very short and consecutive sections (for a CV), each containing a small table:
\section{Work Experience}
\begin{tabular}{r|p{11cm}}
Current & Your job at Your Company, Town \\
Jan 2009 & What your company does \\
& A description of what you do\\
\multicolumn{2}{c}{}\
\end{tabular}
\section{Education}
\begin{tabular}{r|p{11cm}}
Slightly wider first column & University, Town \\
Jan 2009 & Thesis subject \\
& A description of what you did\\
\multicolumn{2}{c}{}\
\end{tabular}
So each table has two columns: The first containing the period, aligned to the right. The second: some more info with a certain width, top (and left) aligned.
The problem is that the width of the left column in the two tables is different, and doesn't look nice since the sections (therefore tables) are consecutive and in one page. I cannot give r a width like p:
\begin{tabular}{r{11cm}|p{11cm}}
Does not work. How can I get the widths of the first columns of the two tables the same length while also having them right aligned?
EDIT Thanks for the answers, they all work for me so I upvoted all of them, and accepted the one that appealed to me the most (and most upvoted), since you don't have to specify the \hfill in each row. However if you don't want to use the array package for any reason then the other solutions are also great.
If you use the array package, you can put the \hfill in the header as follows, so you don't have to remember to put it (or a \parbox) in each row.
\documentclass{article}
\usepackage{multicol}
\usepackage{array}
\begin{document}
\section{Work Experience}
\begin{tabular}{>{\hfill}p{5cm}|p{11cm}}
Current & Your job at Your Company, Town \\
Jan 2009 & What your company does \\
& A description of what you do\\
\multicolumn{2}{c}{}
\end{tabular}
\section{Education}
\begin{tabular}{>{\hfill}p{5cm}|p{11cm}}
Slightly wider first column & University, Town \\
Jan 2009 & Thesis subject \\
& A description of what you did\\
\multicolumn{2}{c}{}
\end{tabular}
\end{document}
to give:
alt text http://www.freeimagehosting.net/uploads/5e29f675e3.jpg
Here's a variant of #RTBarnard's answer using the tabularx package:
\documentclass[a4paper,twoside,draft,12pt]{article}
\usepackage{tabularx}
\begin{document}
\section{Work Experience}
\begin{tabularx}{\textwidth}{>{\raggedleft}X|p{8cm}}
Current & Your job at Your Company, Town \\
Jan 2009 & What your company does \\
& A description of what you do\\
\end{tabularx}
\section{Education}
\begin{tabularx}{\textwidth}{>{\raggedleft}X|p{8cm}}
Somewhat wider than first column,
overflowing into additional lines & University, Town \\
Jan 2009 & Thesis subject \\
& A description of what you did\\
\end{tabularx}
\end{document}
Notes:
Why tabularx? Because it's often
easier to know the width you have
available for the whole table, and
to let TeX calculate the unknown
column widths.
The first parameter is the overall table width. Here, I've specified \textwidth to fill the width of typeblock, but you can change that to whatever measure you need.
I've used \raggedright rather than \hfill: if the item flows onto a second line, \hfill will only right-align the first line of the paragraph.
Was the \multicol significant? I've removed it to keep the answer as simple as possible.
Run with XeTeX under TeXLive.
Here's one solution of many possibilities:
\begin{tabular}{r|p{11cm}}
\parbox{11cm}{\hfill Current} & Your job at Your Company, Town \\
Jan 2009 & What your company does \\
& A description of what you do\\
\multicolumn{2}{c}{}\
\end{tabular}
Basically, create a \parbox with the desired width and put an \hfill at the left.
You can give both p{width} options, and start each cell in the left with an \hfill.
You can use array package to specify a fill command for each row in your first column:
\begin{tabular}{>{\hfill}p{11cm}|p{11cm}|}
For example:
\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{>{\hfill}p{5cm}|p{11cm}|}
This is a test & test
\end{tabular}
\begin{tabular}{>{\hfill}p{5cm}|p{11cm}|}
Test & this is a test
\end{tabular}
\end{document}

Resources