Table filling width with 2 columns each as multi-row - latex

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}

Related

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 split a cell in a table in latex?

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}

Latex table: Vertically center text in one column, other columns top aligned with fixed width

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}

Table in Latex Overlapping with Second Column of the Page

I got a problem with tables in LaTeX. I am using a pre-defined format from a conference, which makes a page consists of two columns. My table is too wide to be contained in one column, so it goes to the second column of the page and overlaps with text!
Why is LaTeX allowing this?
Is it possible in the current format, to make the table a block, so that it uses two columns, and the text in the other column dragged down a bit?
Any other suggestion?
This is my code:
\begin{table}
\centering
\begin{tabular}{|c|c|c|c|c|c|}
\hline
Rule & Factor & Best Value & Delta_t & Delta_{do} & Comments \\
\hline
\multirow{3}{c}{Diagonal Dominance} & Line Angle & 45 & 15 & 30 & The angle between the prominent line of the object and
the diagonal lines \\ % TODO: What object? Make sure it is clear.
& Line Distance & 0 & 0.25 & 1 & The distance, in screen coordinates, from the
prominent line of the object to the diagonal lines. \\ % TODO: Need to define screen coordinates
& Corner Distances & 0 & 0.1 & 0.7 & The distance, in screen coordinates, from the
end of the prominent line of the object to the corners of the screen. \\
\hline
\end{tabular}
\caption{The factors of each rule and their parameters.}
\label{table:factors}
\end{table}
Thank you in advance for your help.
Regards,
Rafid
Generally, the starred version of floating environments are used to tell LaTeX to span two columns. So, something like:
\begin{table*}
% Table contents
\end{table*}
The same can be done with figures.
In place of tabular, use tabularx, for example:
\begin{tabularx}{\textwidth}{|c|c|c|c|c|X|}
Sorry, #Rafid, I normally use memoir, which includes it, so I forgot this bit:
\usepackage{tabularx}

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