centre a table in latex with headings above cells - latex

I have a table I wish to fit in the margins. I also wish to add both EPS and PPS above the middle of the cells with these two in. I am not sure why adding a caption is not working either. Here is the code:
\begin{tabular}{ |p{3cm}||p{3cm}|p{3cm}|p{3cm}|p{3cm}| }
\multicolumn{5}{c}{} \\
\hline
&EPS:Pre &EPS:Post &PPS:Pre & PPS:Post \\
\hline
Species tested:&13 &15& 43& 43\\
Compounds tested:& 745 & 745& 310& 361 \\
Unique tests:& 193& 193& 406& 407\\
Total experiments:&17,811 &17,929& 107,470& 130,926\\
\hline
\end{tabular}
\label{tab}
Thank you.

There where several problems.
Your table was too large, with the margins and intercolumn spacing. I made a macro to fine adjust column width.
To redefine a column (for instance to center a header), you can use `\multicolumn{1}{c}{header}
\label assigns a number that is used in a numbered environment. It is associated with the table environmenent, not with tabular. The label appears within a caption. Also, the table is centered within the table environment.
\documentclass{article}
\begin{document}
\newcommand{\colwidth}{0.17\textwidth}
\newcommand{\centercolumn}[1]{\multicolumn{1}{c|}{#1}}
\begin{table}
\centering
\begin{tabular}{ |p{3cm}||p{\colwidth}|p{\colwidth}|p{\colwidth}|p{\colwidth}| }
% \multicolumn{5}{c}{} \\ useless, I think
\hline
&\centercolumn{EPS:Pre} &\centercolumn{EPS:Post} &\centercolumn{PPS:Pre} & \centercolumn{PPS:Post} \\
\hline
Species tested:&13 &15& 43& 43\\
Compounds tested:& 745 & 745& 310& 361 \\
Unique tests:& 193& 193& 406& 407\\
Total experiments:&17,811 &17,929& 107,470& 130,926\\
\hline
\end{tabular}
\caption{My table}
\label{tab:1}
\end{table}
\end{document}

Related

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}}

Missing Vertical Line Table

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.

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}

rowcolors fill whole row and not just the table row

Ok so i wrote this simple code (shown below) to create an alternate colour table using \rowcolors, yet instead of just colouring the rows of the table it colours the whole row (even more than textwidth). Any help how to fix this?
begin{table}[ht]
\scriptsize
\begin{center}
\rowcolors{1}{lightgray}{white}
\caption{...}
\begin{tabular}{p{0.45\textwidth} | p{0.55\textwidth}}
Filename & Contents \\
\hline
\hline
A & B \\
C & F \\
\end{tabular}
\end{center}
\end{table}
While 0.45\textwidth + 0.55\textwidth would seem to fit within \textwidth, each of your p-columns have an extra column separation... on both sides. To that end, you should remove them in order to make it fit within the text block boundary:
\documentclass{article}
\usepackage[table]{xcolor}
\usepackage{lipsum}
\begin{document}
\begin{table}[ht]
\scriptsize\centering
\rowcolors{1}{lightgray}{white}
\caption{This is a table.}
\begin{tabular}{
p{\dimexpr0.45\textwidth-2\tabcolsep} |
p{\dimexpr0.55\textwidth-2\tabcolsep}}
Filename & Contents \\
\hline
\hline
A & B \\
C & F
\end{tabular}
\end{table}
\lipsum[1]
\end{document}
Also, don't use the center environment; use \centering instead.

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