使用 Ruby PDF::Writer 或 Ruport 显示数学幂?

发布于 2024-07-12 23:49:00 字数 156 浏览 7 评论 0原文

有没有一种简单的方法可以在使用 Ruby PDF::Writer 库生成的 PDF 中显示权力? 我意识到我可以打印比基数稍高的指数,但是我想也许有一些更简单的方法可以做到这一点......例如某种标记。

基本上,我想干净地显示 x**-2。

提前致谢!

Is there an easy way to display powers in PDF generated using the Ruby PDF::Writer library? I realize that I can just print the exponent a bit higher than the base number, however I thought maybe there is some easier way to do this... such as a markup tag of some sort.

Basically, I want to cleanly display x**-2.

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-07-19 23:49:00

我写了一个快速算法来完成我需要做的事情。 希望它也对你有用。 唯一的要求是您使用 PDF::Writer。 下面的方法是使用 PDF::Writer 和 Ruport。 但是,如果您不使用 Ruport,则唯一需要更改的是“draw_text”方法。 将其替换为 PDF::Writer“文本”方法。

def draw_text_with_exponent(text, left, font_size)
    exponent_offset = 1
    font_size_reduction = 5

    words = text.split(" ")

    buffer = ""
    words.each() do |word|
        if (word.gsub("**", '') == word)
            buffer += word + " "
        else
            number = word.split("**")
            base = number[0]
            exponent = number[1]

            buffer += base
            draw_text(buffer, :left => left, :font_size => font_size)
            left += pdf_writer.text_line_width(buffer, font_size)

            pdf_writer.y+=exponent_offset
            draw_text(exponent, :left => left, :font_size => font_size - font_size_reduction)
            left += pdf_writer.text_line_width(exponent, font_size)
            buffer = ""
            pdf_writer.y-=exponent_offset
        end
    end

    if (buffer.length > 0)
        draw_text(buffer, :left => left, :font_size => font_size)
    end
end

这是一个调用示例:

draw_text_with_exponent("The numbers are x**2 and y**3 ok?", 50, 11)

如果您对此方法有任何疑问或者您找到了我原来问题的答案,请告诉我。

-J.P

I wrote up a quick algorithm to do what I need to do. Hopefully it will work for you as well. The only requirement is that you use PDF::Writer. The method below is using PDF::Writer and Ruport. However, if you aren't using Ruport, the only thing you need to change is the "draw_text" method. Substitute it with the PDF::Writer "text" method.

def draw_text_with_exponent(text, left, font_size)
    exponent_offset = 1
    font_size_reduction = 5

    words = text.split(" ")

    buffer = ""
    words.each() do |word|
        if (word.gsub("**", '') == word)
            buffer += word + " "
        else
            number = word.split("**")
            base = number[0]
            exponent = number[1]

            buffer += base
            draw_text(buffer, :left => left, :font_size => font_size)
            left += pdf_writer.text_line_width(buffer, font_size)

            pdf_writer.y+=exponent_offset
            draw_text(exponent, :left => left, :font_size => font_size - font_size_reduction)
            left += pdf_writer.text_line_width(exponent, font_size)
            buffer = ""
            pdf_writer.y-=exponent_offset
        end
    end

    if (buffer.length > 0)
        draw_text(buffer, :left => left, :font_size => font_size)
    end
end

Here is an example call:

draw_text_with_exponent("The numbers are x**2 and y**3 ok?", 50, 11)

Let me know if you have any trouble with this method or you find an answer to my original question.

-JP

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文