Graphics, Figures & TablesA simple Problem with an Excel-like table

Information and discussion about graphics, figures & tables in LaTeX documents.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

A simple Problem with an Excel-like table

Post by Cham »

I'm building a large table, which needs to be of Excel type. It is working great, except that I have a small problem. Here's a MWE showing the table (a bit simplified) :

Code: Select all

\documentclass[10pt,oneside]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{microtype}
\usepackage[left=0.75in,right=0.75in,top=0.5in,bottom=0.5in,includehead,includefoot]{geometry}
\usepackage{amsmath}
\usepackage{tensor}
\usepackage{hhline}

\usepackage[separate-uncertainty=true]{siunitx}
\sisetup{output-decimal-marker = {,}}

\begin{document}

\section{Title}

\begin{center}
%\begin{minipage}{0.925\linewidth}
	\renewcommand{\arraystretch}{1.5}
	\begin{tabular}{|c|r|c|S|S|S|S|}
	\hline
	\multicolumn{7}{|c|}{Table title}                                                                                                                                               \\ \hline
	$t$ & $x$ & $\tensor{t}{_{\text{c}}}$ & $\tensor{v}{_x}$ & $\Delta\tensor{v}{_x}$ & $\tensor{v}{_{\text{moy},\, x}}$ & $\Delta\tensor{v}{_{\text{moy},\, x}}$ \\ \hline
	$\si{\s}$ & $\si{\cm}$ & $\si{\s}$ & \multicolumn{2}{c|}{$\si{\m/\s}$} & \multicolumn{2}{c|}{$\si{\m/\s}$}                                         \\ \hline
	– & 0,1 & – & \multicolumn{2}{c|}{–} & \multicolumn{2}{c|}{–} \\ \hhline{|=|=|=|==|==|}
	0,0000 & 0,0 & 0,0191 & 1,31 & 0,05 & – & – \\ \hline
	0,0382 & 5,0 & 0,0535 & 1,64 & 0,07 & 1,31 & 0,03 \\ \hline
	0,0687 & 10,0 & 0,0818 & 1,92 & 0,08 & 1,46 & 0,01 \\ \hline
	0,0948 & 15,0 & 0,1064 & 2,16 & 0,09 & 1,58 & 0,01 \\ \hline
	0,1180 & 20,0 & 0,1285 & 2,4 & 0,1 & 1,695 & 0,008 \\ \hline
	0,1390 & 25,0 & 0,1487 & 2,6 & 0,1 & 1,799 & 0,007 \\ \hline
	0,1583 & 30,0 & – & – & – & 1,895 & 0,006 \\ \hline
	\end{tabular} \\[10pt]
	\renewcommand{\arraystretch}{1.5}
	\begin{tabular}{|l|}
	\hline
	\multicolumn{1}{|c|}{Légende} \\ \hline
	$t$ is time. \\
	$x$ is position. \\
	$\tensor{t}{_{\text{c}}}$ is a bla blabla bla bla blablabla blabla bla blabla bla. \\
	$\tensor{v}{_x}$ is a bla blabla blablabla bla bla $\tensor{t}{_{\text{c}}}$ bla bla blabla blablabla bla. \\
	$\tensor{v}{_{\text{moy},\, x}}$ is bla bla blablabla blabla blabla bla blabla $t = 0$ bla bla $t$. \\ \hline
	\end{tabular}
%\end{minipage}
\end{center}


\end{document}
Here's a preview of it :
table.jpg
table.jpg (67.47 KiB) Viewed 4336 times
Please, don't ask why I need to show the data that way. It's a complicated matter and it's off-topic anyway.

Now, how can I have ALL the number columns of the same width ?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

A simple Problem with an Excel-like table

Post by Cham »

I may have found a solution, from this page :
http://tex.stackexchange.com/questions/ ... n-extend-t

I just have to paste the command \newcolumntype{C}{>{\centering\arraybackslash}p{3.5em}}, before the table, and use "C" instead of "c".

Now, I'm wondering if we could align the numbers by their comma, using a similar trick.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10324
Joined: Mon Mar 10, 2008 9:44 pm

A simple Problem with an Excel-like table

Post by Stefan Kottwitz »

Nice that you found it. We had this column type thingy already many times, such as in
and more, google said.

But I think you don't need centering, you need alignment. dcolumn helps, or rccol, but siunitx is the most modern package providing it.

Example:

Code: Select all

% Stefan Kottwitz, LaTeX Cookbook, Packt Publishing, 2015
% Chapter 5, Designing Tables
% Aligning numeric data
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{chemformula}
%\sisetup{table-figures-decimal  = 5,
%         table-figures-integer  = 2,
%         table-number-alignment = right}
%\sisetup{table-format = 2.5,
%         table-number-alignment = right}
\begin{document}
  \begin{tabular}{lSS}
    \toprule
              & {atomic mass} & {total mass} \\
    \midrule
    \ch{C}    &    12.011     & 12.011       \\
    \ch{H}    &     1.00794   &  6.04764     \\
    \ch{C2H6} &               & 30.06964     \\
    \bottomrule
  \end{tabular}
\end{document}
Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Re: A simple Problem with an Excel-like table

Post by Cham »

Thanks Stefan,


but how do you set all the number columns of the same width, while aligning the numbers on their comma ?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

A simple Problem with an Excel-like table

Post by Johannes_B »

You want to have the good, the bad and the ugly all in one place. ust like in real life, this leads to tension and fights. You need a real manual crutch to get what you want.

Code: Select all

% Stefan Kottwitz, LaTeX Cookbook, Packt Publishing, 2015
% modified Johannes_B 2016
% Chapter 5, Designing Tables
% Aligning numeric data
\documentclass{article}
\usepackage{booktabs}
\usepackage{siunitx}
\usepackage{chemformula}
    %\sisetup{table-figures-decimal  = 5,
    %         table-figures-integer  = 2,
    %         table-number-alignment = right}
    %\sisetup{table-format = 2.5,
    %         table-number-alignment = right}
\begin{document}
\begin{tabular}{|l|S|S|}
	\toprule
	\multicolumn{1}{p{.3\textwidth}}{}& \multicolumn{1}{p{.3\textwidth}}{atomic mass} & \multicolumn{1}{p{.3\textwidth}}{\centering total mass} \\
	\midrule
	\ch{C}    &    12.011     & 12.011       \\
	\ch{H}    &     1.00794   &  6.04764     \\
	\ch{C2H6} &               & 30.06964     \\
	\bottomrule
\end{tabular}
\end{document}

Suggestion: Make a few of the tables as shown in previous posts and convert them to something that would be published in a journal, i.e. no vertical rules and getting rid of all that mess. Show it to the guys in charge and just say something nice. This might be a simpler approach and long-time more educational to your students than first whacking LaTeXs legs to build in an extra knee and build a crutch around all it. It is really hacky.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

A simple Problem with an Excel-like table

Post by Cham »

Johannes_B wrote: Suggestion: Make a few of the tables as shown in previous posts and convert them to something that would be published in a journal, i.e. no vertical rules and getting rid of all that mess. Show it to the guys in charge and just say something nice. This might be a simpler approach and long-time more educational to your students than first whacking LaTeXs legs to build in an extra knee and build a crutch around all it. It is really hacky.
I'm afraid this is not possible. It's not just a matter of a guy in charge. It is an institutional thing, related to the use of Excel, not LaTeX. I just need to show the students how to use Excel and make their tables of data in their physics lab, and I'm writing their labs protocols documents with LaTeX, instead of Excel. So I don't have a choice here.

This previous table example is extremely ugly, and is not of an Excel type. The lines do not close, and the spacing is way exagerated.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: A simple Problem with an Excel-like table

Post by Johannes_B »

I just took Stefan's example and showed how to fix the width of the cells, using vertical lines for better visualization. You already had all the rest, don't you?
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

A simple Problem with an Excel-like table

Post by Cham »

Sorry for the misunderstanding.

The only thing missing in my code is to align all the numbers to their comma, while having the same column width for all the numbers. I don't know how to change the code line
\newcolumntype{C}{>{\centering\arraybackslash}p{3.5em}},
except of course of adjusting the 3.5em. But how to get the numbers aligned to their comma ?
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

A simple Problem with an Excel-like table

Post by Johannes_B »

That is shown in Stefans post, in mine and in your own. You already do that. I am confused.

Code: Select all

\documentclass[10pt,oneside]{scrartcl}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[french]{babel}
\usepackage{microtype}
\usepackage[left=0.75in,right=0.75in,top=0.5in,bottom=0.5in,includehead,includefoot]{geometry}
\usepackage{amsmath}
\usepackage{showframe}
\usepackage{hhline}

\usepackage[separate-uncertainty=true]{siunitx}
\sisetup{output-decimal-marker = {,}}

\begin{document}

\section{Title}

\begin{center}
    %\begin{minipage}{0.925\linewidth}
	\renewcommand{\arraystretch}{1.5}
	\begin{tabular}{|c|r|c|S|S|S|S|}
		\hline
		\multicolumn{7}{|c|}{Table title}\\ \hline
		\multicolumn{1}{|>{\centering}p{.1\linewidth}|}{$t$} & 
		\multicolumn{1}{>{\centering}p{.1\linewidth}|}{$x$} & 
		\multicolumn{1}{>{\centering}p{.1\linewidth}|}{$t_{\text{c}}$} & 
		\multicolumn{1}{>{\centering}p{.1\linewidth}|}{$v{_x}$} & 
		\multicolumn{1}{>{\centering}p{.1\linewidth}|}{$\Delta v{_x}$} & 
		\multicolumn{1}{>{\centering}p{.1\linewidth}|}{$v_{\text{moy},\, x}$} & 
		\multicolumn{1}{>{\centering}p{.1\linewidth}|}{$\Delta v_{\text{moy},\, x}$} \\ \hline
		$\si{\s}$ & $\si{\cm}$ & $\si{\s}$ & \multicolumn{2}{c|}{$\si{\m/\s}$} & \multicolumn{2}{c|}{$\si{\m/\s}$}\\ \hline
		– & 0,1 & – & \multicolumn{2}{c|}{–} & \multicolumn{2}{c|}{–} \\ \hhline{|=|=|=|==|==|}
		0,0000 & 0,0 & 0,0191 & 1,31 & 0,05 & – & – \\ \hline
		0,0382 & 5,0 & 0,0535 & 1,64 & 0,07 & 1,31 & 0,03 \\ \hline
		0,0687 & 10,0 & 0,0818 & 1,92 & 0,08 & 1,46 & 0,01 \\ \hline
		0,0948 & 15,0 & 0,1064 & 2,16 & 0,09 & 1,58 & 0,01 \\ \hline
		0,1180 & 20,0 & 0,1285 & 2,4 & 0,1 & 1,695 & 0,008 \\ \hline
		0,1390 & 25,0 & 0,1487 & 2,6 & 0,1 & 1,799 & 0,007 \\ \hline
		0,1583 & 30,0 & – & – & – & 1,895 & 0,006 \\ \hline
		\hline
		\multicolumn{7}{|l|}{Légende} \\ \hline
		\multicolumn{7}{|l|}{\parbox{.8\linewidth}{
			\vspace*{1ex}
		$t$ is time. \\
		$x$ is position. \\
		${t}{_{\text{c}}}$ is a bla blabla bla bla blablabla blabla bla blabla bla. \\
		${v}{_x}$ is a bla blabla blablabla bla bla ${t}{_{\text{c}}}$ bla bla blabla blablabla bla. \\
		${v}{_{\text{moy},\, x}}$ is bla bla blablabla blabla blabla bla blabla $t = 0$ bla bla $t$. \\[-.5\normalbaselineskip]
	}
	}
		\\ \hline
	\end{tabular}
%\end{minipage}
\end{center}


\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

A simple Problem with an Excel-like table

Post by Cham »

Oh ! I missed something somewhere then. Thanks. I'll check my code.

EDIT : the columns are too wide in the previous code.

Also, the problem with this table is that the comma are all centered, which makes the spacements looking horrible.

Excel can align the numbers by their comma, while having some nice spacements around the numbers in their cell (comma isn't centered in the cell), with all columns of equal width.
Post Reply