如何通过在两个数字之间留出空格来写数字?

如何通过在两个数字之间留出空格来写数字?

我想以 1 234 的形式写入数字 1234。我试过

\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}
\begin{document}
The number $ 1234 $ is written in the form $ 1\,234 $;

The number $ 123456789 $ is written in the form $ 123\,456\,789$.
\end{document}

我怎样才能自动完成它? 在此处输入图片描述

答案1

自动?抱歉,不行。TeX 无法读取,它只是遵循规则,在它看来,数字序列只是普通符号序列,与坐标

使用siunitx及其强大的\num命令。例如,您可以进行全局或本地设置。默认情况下,四位数字不分组,但您可以本地或全局设置行为。

当然,\sisetup通常会在序言中,这样可以确保排版一致,但您始终可以使用可选参数来\num本地覆盖设置。

\documentclass{article}
\usepackage{siunitx}

\begin{document}

\num{1234} and \num{123456789} and \num[group-minimum-digits=4]{1234} and \num{1234}

\sisetup{group-minimum-digits=4}

\num{1234} and \num{123456789}

\sisetup{group-separator={,}}

\num{1234} and \num{123456789}

\end{document}

在此处输入图片描述

答案2

试用该软件包numprint

指定分隔符,例如\npthousandsep{\,}使用 \numprint{<number>}

A

\documentclass[12pt,a4paper]{article}
\usepackage{amsmath}

\usepackage{numprint} % added <<<<<
\npthousandsep{\,}% added <<<<<

\begin{document}
The number $ 1234 $ is written in the form $ 1\,234 $;

The number $ 1234 $ is written in the form \numprint{1234};


The number $ 123456789 $ is written in the form $ 123\,456\,789$.

The number $ 123456789 $ is written in the form \numprint{123456789}.

\end{document}

答案3

只要你愿意使用 LuaLaTeX,它可以自动在数字中插入适当的空格。

\documentclass{article}

\usepackage{luacode}

% Text Mode
\begin{luacode*}
    -- Move the first and second characters ahead by 200/1000em
    local kern_value = {{ 200, 0, 200, 0 }}

    -- All possible digits
    local digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }

    -- Store 100 copies of the all digits for later slicing
    local _digits = {}
    for i=1,100 do
        table.insert(_digits, digits)
    end

    -- Generate all possible pairs of digits
    local lookup_data = {}
    for i=0,9 do
        local inner = {}
        for j=0,9 do
            inner[tostring(j)] = kern_value
        end
        lookup_data[tostring(i)] = inner
    end

    -- Generate chained rules for up to 100 consecutive digits
    local rules = {}
    for i=100,4,-1 do
        local lookups
        if i % 3 == 1 then
            lookups = { 1, false, false }
        elseif i % 3 == 2 then
            lookups = { false, 1, false }
        else
            lookups = { false, false, 1 }
        end

        table.insert(rules, {
            before = { digits },
            current = { digits, digits, digits },
            after = { table.unpack(_digits, 1, i - 4) },
            lookups = lookups
        })
    end

    -- Create the font feature
    fonts.handlers.otf.addfeature {
        name    = "digitspace",
        type    = "chainposition",
        lookups = {
            {
                type = "pair",
                data = lookup_data
            },
        },
        data = {
            rules = rules
        }
    }

    -- Enable it globally
    luaotfload.features.defaults.dflt.digitspace = true
\end{luacode*}

% Math mode
\begin{luacode*}
    -- Save some constants
    local noad_id = node.id "noad"
    local math_char_id = node.id "math_char"
    local min_byte = string.byte("0")
    local max_byte = string.byte("9")

    luatexbase.add_to_callback('pre_mlist_to_hlist_filter', function(head)
        -- Grab the "sub_mlist" if it's first
        if head.next and head.next.nucleus and head.next.nucleus.head then
            head = head.next.nucleus.head
        end

        local digits = {}

        -- Iterate over all noads
        local n = head
        while true do
            if n and
               n.id == noad_id and
               n.nucleus.id == math_char_id and
               (n.nucleus.char >= min_byte and n.nucleus.char <= max_byte)
            then
                -- We found a digit
                table.insert(digits, n)
            else
                -- End of the run of digits, so now we insert kerns
                for i=#digits-1,1,-1 do
                    local digit = digits[i]

                    if i ~= 0 and (#digits - i) % 3 == 0 then
                        local kern = node.new "kern"
                        kern.kern = tex.sp("0.2em")
                        node.insert_after(head, digit, kern)
                    end
                end

                digits = {}
            end

            if n then
                n = n.next
            else
                break
            end
        end
        return true
    end, "digitspace")
\end{luacode*}

\usepackage{fontspec}
\setmainfont{Latin Modern Roman}

\usepackage[fleqn]{amsmath}
\mathindent=0pt
\parindent=0pt

\begin{document}
    1\\12\\123\\1234\\12345\\123456\\1234567\\12345678\\123456789\\1234567890

    \begin{gather*}
    1\\12\\123\\1234\\12345\\123456\\1234567\\12345678\\123456789\\1234567890
    \end{gather*}
\end{document}

演示

代码相当混乱且效率低下,但似乎运行正常。文本模式解决方案使用字体功能,因此应该基本安全。数学模式解决方案使用低级节点遍历,这意味着可能存在一些错误。

一些已知问题:

  • 一旦激活间距代码,就无法轻易禁用它
  • 小数处理不正确

如果有兴趣的话,我或许可以清理它并将它放在 CTAN 上的包中,尽管我不确定这种方法总体上有多安全。

相关内容