A common way to compare computed numbers using a "new algorithm" to a reference solution is by indicating the correct (or better said, in common) digits of the two numbers. This indication could be a change of font (italic or bold) or a change of font/background colour. What would be the LaTeX way to achieve this? See example in screenshot.

The comparison should be done by numerically using floor(-log10(abs(a-b))) ) and not on the display of the digits themselves (see comment of @DavidCarlisle)

I use the siunitx package's command \num{} to parse, format and align these numbers in a table, using the S column type (one column for the reference, one column for the newly computed values). So it would be cool if I could keep doing that, relying on siunitx.

I guess a solution could be to declare a new command, derived from \num{} that takes two numbers, the reference and the one to be compared to (could be the same to force the reference value to be formatted in bold, for example). But this is way beyond my LaTeX skills...


  • 2
    isn't it better to round then compare numerically rather than compare digits? with a reference of 3.999 then 4.000 is better answer than 3.92 even though it has no digits in common with the reference. (siunitx can do the rounding) Jun 3, 2021 at 8:02
  • Please share a MWE.
    – NBur
    Jun 3, 2021 at 8:10
  • 1
    @NBur, I'm sorry, I can't. I have no idea to get started... The screenshot with the example of what I want to achieve is the best I can do :-(
    – GertVdE
    Jun 3, 2021 at 9:01
  • You might check out \StrCompare from the xstring package. Jun 3, 2021 at 12:55


You must log in to answer this question.

Browse other questions tagged .