Is there any way to truncate numbers inside dollar signs? I found a solution using siunitx
and \num{somenumber}
, but I have a very large output with dollar signs. To make things worse, some of the numbers are in a format like this: $\phantom{000}0.024$
so I can't even replace the dollar signs with \num (I think phantom doesn't work well with \num). So is there any workaround, to truncate the presented number inside dollar signs, to 2 digits after decimal for example? For example: 2.5678 -> 2.56
1 Answer
As long as the \phantom
s are to the left of the decimal...
\documentclass{article}
\newenvironment{specialmath}{\catcode`$=\active}{}
{\catcode`$=\active
\gdef$#1${\dollar#1.000\enddollar}
\gdef\dollar#1.#2#3#4\enddollar{\ensuremath{#1.\ifx.#200\else#2\ifx.#30\else#3\fi\fi}}
}
\begin{document}
\begin{specialmath}
$2$\par
$2.$\par
$2.3$\par
$2.35$\par
$2.357$\par
$2.3578$\par
$52.3578$\par
$\phantom{000}52.3578$
\end{specialmath}
The dollar is restored $x^2$
\end{document}
At the OP's request, here is a version that will not add a decimal if there is none in the originally presented number:
\documentclass{article}
\newenvironment{specialmath}{\catcode`$=\active}{}
{\catcode`$=\active
\gdef$#1${\dollar#1.\relax\relax\enddollar}
\gdef\dollar#1.#2#3#4\enddollar{\ensuremath{%
#1\ifx\relax#2\else%
\ifx.#2.00\else.#2\ifx.#30\else#3\fi\fi%
\fi%
}}
}
\begin{document}
\begin{specialmath}
$2$\par
$2.$\par
$2.3$\par
$2.35$\par
$2.357$\par
$2.3578$\par
$52.3578$\par
$\phantom{000}52.3578$
\end{specialmath}
The dollar is restored $x^2$
\end{document}
-
That's great. Is there a way to keep integers without a decimal? Meaning 2, will stay 2, and not 2.00.– nadavbCommented Nov 8, 2017 at 11:00
-
1@Nashh I have edited my answer to add that solution, as well. Commented Nov 8, 2017 at 11:10
\phantom{000}
is there for alignment purposes, aS
column provided bysiunitx
would allow aligning numbers on the decimal marker inside atabular
.