0

I have a table, in each row, I have a list of values, now I need to change each value to its half (replace each cell value in a table with value/2).

Now, it's possible to manually calculate the halves and place them in the table, but I was wondering if there's a way to do simple math operations inside latex so the value can be replaced easily.

N.B: I'm looking for an in-place change if that's possible in latex.

\small{$\pm$ std} & 17.28 & 25.82 & 24.85 & \textbf{40.62} & 33.99 & 42.13 & 32.7  & 21.46 & 17.64 \\

2 Answers2

2

You can use collcell and xparse to change the numbers into \fpeval{round(<number>/2,2)}.

\documentclass{article}
\usepackage{xparse,xfp,collcell}

\ExplSyntaxOn
\NewDocumentCommand{\halveentry}{m}
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \regex_replace_once:nnN { ([0-9]*\.+[0-9]*) } { \c{fpeval}\cB\{round(\1/2,2)\cE\} } \l_tmpa_tl
  \tl_use:N \l_tmpa_tl
 }
\ExplSyntaxOff

\begin{document}

Original

\begin{tabular}{l *{9}{r}}
\small $\pm$ std & 17.28 & 25.82 & 24.85 & \textbf{40.62} & 33.99 & 42.13 & 32.7  & 21.46 & 17.64 \\
\end{tabular}

Halved

\begin{tabular}{l *{9}{>{\collectcell\halveentry}r<{\endcollectcell}}}
\small $\pm$ std & 17.28 & 25.82 & 24.85 & \textbf{40.62} & 33.99 & 42.13 & 32.7  & 21.46 & 17.64 \\
\end{tabular}

\end{document}

enter image description here

Use just \1/2 instead of round(\1/2,2) if you don't want rounding to two decimal digits.

egreg
  • 1,121,712
0

You can use packages like spreadtab, or calctab for simple math operations.

But if you are looking to make the changes in-place, I do not think there is any way.

  • Thanks, I knew about those, but I was actually looking for an in-place change, I just wrote a python script to replace the values currently, will accept your answer if no in-place solution is shown. – Zabir Al Nazi May 28 '20 at 07:58