3
$\begingroup$

I am working with large tables of numbers, where the numbers can have as many as 10 decimal points. To make the work more presentable, I use

SetOptions[SelectedNotebook[], PrintPrecision -> 5]

However, I now need to copy these tables into a LaTex file. "Copy as LaTex" prints the tables well but with the full numbers, up to the 10th decimal place. Is there a way to copy these numbers to a set decimal place (i.e. up to only the 5th decimal place)?

Thanks!

$\endgroup$
1
  • $\begingroup$ Are the numbers machine numbers or extended precision numbers? If they are extended precision numbers, then it is expected that all digits are displayed. $\endgroup$
    – Carl Woll
    Feb 10, 2021 at 17:28

1 Answer 1

2
$\begingroup$

PrintPrecision is used for display in a notebook, and the conversion to TeX is happening outside the notebook. It may not be possible to use it to control the conversion to TeX. For instance, the box form in a notebook contains the full form of real numbers, even though only usually six digits are displayed.

Setting the Precision of a number will control how many digits are displayed, as the precision of a number is stored in a number and may be accessed when converting it to boxes or TeX. Maybe one of these will work.

TeXForm[tab /. x_Real :> SetPrecision[x, 5]]
TeXForm[Map[SetPrecision[#, 5] &, tab, {2}]]
\left(
\begin{array}{ccc}
>      0.66667 & 1.0513 & 1.4359 \\
>      0.95238 & 1.3370 & 1.7216 \\
>      1.2381 & 1.6227 & 2.0073 \\
>      1.5238 & 1.9084 & 2.2930 \\
>      1.8095 & 2.1941 & 2.5788 \\
>     \end{array}
\right)

Alternatively, you can leave off the TeXForm and convert the output cell with "Copy as LaTeX".

$\endgroup$
3
  • $\begingroup$ Brain is still sleepy. SetPrecision[tab, 5] will work on a table of real numbers. $\endgroup$
    – Michael E2
    Feb 10, 2021 at 15:07
  • $\begingroup$ Thank you so much! Option 2 worked perfectly, even with my imaginary numbers. ^-^ $\endgroup$ Feb 10, 2021 at 19:02
  • $\begingroup$ @PhysicsHobbit For real & complex values, x_Real | x_Complex :> SetPrecision[x, 5] should work. The alternatives do slightly different things. You probably wouldn't want to use SetPrecision on Integer entries and get 1.0000 instead of 1. There might be other fine tuning needed for tables, depending on what kind of entries they have. (This comment is partly for future visitors who might want to adapt one of these approaches.) $\endgroup$
    – Michael E2
    Feb 10, 2021 at 21:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.