TeX 中的浮点格式

TeX 中的浮点格式

很久以后,我接触到了 TeX,并发现了一个在传统语言中看起来很简单但在 TeX 中却很难解决的问题。

我们有一个超出我们控制范围的系统,它为我们提供通用美国格式的 TeX 数字输入,即小数部分用点分隔符,千位用逗号分隔。我们想使用fp包裹但它实际上不支持此输入。此外,使用\numprint似乎对我们来说不起作用。

在将 TeX 中的数字传递给进一步计算之前,是否有任何简单的方法可以去掉数字中的逗号千位分隔符fp?使用 after 很容易将数字转换回来numprint

我们可以删除一个逗号,但是有没有简单的方法可以删除所有逗号?

答案1

要从,字符串中删除所有内容,可以使用以下命令

\makeatletter
\newcommand{\removecommas}[1]
  {\edef#1{\expandafter\removecommas@#1,\relax}}
\def\removecommas@#1,#2{%
  #1%
  \ifx#2\relax
    \expandafter\removecommas@@
  \fi
  \removecommas@#2}
\def\removecommas@@#1#2{}
\makeatother

% example:
\def\MyFloatingPoint{1,000,000.00}
\removecommas{\MyFloatingPoint}
\typeout{\MyFloatingPoint} % prints "1000000.00" in the log

另一个解决方案是使用expl3包。

\usepackage{expl3}
\def\MyFloatingPoint{1,000,000.00}
\ExplSyntaxOn
\tl_remove_all:Nn \MyFloatingPoint { , }
\ExplSyntaxOff
\typeout{\MyFloatingPoint} % prints "1000000.00" in the log

这里\ExplSyntaxOn和是和\ExplSyntaxOff的类似物,它们将和变成字母,并告诉 TeX 忽略空格。 expl3 包还提供了操作浮点数的方法,可以作为的替代方案。\makeatletter\makeatother_:fp

另外,您可能对使用siunitx而不是感兴趣numprint。 它的功能更强大(尽管从未使用过它们中的任何一个)。

答案2

如果您正在执行简单的浮点运算,我认为您可以使用 pst-fp。我在这个 tex 文件中使用了它。

https://github.com/camilz/pstricks-art/blob/master/roots.tex

它默认与点一起工作。

查看 pst-news 并搜索 \pstFPadd、\pstFPmul、\pstPFdiv 等命令

http://ctan.math.utah.edu/ctan/tex-archive/graphics/pstricks/base/doc/pst-news10.pdf

答案3

您可以在 Vim 中打开 TeX 文件并输入以下内容:

:%s/,//g

这将查找并替换所有逗号,从而将其删除。末尾的 g 表示“全局”,因此,如果希望保留任何逗号,请使用 cg 而不是 g 在每个逗号前进行检查。

相关内容