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}
Related
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}
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}
Using the wrapfig package with a table inside a section, the table's first row is not aligned with the text that wraps it. This problem is not present when working outside of a section.
\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}
\begin{document}
\section{Section}
\begin{wraptable}{l}{0pt}
\begin{tabular}{cccc}
A & B & C & D \\
E & F & G & H\\
\end{tabular}
\label{Mytable}\caption{This is my table.}
\end{wraptable}
\textbf{This bit of text should be aligned with the table's top row.}
\lipsum[2]
\end{document}
What this gives is:
Whereas ideally I'd like to get something like:
You could try to adjust \intextsep:
\documentclass{article}
\usepackage{wrapfig}
\usepackage{lipsum}
\begin{document}
\section{Section}
{
\setlength\intextsep{-0.4ex}
\begin{wraptable}{l}{0pt}
\begin{tabular}{cccc}
A & B & C & D \\
E & F & G & H\\
\end{tabular}
\label{Mytable}\caption{This is my table.}
\end{wraptable}
\textbf{This bit of text should be aligned with the table's top row.}
\lipsum[2]
}
\end{document}
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}
I am trying to center one of the columns in a table and It says I have an extra column. The table also doesnt look correct i.e.
What it looks like:
F(t) F(s)
u(t)
1/s
e 1/(s-a)
What I expect:
F(t) F(s)
u(t) 1/s
e 1/(s-a)
I think it is because I'm trying to insert an equation in the table but I'm not sure.
Here is the code
\documentclass[12pt, letterpaper, fleqn]{article}
\usepackage{array}
\begin{document}
\begin{table}
% \setlength{\tabcolsep}{6pt}
% \renewcommand{\arraystretch}{1}
\begin{tabular}{p{0.225\textwidth} >{\centering}p{0.15\textwidth}}
\textbf{F(t)} & \textbf{F(s)} \\
\(u(t)\) & \(\frac{1}{s}\) \\
\(e^{at}\) & \(\frac{1}{s-a}\) \\
\end{tabular}
\end{table}
\end{document}
Your use of \centering to centre the column works, but it screws up the way \\ is interpreted. A correction is included in the array package documentation after using alignment switches (like \centering, \raggedright, \raggedleft, etc) using \arraybackslash:
\documentclass{article}
\usepackage{array}
\begin{document}
\begin{tabular}{ p{0.225\textwidth} >{\centering\arraybackslash}p{0.15\textwidth} }
$\mathbf{F(t)}$ & $\mathbf{F(s)}$ \\
$u(t)$ & $1 / s$ \\
$e^{at}$ & $1 / (s - a)$
\end{tabular}
\end{document}
If you remove >{\centering}, then your code should compile as expected. Post a comment otherwise! Do you want any column to have centered contents?