Is it possible to have automatic rounding of numbers in a table?
Say that I have this, made in the usual way with a tabular
environment:
pi | 3.14592654
e | 2.7182818284
phi | 1.6180339887
Can I control how many significant digits are shown without truncating the numbers manually myself?
So for example, if I need four significant digits, the numbers should appear as
pi | 3.1459
e | 2.7183
phi | 1.6180
Bonus points for actually rounding the numbers according to the usual rules without simply truncating them.
I understand this is probably non-trivial, so here's a relaxed version that will also work in my circumstances (all numbers have one and the same precision and character count):
(How) Can I control how many characters are rendered in a column of a table?
So if I define the text in the columns as:
\begin{tabular} % some black magic column definition
text & alongword\\
text & anotherlongword\\
text & morelongwords\\
\end{tabular}
I'd like to get something like:
text along
text anoth
text morel
I know there are non-TeX ways to do this, and I will probably resolve to writing a script if all else fails, but I'm curious whether there's a way to do it solely with TeX & friends.
Best Answer
For numbers:
There are some packages, like
siunitx
and math engine oftikz
(especially inpgfplotstable
), which can used to format numbers in LaTeX.numprint
may be the simplest one, and can be easily used in tabulars.numprint
package prividesn
andN
column types for numbers in tabular, and\nprounddigits
for set the precision. An example:Please read the manual for more options.
For common characters, @TH has showed some TeX trick. However, you can use
xstring
package or something else to do the dirty work. I don't know how to get full control intabular
, so here is just a incomplete solution, a mix ofxtring
and primitive TeX:Any further suggestions are welcome.