How to create a table that has combination of single column and double columns rows in latex? - latex

I am trying to create a table that has combination of single column and double columns rows? Any idea how can I do that in latex?
See Figure below:

You should set the tabular using the maximum number of columns, and then make some columns span \multicolumn:
\documentclass{article}
\usepackage{tabularx}
\usepackage[nopar]{lipsum}
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{| X | X | X |}
\hline
& Abstract & Direct \\
\hline
Type A & \multicolumn{2}{ p{\dimexpr.6667\linewidth-2\tabcolsep} |}{\lipsum[1]} \\
\cline{2-3}
& A & B \\
\hline
Type B & \multicolumn{2}{ p{\dimexpr.6667\linewidth-2\tabcolsep} |}{\lipsum[2]} \\
\cline{2-3}
& A & B \\
\hline
\end{tabularx}
\end{document}

Related

How to build a table in latex that one column can have three subcolumns

I am trying to build a table in Latex that one column which in my case in "Factor Loading" has three subcolumns, and also text in the "Item" column to be wrapped if it too big.
\multicolumn{}{}{} is the way to merge a number of cells but in your case, I would move "Factor loading" to a bottom part with annotations. The table seems to look more balanced without and the information is still there.
Here is the solution with a few improvements
\documentclass{article}
\usepackage{geometry}
\usepackage{tabularx} % Mainly for X-type column. Also loads very useful `array` package
\usepackage{booktabs} % For custom rules: \toprule, \bottomrule, \midrule, \cmidrule and \spacialrule
\usepackage{caption} % For custom formatting of captions
\usepackage{ragged2e} % Adds \RaggedRight and \Centering and improves text typesetting in narrow cells
\usepackage[nopar]{kantlipsum} % Only for summy text - can be removed in final draft
\captionsetup[table]{position=top, skip=1pt} % Adds a small gap between caption and a table
\newcommand\tn[1]{\rlap{\textsuperscript{#1}}} % A custom macro to add an annotation mark
\newcommand\thead[1]{\textbf{#1}} % Formats headings
\renewcommand{\tabularxcolumn}[1]{>{\RaggedRight}p{#1}} % For X formatting. Here, it only adds \RaggedRight
\newcolumntype{F}[1]{>{\Centering}p{#1}} % A custom column-type. Adds centering to default left alignment of p{}
\begin{document}
\begin{table}[tbh]
\renewcommand*{\arraystretch}{1.5} % Stretch a table vertically adding small spacing between rows
\centering
\caption{The table}
\label{tab:table}
\begin{tabularx}{1.0\linewidth}{#{} p{2.2cm} | X | *3{#{}F{1.2cm}} #{}}
\toprule
\multicolumn{1}{#{}p{2.2cm}}{\thead{Construct}} % \multicolumn can also cancel vertical bars around cells
& \multicolumn{1}{c}{\thead{Items}}
& \thead{KR}\tn{*}
& \thead{SG}\tn{*}
& \thead{US}\tn{*} \\
\specialrule{\lightrulewidth}{3pt}{0pt}
Collaborative & \kant[1][1] & 0.798 & 0.814 & 0.878 \\
& \kant[1][2] & 0.749 & 0.874 & 0.838 \\
& \kant[1][3] & 0.865 & 0.878 & 0.924 \\
& \kant[1][4] & 0.893 & 0.834 & 0.820 \\
& \kant[1][5] & 0.910 & 0.86 & 0.898 \\
& \kant[1][6] & 0.770 & 0.903 & 0.921 \\
\specialrule{\heavyrulewidth}{0pt}{0pt}
\multicolumn{5}{#{}l}{\tn{*}\hspace{0.25em} Factor loading}
\end{tabularx}
\end{table}
\end{document}
EDIT.Also, as suggested, tabularray is another alternative. The package gives you very easy interface to actually paint tables. The downside is time of compilation, which is a few times longer than that of regular tables.
Just for fun, here a variation with the tabularray package:
\documentclass{article}
\usepackage{tabularray}
\usepackage{lipsum}
\begin{document}
\begin{table}[tbh]
\caption{The table}
\label{tab:table}
\begin{tblr}{|l|X|c|c|c|}
\hline
Construct & Items & \SetCell[c=3]{} Factor loading &&\\
\hline
& & KR & SG & US \\
\hline
Collaborative & \lipsum[1][1] & 0.798 & 0.814 & 0.878 \\
& \lipsum[1][1] & 0.749 & 0.874 & 0.838 \\
& \lipsum[1][1] & 0.865 & 0.878 & 0.924 \\
& \lipsum[1][1] & 0.893 & 0.834 & 0.820 \\
& \lipsum[1][1] & 0.910 & 0.86 & 0.898 \\
& \lipsum[1][1] & 0.770 & 0.903 & 0.921 \\
\hline
\end{tblr}
\end{table}
\end{document}

How to put text under the title of table (caption)?

I need to add a reference under the title of my table and this was my code. However, the reference appear in the Table content. I need just to add reference under the title of the table before the tabular without adding it to the table content.
\begin{document}
\begin{table}[]
\fontsize{8}{10}\selectfont
\tabcolsep=0.3cm
\centering
\caption{Dataset statistics \cite{reference_here}}
%\vspace{-.2cm}
\label{table:dataset}
\begin{tabular}{#{}lr#{}}
\toprule
\textbf{Statistic} & \textbf{Count} \\ \midrule
\# of A & 800 \\
\# of B & 36,681 \\
\# of C & 36,681 \\
\# of D & 112,480 \\
Total E & 723,360 \\
\bottomrule
\end{tabular}
\vspace{-.3cm}
\end{table}
\end{document}
You can define two different caption texts, one for the actual table and one for the list of tables:
\caption[version for LoT]{Dataset statistics \cite{reference_here}}

How to set the fixed width of columns?

Hello everyone I am creating a table on latex my code looks like this:
\begin{table}[H]
\centering
\caption{caption}
\label{my-label}
\begin{tabular}{lll}
\hline
\multicolumn{3}{|c|}{\cellcolor[HTML]{34CDF9}{\color[HTML]{000000} Matriz confusión Genero.}} \\ \hline
\multicolumn{1}{|l|}{} & \multicolumn{1}{l|}{M} & \multicolumn{1}{l|}{F} \\ \hline
\multicolumn{1}{|l|}{M} & \multicolumn{1}{l|}{43} & \multicolumn{1}{l|}{7} \\ \hline
\multicolumn{1}{|l|}{F} & \multicolumn{1}{l|}{11} & \multicolumn{1}{l|}{39} \\ \hline
\end{tabular}
\end{table}
It works well but the problem comes when I try to fix the width of the columns I tried:
\begin{tabular}{l{2cm}|l{2cm}|l{2cm}}
The result is the same table, with variable length of columns, I would like to fix the length of the columns, I would like to appreciate any suggestion to solve this problem.
Consider the following code:
\documentclass[a4paper]{article}
\usepackage{}
\begin{document}
\begin{table}%[H]
\centering
\caption{caption}
\label{my-label}
\begin{tabular}{|p{20mm}|p{15mm}|p{10mm}|}
\hline
% \multicolumn{3}{|c|}{\cellcolor[HTML]{34CDF9}{\color[HTML]{000000} Matriz confusión Genero.}} \\ \hline
\multicolumn{3}{|c|}{Matriz confusión Genero.} \\ \hline
& M & F \\ \hline
M & 43 & 7 \\ \hline
F & 11 & 39 \\ \hline
\end{tabular}
\end{table}
\end{document}
that outputs the following table:
You may be interested in particular in the line
\begin{tabular}{|p{20mm}|p{15mm}|p{10mm}|}
implementing paragraph alignment for the contents of a column of given width (here 20, 15 and 10 mm respectively).
To make it simpler, you should just get rid of all of those \multicolumn{1}{}{} and change
\begin{tabular}{l{2cm}|l{2cm}|l{2cm}}
to
\begin{tabular}{|p{2cm}|p{2cm}|p{2cm}|}

Latex Table Syntax

\begin{center}
\begin{table}[h!]
\centering
\begin{tabular}{|l|l|l|l|}
\hline
Name of the Dataset File & Number of Cases & Number of Records & Primary Key \\
\hline
Detail Interaction & 147,004 & 2400 & Interaction ID \\
\hline
Detail Incident & \\
\hline
Detail Change & \\
\hline
Detail Activity & \\
\hline
\end{tabular}
\caption{Dataset description of the four datasets provided by Rabobank Group ICT.}
\end{table}
\end{center}
The table doesnt fit to the page. Can someone help me in making it in a page. The table has 4 columns and 5 rows.
I'd suggest writing the table in a more elegant way using booktabs, as there seems to be some superfluous information the headers:
\documentclass{article}
\usepackage{booktabs,siunitx}
\begin{document}
\noindent
\begin{tabular}{ l r r l }
\toprule
Dataset Filename & \multicolumn{1}{c}{Cases} & \multicolumn{1}{c}{Records} & Primary Key \\
\midrule
Detail Interaction & \num{147004} & \num{2400} & InteractionID \\
Detail Incident & \num{12345} & \num{20000} & IncidentID \\
Detail Change & \num{12} & \num{412} & ChangeID \\
Detail Activity & \num{7890} & \num{1234567} & ActivityID \\
\bottomrule
\end{tabular}
\end{document}
First of all, your code is missing some &s, in lines 11, 13 and 15: try
\begin{table}[h!]
\centering
\begin{tabular}{|l|l|l|l|}
\hline
Name of the Dataset File & Number of Cases & Number of Records & Primary Key \\
\hline
Detail Interaction & 147,004 & 2400 & Interaction ID \\
\hline
Detail Incident & & & \\% 2 occurrences of & added here
\hline
Detail Change & & & \\% 2 occurrences of & added here
\hline
Detail Activity & & & \\% 2 occurrences of & added here
\hline
\end{tabular}
\caption{Dataset description of the four datasets provided by Rabobank Group ICT.}
\end{table}
In a second instance, I advise you to see if you prefer something like
\begin{tabular}{|p{.3\textwidth}|p{.2\textwidth}|p{.2\textwidth}|p{.2\textwidth}|}
instead of just
\begin{tabular}{|l|l|l|l|}
In this case, you can use \par to get a linebreak inside a cell (for example: Name of the\par Dataset File).

How to insert manual line breaks inside LaTeX tables?

I know that if you define a width in a table's column, you can get automatic word-wrapping.
However, I need to control where newlines should happen in a specific table cell.
Thus, how can I insert manual line breaks in a LaTeX table cell?
Usually, you use a column definition like p{3cm} instead of l, and then use \newline instead of \\ in the cell body.
You could do it like this:
\documentclass{report}
\begin{document}
\begin{tabular}{|l|l|}
\hline
A & B \\
& C \\
\hline
D & E \\
\hline
\end{tabular}
\end{document}
which produces:
The command \shortstack can be used to wrap cell content and use \\ inside it:
\begin{tabular}{|l|l|}
\hline
one line & \shortstack{two\\ lines} \\
\hline
XX & YYY \\
\hline
\end{tabular}
EDIT: however I just realised that interline spacing might differ between your columns. So it's not the prettiest solution.
It can be achieved by using \newline. Since, the accepted answer did not have any sample snippet, a working sample is provided here:
\begin{tabular}{p{2cm} p{10cm}}
\em{Programming} \textsc{languages} & Java, Node.js, Python, Clojure \\
\newline & \newline \\
\em{Development systems} & Concurrent Programming, Design Patterns
\end{tabular}
You can do like Bart suggested above and combine with multirow to centralize single line texts.
\begin{table}[h]
\centering
\caption{Optimized models (Softmax) final results with confidence intervals.}
\begin{tabular}{|c|c|c|c|c|}
\hline
\multirow{2}*{Architecture} & Batch & N. & Learning & \multirow{2}*{Micro-F1} \\
& size & epochs & rate & \\
\hline
ResNet50& 64 & 60 & $5\times10^{-3}$ & $(\textbf{0.7683} \pm 0.0223)$ \\
\hline
ResNet152\_V2& 64 & 40 & $5\times10^{-4}$ & $(0.6698 \pm 0.0467)$\\
\hline
\end{tabular}
\label{final_result_softmax}
\end{table}
Yields:
\newline works to break a line within a cell in tabularx environment.

Resources