Aligning column in tabularx - alignment

I want to align the last column properly. There is extra space towards the end. Please let me know how to do that. Thanks.
\documentclass[11pt]{article}
\usepackage{setspace} %double spacing and spacing in tables
\usepackage{amsmath} %equations etc. in latex
\usepackage[capposition=top]{floatrow} %so that the caption for figures appear at the top
\usepackage[tablename=TABLE,figurename = FIGURE,labelsep=newline,aboveskip=0pt,font=bf,justification=centering]{caption} %so that caption looks cool
\usepackage{booktabs} %midrule etc. which adds space around lines. Make tables look good.
\usepackage{tabularx} %use tabularx environment for creating one page tables
\usepackage[margin=1in]{geometry} %defining the margin for the page
\usepackage[autostyle]{csquotes} %for quotes. alternative would be " and "
\usepackage[table]{xcolor} %rowcolor, cellcolor, color for references
\usepackage{pdflscape} %landscape and keep the pages straight
\usepackage{everypage} %For AddEveryHookPage
\usepackage{hanging} %For references
\usepackage{longtable} %For multiple pages
\usepackage{multirow} %Valuable package as can be seen from table 5,8, and 10
\usepackage{graphicx} %created line using this package
\usepackage{bm} %bold and italics in the math enviornment at the same time
\usepackage{dcolumn} % a package actually used in this example
\def\hang{\par\noindent\makebox[1.5em][l]{ }\hangindent1.5em} %hanging indent
\makeatletter
\setlength{\#fptop}{0pt} %to make tables and figures start at the top of the page
\makeatother
\newcommand{\Lpagenumber}{\ifdim\textwidth=\linewidth\else\bgroup %to correct the page numbering for landscape pages
\dimendef\margin=0
\ifodd\value{page}\margin=\oddsidemargin
\else\margin=\evensidemargin
\fi
\raisebox{\dimexpr -\topmargin-\headheight-\headsep-0.5\linewidth}[0pt][0pt]{%
\rlap{\hspace{\dimexpr \margin+\textheight+\footskip}%
stabu \llap{\rotatebox{90}{\thepage}}}}%
\egroup\fi}
\AddEverypageHook{\Lpagenumber}% %to correct the page numbering for landscape pages
\newcolumntype{Y}{>{\centering\arraybackslash}X} %to center the columns for tabularx environment
\newcolumntype{Z}{>{\centering\arraybackslash}p{0.75in}} %to center the columns for tabularx environment
\newcolumntype{K}{>{\centering\arraybackslash}p{0.3in}} %to center the columns for tabularx environment
\newcommand{\gmc}[3]{\multicolumn{#1}{#{}#2#{}}{#3}} %short form for multicolumn
\doublespacing %make lines double spaced
\begin{document}
\setcounter{page}{-1} %start numbering at page 3 (2 - n where n = -1)
\thispagestyle{empty} %suppress page number
\begingroup % keep any font size changes local to group
\captionof{table}{\textbf{Descriptive Statistics}}
\singlespacing
\footnotesize
\centering
\setlength\tabcolsep{3pt} %default
\renewcommand{\arraystretch}{1}
\begin{tabularx}{\textwidth}{#{\extracolsep{\fill}}l*{2}{c}}
\gmc{3}{l}{\textbf{Panel A: Sample Representation across Economic Sectors}} \\\midrule
\textbf{Sector} & \textbf{N} & \textbf{\% Frequency} \\\midrule
Energy & 524 & 1.65\% \\
Materials & 2,648 & 8.34\% \\
Industrials & 5,905 & 18.60\% \\
Consumer Discretionary & 5,549 & 17.48\% \\
Consumer Staples & 2,174 & 6.85\% \\
Health Care & 5,167 & 16.28\% \\
Financials & 0 & 0.00\% \\
Information Technology & 9,774 & 30.79\% \\
Telecommunication Services & 0 & 0.00\% \\
Utilities & 0 & 0.00\% \\\midrule
Total & 31,741 & 100.00\% \\\midrule
\end{tabularx}
\endgroup
\end{document}

If you just want it to have no space for the last column you can
replace
\begin{tabularx}{\textwidth}{#{\extracolsep{\fill}}l*{2}{c}}
with:
\begin{tabularx}{\textwidth}{l Y r}

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}

An extra column created in a single row in a table

I wanted a table in latex, and the headings to be centered both horizontally and vertically within it with more vertical space than the rest of the columns. This is the code i wrote;
\begin{tabular}{| l | l | l |}\hline
\rowcolor{lightgray} \multicolumn{1}{|c|}{\large{\textsc{Vitamin}}} & \multicolumn{1}{|c|}{\large{\textsc{Use In Body}}} & \multicolumn{1}{|c|}{\large{\textsc{Deficiency Disease}}}\rule{0pt}{25pt} \rule[-25pt]{0pt}{25pt}\\\hline
\linebreak
& &
\end{tabular}
This is the result i get.
Result image
There is an extra column or something that exists after the third column. Do you know any fixes random stranger?
You need to insert the vertical rule (strut) within a column. You have defined it outside it which is causing the unexpected extensions.
\documentclass{article}
\usepackage[table]{xcolor}
\begin{document}
\begin{tabular}{| l | l | l |}\hline
\rowcolor{lightgray}\multicolumn{1}{|c|}{\large{\textsc{Vitamin}}} & \multicolumn{1}{c|}{\large{\textsc{Use In Body}}} & \multicolumn{1}{c|}{\large{\textsc{Deficiency Disease\rule{0pt}{25pt} \rule[-25pt]{0pt}{25pt}}}}
\\\hline
& & \\
\hline
\end{tabular}
\end{document}
You can use a single rule instead of two like this \rule[-25pt]{0pt}{50pt}. Also, notice that the 1st row is not vertically centered.
I would propose you use the tabularray package which makes managing rows and columns very easy. Also, it's more preferred for color tables with vertical lines.
Code using the tabularray package:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{
hlines, vlines,
colspec=lll,
row{1}={c, m, 50pt, lightgray, font=\large\scshape},
}
Vitamin & Use In Body & Deficiency Disease\\
&&\\
\end{tblr}
\end{document}

How to avoid the line break in my column-headings?

I want to create a table with (at least) 6 columns. Right now, I automatically get a line break after 4 columns and i don't know how to avoid it.
I thought I'd already have my solution with /resizebox, but obviously it doesn't change anything.
\begin{table}[htb]
\caption{Evaluationstabelle}
\resizebox{\textwidth}{!}{%
\begin{tabular}{cccc}\toprule
\textbf{Anzahl Features} &\textbf{Entfernte Features} &\textbf{Filter} &\textbf{Precision} &\textbf{Recall} &\textbf{F-Score} \\
\midrule
08.05. & Ausarbeitung & Formuliere & Bsp & ok & shit \\[20pt]
\bottomrule
\end{tabular}
}
\end{table}
This is how it looks right now:
But I'd want all Columns in one line. If it doesn't fit, maybe by reducing the font size or something.
Whatever you do, don't use \resizebox for elements that contain text, see https://tex.stackexchange.com/questions/425453/why-not-scale-elements-that-contain-text for more details.
One possibility would be to use a tabularx and let latex decide on the best columns widths and linebreaks. In case this is not enough to fit your table in your available textwidth, this can be combined with a smaller font (commented in the code below)
\documentclass{article}
\usepackage{caption}
\usepackage{geometry}
\usepackage{array}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{makecell}
\renewcommand\theadfont{\normalfont\bfseries}
\renewcommand\theadalign{YY}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{document}
\begin{table}[htbp]
\caption{Evaluationstabelle}
%\small
\begin{tabularx}{\linewidth}{#{}YYYYYY#{}}
\toprule
\thead{Anzahl Features} & \thead{Entfernte Features} &\thead{Filter} &\thead{Precision} & \thead{Recall} & \thead{F-Score}\\
\midrule
08.05. & Ausarbeitung & Formuliere & Bsp & ok & test\\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

Vertical align in table on Latex

I want to make a table and print the text in the middle of all cases. I use the array package and I wrote:
\begin{table}[H]
\begin{center}
\begin{tabular}{|m{3cm}|m{2.5cm}|m{2.5cm}|m{2.5cm}|m{6cm}|}
But then Latex says that I missed one '$' or one '{' in my table... but there is only text, so I don't understand. When I switched to :
\begin{table}[H]
\begin{center}
\begin{tabular}{|m{3cm}|C{2.5cm}|C{2.5cm}|C{2.5cm}|C{6cm}|}
all is ok and works... but it's ugly. I read a lot of stuff about that on the site already but never fixed my obvious-like problem :/
Here is a minimal example of the layout I use for tables.
Different combinations of sizing and formatting are shown.
I am not sure if this is what you asked for. Please reformulate your question, if it is not.
\documentclass{scrartcl}
\usepackage{booktabs} % \cmidrule in tables
% \usepackage{caption} % Nice Captions
% \usepackage{longtable} % Tables larger than one page
% \usepackage{multirow} % Mergings Cells
% \usepackage{multicol} % Merging Cells
\usepackage{tabularx}
\begin{document}
\begin{table}
\caption{Some caption, for tables always above}
\label{some label}
\begin{tabularx}{0.99\textwidth}{>{\centering}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}p{0.5cm}>{\raggedleft}p{3cm}>{\raggedright}X>{\raggedleft\arraybackslash}X}
\toprule
\textsc{Foobar} & $a$ & $b$ & $c$ & $d$ & $e$ & $f$ \\
\cmidrule(r){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-4} \cmidrule(lr){5-5} \cmidrule(lr){6-6} \cmidrule(l){7-7}
1 & 2 & 2 & 2 & 2 & 2 & 2 \\
2 & 4 & 5 & 4 & 4 & 4 & 4 \\
3 & 4 & 3 & 4 & 3 & 3 & 3 \\
\bottomrule
\end{tabularx}
\end{table}
\end{document}

Resources