I am wanting to create a table as a header in LaTeX. 3 columns, the first cell must span two rows, the second cell must span two rows and the third cell must be split between the two rows. So 4 cells total but 3 columns like the picture below. Im having trouble with the spacing when I try and split the last column into two rows.
One first sketch:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|}
\hline
\multirow{2}{*}{Some text A} & \multirow{2}{*}{Some text B} & Some text C\\
\cline{3-3}
& & Some text D\\
\hline
\end{tabular}
\end{document}
This gives you:
It is not clear to me if you want your middle column to be larger than the two on the sides. Anyway, see this other answer of mine!
The tabularray package makes merging cells very easy:
\documentclass{article}
\usepackage{tabularray}
\begin{document}
\noindent%
\begin{tblr}{
colspec={lXc},
vlines,
hlines
}
\SetCell[r=2]{} some text & \SetCell[r=2]{} some long text some long text some long text & short text\\
&& short text\\
\end{tblr}
\end{document}
Related
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}
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}
I am trying to setup a table with 2 columns. It should fill the whole width of a A4 page.
Each column should contain a \multirow (different number of rows for each column).
The left column should be aligned on the left, the right column should be aligned on the right.
I am struggling with the details of the space filled between the columns. So far I've tried with the usual tabular package, but also with longtable, but I see all sorts of indentation problems. It seems like these packages don't honor the geometry details of the page.
See here:
% \begin{longtable}{#{\extracolsep{\fill}\hspace{\tabcolsep}} l r}
\begin{tabular}{>{\raggedright}p{0.5\linewidth}>{\raggedleft}p{0.5\linewidth}}
\multirow{2}{*}{\begin{tabular}{l}xx \\ zz \\ \end{tabular}} &
\multirow{2}{*}{\begin{tabular}{r}xx \\ zz \\ \end{tabular}} \\
\end{tabular}
% \end{longtable}
I would like to have 2 columns:
one on the far left, aligned on the left
the other on the far right, aligned on the right
no frames, or lines, just the text/words
If you don't need frames and the columns have different number of rows anyway, why not place two minipages besides each other instead of using a table?
\documentclass{article}
\begin{document}
\noindent%
\begin{minipage}{.5\textwidth}
\raggedright
xx
zz
\end{minipage}%
\begin{minipage}{.5\textwidth}
\raggedleft
xx
zz
\end{minipage}%
\end{document}
I have code for a latex table, but it seems it's missing the right vertical line in my title row. Am I missing something small? Here's my code:
\begin{center}
\begin{tabular}{||c c c||}
\hline
\textbf{Table Title} \\ [0.5ex]
\hline\hline
\hline
Col A & Col B & Col C \\
\hline\hline
1 & 0.39 & 6.16 \\
\hline
2 & 0.40 & 4.13 \\
\hline
3 & 0.28 & 0.12 \\
\hline
\end{tabular}
\end{center}
and this is the table I get:
As the row of interest spans three columns, you might want to use \multicolumn command: \multicolumn{# of columns}{alignment}{text}. So try replacing
\textbf{Table Title} \\ [0.5ex]
with
\multicolumn{3}{||l||}{\textbf{Table Title}}\\
Technically talking, the vertical line on the right of the first row does not appear because the table is set to have three cells per row but that first row only has one cell.
The two & in the line of code below would separate three cells within that row, allowing the vertical line on the right to show:
\textbf{Table Title} & & \\ [0.5ex]
Anyway, the solution using multicolumn shown in the other answer is to be preferred: this command allows the content of the first row to span along the three columns, less likeky influencing the overall width of the table.
I try to vertically center a text in the first column of a latex table with unknown length of the text of other columns. The other columns should be top aligned.
I already tried with tabular, tabularx, tabu table environment. All the approaches I have found in the internet for vertically center something are using the baseline or some kind of multirow environment.
multirow: not working, because of the unknown number of rows generating a long text in a fixed width column.
baseline: not working, because all other columns should be top aligned.
\documentclass{article}
\begin{document}
\begin{tabular} {| p{2cm} p{2cm} p{2cm} |}
\hline
centered & This is a long top aligned text, dynamically length. & This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environment. \\
\hline
\end{tabular}
\end{document}
I want that the text "centered" is vertically centered in this row.
A nearly identical question has several answers in TexStackExchange.
Also: https://www.latex-tables.com/ suggests using vcell for this kind of thing
\documentclass{article}
\usepackage{vcell}
\begin{document}
\begin{table}
\centering
\begin{tabular}{| p{2cm} p{2cm} p{2cm} |}
\hline
\vcell{centered}
& \vcell{This is a long top aligned text, dynamically length.}
& \vcell{This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environment.} \\
[-\rowheight]
\printcellmiddle
& \printcelltop
& \printcelltop \\
\hline
\end{tabular}
\end{table}
\end{document}
Meanwhile I came around with a workaround. I call it workaround because I think there should exist a simpler solution to this "easy" problem.
\documentclass{article}
\usepackage{adjustbox}
\usepackage{calc}
\usepackage{makecell}
\begin{document}
\newdimen\widthcola
\setlength{\widthcola}{2cm}
\newdimen\widthcolb
\setlength{\widthcolb}{2cm}
\newdimen\widthcolc
\setlength{\widthcolc}{2cm}
\newcommand{\tabline}[3]{
\newdimen\heightcella
\setlength{\heightcella}{\totalheightof{\makecell[t{p{\widthcola}}]{#1}}}
\newdimen\heightcellb
\setlength{\heightcellb}{\totalheightof{\makecell[t{p{\widthcolb}}]{#2}}}
\newdimen\heightcellc
\setlength{\heightcellc}{\totalheightof{\makecell[t{p{\widthcolc}}]{#3}}}
\newdimen\heightcellmax
\setlength{\heightcellmax}{\heightcella}
\setlength{\heightcellmax}{\maxof{\heightcellmax}{\heightcellb}}
\setlength{\heightcellmax}{\maxof{\heightcellmax}{\heightcellc}}
\adjustbox{padding=0mm, margin=0mm, raise=-0.5\heightcellmax+0.5\heightcella}{\makecell[t{p{\widthcolc}}]{#1}} & \makecell[t{p{\widthcolc}}]{#2} & \makecell[t{p{\widthcolc}}]{#3} \\
}
\begin{tabular} {| p{\widthcola} p{\widthcolb} p{\widthcolc} |}
\hline
\tabline{centered}{This is a long top aligned text, dynamically length.}{This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environemnt.}
\hline
\end{tabular}
\end{document}
So, does anybody know a simpler and more straightforward solution?
With the tabularray package it is easy to set the alignment for each column separately:
\documentclass{article}
\usepackage{tabularray}
\begin{document}
\begin{tblr}{| Q[2cm,valign=m] Q[2cm,valign=h] Q[2cm,valign=h] |}
\hline
centered & This is a long top aligned text, dynamically length. & This is a long top aligned text, much longer than the previous one...or shorter. Who knows what text length is given to me in my new environment. \\
\hline
\end{tblr}
\end{document}