Math & ScienceAlignment glitch with siunitx numbers

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Alignment glitch with siunitx numbers

Post by Cham »

In the following MWE example, I would like to align all the numbers with their \pm symbol :

Code: Select all

\documentclass[10pt,letterpaper]{book}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage[separate-uncertainty=true]{siunitx}
\sisetup{output-decimal-marker = {,}}
\usepackage{icomma}
\usepackage{tensor}

\begin{document}

	\begin{alignat*}{2}
		\tensor{\mathcal{N}}{_1} &= \SI{0,03+-0,04}{\mA}, \\
		\tensor{\mathcal{N}}{_2} &= \SI{-0,02+-0,05}{\mA}, \\
		\tensor{\mathcal{N}}{_3} &= \SI{0,01+-0,02}{\mA}, \\
		\tensor{\mathcal{N}}{_4} &= \SI{-0,04+-0,03}{\mA}, \\
		\tensor{\mathcal{M}}{_1} &= \SI{0,003+-0,004}{\V}, \\
		\tensor{\mathcal{M}}{_2} &= \SI{0,06+-0,09}{\V}, \\
		\tensor{\mathcal{M}}{_3} &= \SI{-0,08+-0,05}{\V}.
	\end{alignat*}

\end{document}
Preview of what this code is doing :
numbers.jpg
numbers.jpg (22.66 KiB) Viewed 4577 times
How can I align the numbers on their +- sign (red line) ? I tried adding commands like [cmd]\phantom{-}[/cmd], but it doesn't work well.

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

mas
Posts: 226
Joined: Thu Dec 04, 2008 4:39 am

Alignment glitch with siunitx numbers

Post by mas »

Are you articular about using {alignat}? I feel {tabular} with siunitx would better serve you.

Using {alignat}, I split the quantities into more columns which gives what you want.

Code: Select all

\documentclass[10pt,letterpaper]{book}
\usepackage{lmodern}
\usepackage{amsmath}
\usepackage[separate-uncertainty=true]{siunitx}
\sisetup{output-decimal-marker = {,}}
\usepackage{icomma}
\usepackage{tensor}
 
\begin{document}
 
\begin{alignat*}{4}
  \tensor{\mathcal{N}}{_1} &= &\num{ 0,03} &\,\pm\,& &\SI{0,04}{\mA}, \\
  \tensor{\mathcal{N}}{_2} &= &\num{-0,02} &\,\pm\,& &\SI{0,05}{\mA}, \\
  \tensor{\mathcal{N}}{_3} &= &\num{ 0,01} &\,\pm\,& &\SI{0,02}{\mA}, \\
  \tensor{\mathcal{N}}{_4} &= &\num{-0,04} &\,\pm\,& &\SI{0,03}{\mA}, \\
  \tensor{\mathcal{M}}{_1} &= &\num{0,003} &\,\pm\,& &\SI{0,004}{\V}, \\
  \tensor{\mathcal{M}}{_2} &= &\num{ 0,06} &\,\pm\,& &\SI{0,09}{\V}, \\
  \tensor{\mathcal{M}}{_3} &= &\num{-0,08} &\,\pm\,& &\SI{0,05}{\V}.
\end{alignat*}
 
\end{document}
The output
x.png
x.png (6.93 KiB) Viewed 4566 times

OS: Debian/GNU Linux; LaTeX System : TeXLive; Editor : Vim
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Alignment glitch with siunitx numbers

Post by Cham »

Hmm, the spacings are looking a bit weird. Maybe I should align the units instead of the +- ? Or the commas of the first values (not their uncertainty)?? How should I do this ?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Alignment glitch with siunitx numbers

Post by Stefan Kottwitz »

I would align at relations, operators, and the decimal point (or fill up with zeroes at the right and align the numbers at the right then).

Stefan
LaTeX.org admin
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Alignment glitch with siunitx numbers

Post by Cham »

Stefan, can you be more specific ? Your code version, please ?
Post Reply