How to show tables with LATEX in Google Colab? - latex

How are you,
I am using Google Colab to solve exercises of my university in combination with the pandas library.
According to the documentation of Colab to be able to show some formula using LATEX, you have to introduce it between two dollar signs ($) which works correctly for me. For example here an image of a part of my document that works without problems.
The drawback occurs when I try to generate a table with some values ​​that will serve to explain the problem I am solving but are not rendered. Next I show the image of how it is rendered
As a test try to render a matrix that is the closest thing to a table and I have no problems.
Can the problem occur because Colab is not supporting or is blocking the rendering of tables?
What would be the correct way to generate a table within the text part of Google Colab?
I have found solutions for how to use LATEX in other stackoverflow responses but these solutions do not work for me since I want the table to appear in the text part of my colab document and not within the code part.
Thank you for your answers.

Use array instead of tabular. Tabular environments can be used to display tables that are typeset as text in math mode, but the most common is the use of arrays.
For some unknown reason, in google colab, array is supported and considered as a valid LaTeX command, while tabular is considered as a code sample.
\begin{array}{ccc}
foo&bar&baz\\
1&2&3
\end{array}
\begin{tabular}{ccc}
foo&bar&baz\\
1&2&3
\end{tabular}
Array enters TeX math mode and even plain text is typeset in math mode. To avoid this behavior you can use \text{my text}
\begin{array}{ccc}
\text{foo}&\text{bar}&\text{baz}\\
1&2&3
\end{array}

In co labs using latex for Big O notation, this worked for me
Math(r'\displaystyle \\
\begin{array}{|c|c|c|c|c|c|c|c|}\hline\\ \\
\mathcal{} & \mathcal{A} & \mathcal{B} & \mathcal{O} & o & \Omega & \omega & \Theta \\ \hline\\ \\
a & lg^kn & n^\epsilon & yes\; no & yes\; no & yes\; no & yes\; no & yes\; no \\ \hline\\ \\
b & n^k & c^n & yes\; no & yes\; no & yes\; no & yes\; no & yes\; no \\ \hline\\ \\
c & \sqrt{n} & n^{sin n} & yes\; no & yes\; no & yes\; no & yes\; no & yes\; no \\ \hline\\ \\
d & 2^n & 2^{n/2} & yes\; no & yes\; no & yes\; no & yes\; no & yes\; no \\ \hline\\ \\
e & n^{lg c} & c^{lg n} & yes\; no & yes\; no & yes\; no & yes\; no & yes\; no \\ \hline\\ \\
f & lg(n!) & lg(n^n) & yes\; no & yes\; no & yes\; no & yes\; no & yes\; no \\ \hline\\
\end{array}\\')

Just play around with |c| 's and \hline 's to get nice-looking tables using array in latex.
Below are a few handy templates (works in colab).
Template 1
\begin{array}{|c|c|} \hline
column1 & column2 & column3 & column4 \\ \hline
foo & bar & foo & bar \\
foo & bar & foo & bar \\
foo & bar & foo & bar \\ \hline
\end{array}
Template 2
\begin{array}{|c|} \hline
column1 & column2 & column3 & column4 \\ \hline
foo & bar & foo & bar \\
foo & bar & foo & bar \\
foo & bar & foo & bar \\ \hline
\end{array}
Template 3
\begin{array}{c} \hline
column1 & column2 & column3 & column4 \\ \hline
foo & bar & foo & bar \\
foo & bar & foo & bar \\
foo & bar & foo & bar \\ \hline
\end{array}
Template 4
\begin{array}{|c|c|} \hline
column1 & column2 & column3 & column4 \\ \hline
foo & bar & foo & bar \\ \hline
foo & bar & foo & bar \\\hline
foo & bar & foo & bar \\ \hline
\end{array}

Related

Table with box inside

How to code this kind of table? The code for the table is easy for me but I don't know how to put a box in the upper right in every column. Thank you.
I found a way to add a box in the upper right corner, by adding additional columns (one left and one right for symmetry reasons) and rows to the table.
This is my solution, though they are propably more elegant ways to achive this:
\documentclass{article}
\usepackage{array}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}
\begin{document}
\begin{table}
\centering
\begin{tabular}{|c P{15mm} c|c P{15mm}c|cP{15mm}c|cP{15mm}c|c P{15mm}c|}
\hline
& & & & & & & & & & & & & & \\
& & & & A & & & B & & & C & & & Capacity & \\
& & & & & & & & & &
& & & & \\
\hline
& & & & & \multicolumn{1}{|c|}{5} & & & \multicolumn{1}{|c|}{4} & & & \multicolumn{1}{|c|}{3} & & & \\ \cline{6-6} \cline{9-9} \cline{12-12}
& D & & & & & & & & & & & & 100 & \\
& & & & & & & & & & & & & & \\
\hline
& & & & & \multicolumn{1}{|c|}{8} & & & \multicolumn{1}{|c|}{4} & & & \multicolumn{1}{|c|}{3} & & & \\ \cline{6-6} \cline{9-9} \cline{12-12}
& E & & & & & & & & & & & & 300 & \\
& & & & & & & & & & & & & & \\
\hline
& & & & & \multicolumn{1}{|c|}{9} & & & \multicolumn{1}{|c|}{7} & & & \multicolumn{1}{|c|}{5} & & & \\ \cline{6-6} \cline{9-9} \cline{12-12}
& F & & & & & & & & & & & & 300 & \\
& & & & & & & & & & & & & & \\
\hline
\end{tabular}
\end{table}
\end{document}
This is the output:

Right margin in table cells aligned to the right in Latex

I have the following table in Latex
Created with this code:
\begin{table}[h]
\caption{aaaa}
\label{tab:treatments}
\centering
\scalebox{0.8}{
\begin{tabular}{l c c c}
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{Quantificazione log-dim} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\%& 5.4\\
B& A & 2.0\%& 4.6\\
C & N & 5.8\% & 4.6\\
D & N & 3.5\% & 4.26\\
E & K & 22.5\% & 3.7\\
\bottomrule\\
\end{tabular}
}
\end{table}
I would like to have the third column of percentages aligned to the right
At the same time the column label is too large. If I aligned the column to the right it would and up visually weird.
In a Word table I would align the column to the right and than add a right margin to the cells. The numbers would move toward the cell center while keeping their alignment to the right.
How do I do that in Latex? How do I add a margin to the right?
There are a number of options you can try:
Original:
Multi-row header:
Manual alignment of centred columns using \phantoms:
Manual alignment of centred columns using \eqmakeboxes (from eqparbox):
Automated alignment of centred columns using siunitx:
\documentclass{article}
\usepackage{booktabs,graphicx,makecell,siunitx,eqparbox}
\newcommand{\tabhead}{\textbf}
\begin{document}
Original:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{Quantificazione log-dim} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4 \\
B & A & 2.0\% & 4.6 \\
C & N & 5.8\% & 4.6 \\
D & N & 3.5\% & 4.26 \\
E & K & 22.5\% & 3.7 \\
\bottomrule
\end{tabular}
\bigskip
Multi-row header:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4 \\
B & A & 2.0\% & 4.6 \\
C & N & 5.8\% & 4.6 \\
D & N & 3.5\% & 4.26 \\
E & K & 22.5\% & 3.7 \\
\bottomrule
\end{tabular}
\bigskip
Manual alignment of centred columns using \verb|\phantom|s:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4\phantom{0} \\
B & A & \phantom{0}2.0\% & 4.6\phantom{0} \\
C & N & \phantom{0}5.8\% & 4.6\phantom{0} \\
D & N & \phantom{0}3.5\% & 4.26 \\
E & K & 22.5\% & 3.7\phantom{0} \\
\bottomrule
\end{tabular}
\bigskip
Manual alignment of centred columns using \verb|\eqmakebox|es:
\begin{tabular}{ l c c c }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & \eqmakebox[log][r]{99.3\%} & \eqmakebox[cint][l]{5.4} \\
B & A & \eqmakebox[log][r]{ 2.0\%} & \eqmakebox[cint][l]{4.6} \\
C & N & \eqmakebox[log][r]{ 5.8\%} & \eqmakebox[cint][l]{4.6} \\
D & N & \eqmakebox[log][r]{ 3.5\%} & \eqmakebox[cint][l]{4.26} \\
E & K & \eqmakebox[log][r]{22.5\%} & \eqmakebox[cint][l]{3.7} \\
\bottomrule
\end{tabular}
\newpage
Automated alignment of centred columns using \verb|siunitx|:
\begin{tabular}{ l c S[table-format = 2.1, table-space-text-post = \%] S[table-format = 1.2] }
\toprule
\tabhead{Por 1} & \tabhead{Por 3} & \tabhead{\makecell[b]{Quantificazione \\ log-dim}} & \tabhead{cintin\'a} \\
\midrule
A & M & 99.3\% & 5.4 \\
B & A & 2.0\% & 4.6 \\
C & N & 5.8\% & 4.6 \\
D & N & 3.5\% & 4.26 \\
E & K & 22.5\% & 3.7 \\
\bottomrule
\end{tabular}
\end{document}

How to merge cells with multirow in LaTeX?

I have the following code:
\begin{table}[ht]
\begin{center}
\begin{tabular}{ | l | l | l | l | l | l | l | }
\hline
\multirow{2}{*}{Oprava} & \multirow{2}{*}{zkratka} & \multirow{2}{*}{alg.} & chybovost \% & & & \\ \hline
& & & MAE & RSE & RMSE & RRSE \\ \hline
velikosti & vel. & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
bilateralne & bilat. & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
oprava & zkratka & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
oprava & zkratka & NBM & & & & \\ \hline
& & c4.5 & & & & \\ \hline
& & kNN & & & & \\ \hline
& & SMO & & & & \\ \hline
\end{tabular}
\end{center}
\caption{Multi-row table}
\label{tab:multicol}
\end{table}
And what I get is this:
I need to merge the cells, so that the text is not crossed out. The same for the rows with the repeated algorithms names - NBM, kNN etc ..... they should me merged and centered. Pls help.
Thanks
The solution to your problem is very simple. You should use \cline{4-7} instead of \hline. Namely \hline makes a horizontal line over the complete width of the table, while \cline{4-7} will only make a horizontal line from column 4 until 7, and thus not crossing the first ones.
To get the text in front of the repeated words centered and merged, you can use multirow{}{}, again in combination with \cline{}. More info about these commands at this link.
An example of the code is given below:
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l|l|l|l|l|}
\hline
\multirow{2}{*}{Oprave} & \multirow{2}{*}{Zkratka} & \multirow{2}{*}{Alg.} & \multicolumn{4}{l|}{Chybovost \%} \\ \cline{4-7}
& & & MAE & RSE & RMSE & RRSE \\ \hline
\multirow{4}{*}{velikosti} & \multirow{4}{*}{vel.} & NBM & & & & \\ \cline{3-7}
& & c4.5 & & & & \\ \cline{3-7}
& & kNN & & & & \\ \cline{3-7}
& & SMO & & & & \\ \hline
\multirow{4}{*}{bilateralne} & \multirow{4}{*}{bilat.} & NBM & & & & \\ \cline{3-7}
& & c4.5 & & & & \\ \cline{3-7}
& & kNN & & & & \\ \cline{3-7}
& & SMO & & & & \\ \hline
\multirow{4}{*}{oprava} & \multirow{4}{*}{zkratka} & NBM & & & & \\ \cline{3-7}
& & c4.5 & & & & \\ \cline{3-7}
& & kNN & & & & \\ \cline{3-7}
& & SMO & & & & \\ \hline
\multirow{4}{*}{oprava} & \multirow{4}{*}{zkratka} & NBM & & & & \\ \cline{3-7}
& & c4.5 & & & & \\ \cline{3-7}
& & kNN & & & & \\ \cline{3-7}
& & SMO & & & & \\ \hline
\end{tabular}
\end{table}
Resulting into:
Note: if you have struggles with table generation, or you want a more quick way to easily make tables as you would do in Excel, you can use an online table generator for Latex.
I often use http://www.tablesgenerator.com/

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).

Latex table multiple row and multiple column

I'm trying to create a table in Latex but without success. I tried different solutions but no one solves my problem.
I would like create a table like the picture below:
Can anyone show how to do this in Latex please?
One first sketch may be the following:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|c|c|c|}
\hline
\multirow{3}{*}{A} & \multicolumn{2}{c|}{User B} & %
\multicolumn{2}{c|}{User C} & \multirow{3}{*}{D}\\
\cline{2-5}
& \multicolumn{2}{c|}{Value} & \multicolumn{2}{c|}{Value} & \\
\cline{2-5}
& B1 & B2 & C1 & C2 & \\
\hline
& & & & & \\
\hline
& & & & & \\
\hline
% etc. ...
\end{tabular}
\end{document}
It produces:
Addendum:
\documentclass{article}
\usepackage{multirow}
\begin{document}
{\sffamily %
\begin{tabular}{|c|c|c|c|c|c|c|}% seven columns now, not six...
\hline
\multirow{3}{*}{A} & \multicolumn{2}{c|}{User B} & \multirow{3}{*}{X} & %
\multicolumn{2}{c|}{User C} & \multirow{3}{*}{D}\\
\cline{2-3}\cline{5-6}
& \multicolumn{2}{c|}{Value} & & \multicolumn{2}{c|}{Value} & \\
\cline{2-3}\cline{5-6}
& B1 & B2 & & C1 & C2 & \\
\hline
& & & & & & \\
\hline
& & & & & & \\
\hline
% etc. ...
\end{tabular}
}%
\end{document}
produces:
Please, critically check all the differences between the original code and this latter.
Don't forget that several LaTeX packages can help you improving style, dimensions and spacing of your table: among these, I advise you to have a look at bigstrut.
Also:
\documentclass{article}
\usepackage{multirow}
\begin{document}
\begin{tabular}{|c|c|c|c|c|c|c|}
\hline
\multirow{3}{*}{A} & \multirow{3}{*}{X} & \multicolumn{2}{c|}{User B} & \multicolumn{2}{c|}{User C} & \multirow{3}{*}{D}\\
\cline{3-6}
& & \multicolumn{2}{c|}{Value} & \multicolumn{2}{c|}{Value} & \\
\cline{3-6}
& & B1 & B2 & C1 & C2 & \\
\hline
& & & & & & \\
\hline
\end{tabular}
\end{document}
You should now be able to operate on your own further changes to the model of table.
Check out the multirow package:
http://texblog.org/2012/12/21/multi-column-and-multi-row-cells-in-latex-tables/
You have to include the library:
%multi-column
\multicolumn{number cols}{align}{text} % align: l,c,r
%multi-row
\usepackage{multirow}
\multirow{number rows}{width}{text}
Then it looks like this:
\documentclass[11pt]{article}
\usepackage{multirow}
\begin{document}
\begin{table}[ht]
\caption{Multi-column and multi-row table}
\begin{center}
\begin{tabular}{ccc}
\hline
\multicolumn{2}{c}{\multirow{2}{*}{Multi-col-row}}&X\\
\multicolumn{2}{c}{}&X\\
\hline
X&X&X\\
\hline
\end{tabular}
\end{center}
\label{tab:multicol}
\end{table}
\end{document}
note: code examples from the link provided

Resources