How to insert manual line breaks inside LaTeX tables? - latex

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.

Related

LaTex table - Column size and text alignment

I am quite a beginner in LaTex and generated the following table:
\documentclass{article}
\usepackage[utf8]{inputenc}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|p{2,5cm}||p{3,5cm}|p{2cm}| p{2cm}|}
\hline
\multicolumn{4}{|c|}{Initial Run of Models} \\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines \\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tabular}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
Is there any way to maintain the width of the columns and still align the text in the columns to be on the right side?
You could use the array package and add >{\raggedleft}:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|>{\raggedleft}p{2.5cm}||>{\raggedleft}p{3.5cm}|>{\raggedleft}p{2cm}| >{\raggedleft\arraybackslash}p{2cm}|}
\hline
\multicolumn{4}{|c|}{Initial Run of Models} \\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines \\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tabular}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
Personally, I would suggest to use the tabularray package instead. This package makes it easy to set the alignment for row/columns or individual cells.
You could use Q[2.5cm] to specify a width, but if you don't, tabularray will determine a much better width for you.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularray}
\begin{document}
\begin{table}[h]
\centering
\begin{tblr}{
colspec={|Q||Q|Q|Q|},
rows={halign=r},
column{1}={halign=l},
row{1}={halign=c},
row{2}={halign=l},
}
\hline
\SetCell[c=4]{} Initial Run of Models &&& \\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines \\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tblr}
\caption[Initial Run of Models]{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
If you use the package array as suggested in the first answer, then you can define in your header one or more newcolumntypes, for example this one called R, right aligned with the width to be assigned to each column. More details here.
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{array}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\begin{document}
\begin{table}[h]
\centering
\begin{tabular}{|R{2,5cm}||R{3,5cm}|R{2cm}|R{2cm}|}
\hline
\multicolumn{4}{|c|}{Initial Run of Models}\\
\hline
Objective & Service Level in \% & Costs in € & Number of Lines\\
\hline
Max. direct pass. & 93.48 & 1,258.41 & 7\\
Min. travel time & 77.99 & 933.13 & 7\\
\hline
\end{tabular}
\caption{\label{Tab:InitialRun}Initial Run of Models}
\end{table}
\end{document}
Here I changed to R all of the four columns but they don't have to be all of the same kind.
Make a search because surely there are many "duplicates" of this question here and on TeX SE.

Why in overleaf I miss a vertical edges in the table?

I am new in Latex, I create a table
\begin{table}[!ht]
\centering
\caption{Cities analyzed in this study}
\begin{adjustbox}{max width=\textwidth}
\begin{tabular}{|l|c|c|c|c|c|c|c|c|c|}
\hline
City & Number of & Number of & \multicolumn{2}{c}{Transportation type} \\
\cline{4-9}
& stations & routes & Bus & Tram & Subway&Rail&Ferry& Cable-car \\
\hline
Adelaide & 7548 & 9234 & 8950 &54 & - & 230 & -&- \\
\hline
But in the top right, the vertical line is not showing. How can I make it appear?
My second question how can I put 'Transportation type' on the center of the cell?
You can add the missing edge by using \multicolumn{6}{c|}{...}, but before you actually use this, please have a look at http://betterposters.blogspot.com/2012/08/the-data-prison.html or https://www.inf.ethz.ch/personal/markusp/teaching/guides/guide-tables.pdf for some guides about nice table layouts. Using vertical lines is really bad style.
Please also don't scale elements that contain text. This will make result in a suboptimal usage of font shapes. If you must make your table smaller, manually choose an appropriate font size.
\documentclass{article}
\begin{document}
\begin{table}[!ht]
\centering
\caption{Cities analyzed in this study}
\begin{tabular}{|l|c|c|c|c|c|c|c|c|c|}
\hline
City & Number of & Number of & \multicolumn{6}{c|}{Transportation type} \\
\cline{4-9}
& stations & routes & Bus & Tram & Subway&Rail&Ferry& Cable-car \\
\hline
Adelaide & 7548 & 9234 & 8950 &54 & - & 230 & -&- \\
\hline
\end{tabular}
\end{table}
\end{document}

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 repeat top rows of a table in each page in LaTeX

I have a table in LaTeX which spans multiple pages.
I want the top two rows repeated in each page.
How can I do that?
From here:
Tables longer than a single page
[...]
Both longtable and supertabular allow definition of head- and footlines for the table; longtable allows distinction of the first and last head and foot.
[...]
you've got to use the longtable package. Here is an example of the head (source: http://users.sdsc.edu/~ssmallen/latex/longtable.html)
\begin{center}
\begin{longtable}{|l|l|l|}
\caption[Feasible triples for a highly variable Grid]{Feasible triples for highly variable Grid, MLMMH.} \label{grid_mlmmh} \\
\hline \multicolumn{1}{|c|}{\textbf{Time (s)}} & \multicolumn{1}{c|}{\textbf{Triple chosen}} & \multicolumn{1}{c|}{\textbf{Other feasible triples}} \\ \hline
\endfirsthead
\multicolumn{3}{c}%
{{\bfseries \tablename\ \thetable{} -- continued from previous page}} \\
\hline \multicolumn{1}{|c|}{\textbf{Time (s)}} &
\multicolumn{1}{c|}{\textbf{Triple chosen}} &
\multicolumn{1}{c|}{\textbf{Other feasible triples}} \\ \hline
\endhead
\hline \multicolumn{3}{|r|}{{Continued on next page}} \\ \hline
\endfoot
\hline \hline
%here your table content goes
\end{longtable}
\end{center}
\endlastfoot

LaTeX table too wide. How to make it fit?

I just started to learn latex and now I'm trying to create a table. This is my code:
\begin{table}
\caption{Top Scorers}
\begin{tabular}{ l l }
\hline
\bf Goals & \bf Players\\
\hline
4 & First Last, First Last, First Last, First Last\\
3 & First Last\\
2 & First Last\\
1 & First Last, First Last, First Last, First Last, First Last, First Last, First Last, First Last, First Last, First Last, First Last, First Last, First Last\\
\hline
\end{tabular}
\end{table}
The problem is that the table is wider than the page. I was hoping that it would automatically fit to the page like normal text does, but it didn't. How do I tell latex to make the table fit to the page?
Use p{width} column specifier: e.g. \begin{tabular}{ l p{10cm} } will put column's content into 10cm-wide parbox, and the text will be properly broken to several lines, like in normal paragraph.
You can also use tabular* environment to specify width for the entire table.
You have to take whole columns under resizebox. This code worked for me
\begin{table}[htbp]
\caption{Sample Table.}\label{tab1}
\resizebox{\columnwidth}{!}{\begin{tabular}{|l|l|l|l|l|}
\hline
URL & First Time Visit & Last Time Visit & URL Counts & Value\\
\hline
https://web.facebook.com/ & 1521241972 & 1522351859 & 177 & 56640\\
http://localhost/phpmyadmin/ & 1518413861 & 1522075694 & 24 & 39312\\
https://mail.google.com/mail/u/ & 1516596003 & 1522352010 & 36 & 33264\\
https://github.com/shawon100& 1517215489 & 1522352266 & 37 & 27528\\
https://www.youtube.com/ & 1517229227 & 1521978502 & 24 & 14792\\
\hline
\end{tabular}}
\end{table}
You can use these options as well, either use \footnotesize or \tiny. This would really help in fitting big tables.
\begin{table}[htbp]
\footnotesize
\caption{Information on making the table size small}
\label{table:table1}
\begin{tabular}{ll}
\toprule
S.No & HMD \\
\midrule
1 & HTC Vive \\
2 & HTC Vive Pro \\
\bottomrule
\end{tabular}
\end{table}

Resources