使用 LaTeX 重构文本中的数学

发布于 2024-09-17 06:02:56 字数 459 浏览 12 评论 0 原文

我想使用轻量级标记语言在大学课程中做笔记。

我选择的编辑器是 gedit,我找到了 reStructuredText Tools for Gedit,它将运行reStructuredText 处理器并在 gedit 的窗格中呈现 HTML。这太棒了,已经完成了 80%。

但对于我的许多课程,我需要在笔记中包含数学方程或希腊字符。虽然我对LaTeX不是很熟悉,但我的理解是它有这些能力。

如何在 reST 文档中使用 LaTeX? reST 文档是否需要处理成 LaTeX,然后渲染成 HTML,还是有更好的方法? Markdown 会让这变得更容易吗?如果需要的话我可以修改 gedit 插件。

最后,有人这样做吗?对于在纯文本编辑器中记课堂笔记还有其他建议吗?

谢谢!

I would like to use a lightweight markup language to take notes in my college classes.

My editor of choice is gedit, and I found reStructuredText Tools for Gedit, which will run the reStructuredText processor and render the HTML in a pane in gedit. This is great, and 80% of the way there.

But for many of my classes I need to include math equations or greek characters in my notes. Although I'm not very familiar with LaTeX, my understanding is that it has these capabilities.

How can I use LaTeX in a reST document? Would the reST document need to be processed into LaTeX, then that rendered into HTML, or is there a better way? Would markdown make this easier? I can modify the gedit plugin if necessary.

Lastly, does anyone do this? Any other suggestions for taking class notes in a plain text editor?

Thanks!

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

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

发布评论

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

评论(11

凉薄对峙 2024-09-24 06:02:57

LaTeX 和 ReST 是同一问题(整个文档标记)的两种解决方案,我不确定其中一个是否会高兴地生活在另一个中。

  • 首先是 LaTeX,然后是 ReST:LaTeX 需要精确定位印刷元素,以便布置方程(以及其他所有内容)。 ReST 不会将 LaTeX 输出(PostScript 或类似的)作为输入。
  • 首先是 ReST,然后是 LaTeX:您必须为 ReST 编写一个输出格式化程序来输出 LaTeX 代码,并在 ReST 中定义扩展以防止它误解您的方程或其他显式 TeX。 LaTeX 的输出仍然类似于 PostScript 或 PDF。

我见过使用 TeX 的子集生成图形方程的代码,然后将其提取到 HTML 或类似的代码中(例如,Trac 的 LaTeX Formula 宏),这可能是最好的可用解决方案。

LaTeX and ReST are two solutions to the same problem (whole-document markup), and I'm not sure that one would be happy living inside the other.

  • LaTeX first, then ReST: LaTeX needs to precisely position typographical elements in order to lay out equations (and everything else). ReST wouldn't take the LaTeX output (PostScript or similar) as input.
  • ReST first, then LaTeX: You'd have to write an output formatter for ReST that output LaTeX code, and define extensions in ReST to prevent it from misinterpreting your equations or other explicit TeX. Your output from LaTeX will still be something akin to PostScript or PDF.

I've seen code for using a subset of TeX to generate a graphic equation which is then pulled into HTML or similar (for example,Trac's LaTeX Formula macro), which may be the best available solution.

等风来 2024-09-24 06:02:57

稍微分解一下你的问题,第一个目标是将乳胶代码逐字输出(除了<,>,蚀刻的cgi转义)到从你选择的轻标记生成的html中:一旦你有了它,它可以很容易地通过 mathJax (强烈推荐)或 itex 正如 gozzilli 提到的。

首先我应该说,使用 markdown 而不是 ReST 可能会更容易,因为 ReST 使用 \ 进行转义,因此所有 LaTeX 都必须受到保护。有关使用 markdownmathJax,去 mathoverflow.net 尝试设置。

尽管如此,我会假设您确实想要 ReST (和 mathJax) ,在这种情况下有两种方法:使用库存 ReST (和繁琐的转义),或者向 docutils 添加一些 LaTeX 处理程序。

选项 1 - Stock ReST:

对于内联数学,只需逐字加上您标记后处理器正在寻找的任何内容:“$x<\pi$”。对于多行数学,您需要一个文字块,但您需要为例如 MathJaX 设置一个类属性来解析它,否则它会跳过

 标签:

.. class:: mathjax_process

::

  \begin{equation}
    x<\pi
  \end{equation}

选项 2:扩展您的 ReST 处理器

如果您愿意做一些修改来扩展您的 ReST 处理器,您可以通过定义自定义解释文本 内联乳胶的角色(例如 :latex:`x<\pi` )和用于多行数学的自定义 指令,例如

.. latex::

  \begin{equation}
    x<\pi
  \end{equation}

内联数学如果您使用 默认角色

有多种方法可以实现角色和指令。我已经(大致)得到了以下内容,它具有一个很好的功能,即(与我见过的大多数其他黑客不同)它对于没有集成后期处理的编写者来说会降级为文字 LaTex 代码。

from docutils import nodes, utils
from docutils.parsers.rst import directives, Directive 

class Latex(Directive):
    """ Latex directive to display LaTeX as escaped literal wrapped in
        <pre class="latex">. 
    """
    required_arguments = 0
    optional_arguments = 0
    has_content = True
    def run(self):
        self.assert_has_content()
        prenode=nodes.literal_block(self.block_text,
                                    '\n'.join(self.content),
                                    classes=['latex'])
        return [prenode]
directives.register_directive('latex', Latex)

def latex_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
    node = nodes.literal(rawtext,
                         '\(%s\)'%utils.unescape(text, 1),
                         classes=['latex'])
    return [node],[]
register_local_role('latex', latex_role)

上面将使用“latex”类来标记要处理的内容,因此您需要配置 mathJax或相当于寻找此类。或者,更改上面的代码以将类设置为“mathjax_process”,这是默认的 mathJax 覆盖类。

Breaking down your problem a bit, the first goal is to get the latex code output verbatim (except for cgi-escapes for <,>, etch.) into the html generated from your light markup of choice: once you have it there, it can easily be LaTex'ed by mathJax (highly recommended) or itex as mentioned by gozzilli.

First I should say that using markdown instead of ReST would probably be easier because ReST uses \ for escaping, so all LaTeX has to be protected. For an example of how nicely things work out with markdown and mathJax, go play around with the setup at mathoverflow.net.

Nevertheless, I will assume that you really want ReST (and mathJax) , in which case there are two ways ahead: use stock ReST (and cumbersome escaping), or add some LaTeX handlers to docutils.

Option 1 - Stock ReST:

For inline math, simply use verbatim plus whatever you tags your postprocessor is looking for: ``$x<\pi$``. For multi-line math, you want a literal block, but you need to set a class attribute for e.g. MathJaX to parse it, as it otherwise skips <pre> tags:

.. class:: mathjax_process

::

  \begin{equation}
    x<\pi
  \end{equation}

Option 2: Extend you ReST processor

If you are willing to do a bit of hacking to extend your ReST processor, you can get a much nicer notation for the LaTeX literals, by defining a custom interpreted text role for inline latex (e.g. :latex:`x<\pi`) and a custom directive for multi line math, e.g.

.. latex::

  \begin{equation}
    x<\pi
  \end{equation}

The inline math notation can even be shortened to `x<\pi` if you use default-role.

There are several ways to implement the role and directive. I have arrived at (roughly) the following which has the nice feature that (unlike most other hacks I have seen) it degrades to literal LaTex code for writers with no integrated post processing.

from docutils import nodes, utils
from docutils.parsers.rst import directives, Directive 

class Latex(Directive):
    """ Latex directive to display LaTeX as escaped literal wrapped in
        <pre class="latex">. 
    """
    required_arguments = 0
    optional_arguments = 0
    has_content = True
    def run(self):
        self.assert_has_content()
        prenode=nodes.literal_block(self.block_text,
                                    '\n'.join(self.content),
                                    classes=['latex'])
        return [prenode]
directives.register_directive('latex', Latex)

def latex_role(role, rawtext, text, lineno, inliner, options={}, content=[]):
    node = nodes.literal(rawtext,
                         '\(%s\)'%utils.unescape(text, 1),
                         classes=['latex'])
    return [node],[]
register_local_role('latex', latex_role)

The above will use the class "latex" to mark stuff for processing, so you'll need to configure mathJax or equivalent to look for this class. Alternatively, change the code above to set the class to "mathjax_process" which is the default mathJax override class.

情话难免假 2024-09-24 06:02:57

问题是 reST 并没有真正的插件。您可以选择可扩展的框架,例如 Sphinx;已包含 JSMath 的插件,以及 MathJax 也是可能的。此外,Sphinx 使用 LaTeX 直接构建 PDF 文档。

另一个更轻量级的解决方案包括编辑 reST 模板以包含 MathJax,以便您可以在文档中使用 MathJax 的(类似 LaTeX)语法。

但通常我只是直接使用 LaTeX 来处理任何重要的事情 - 这些轻量级标记语言的定制功能对于所有事情来说都不够全面。

The problem is that reST doesn't really have plugins. You might choose an extensible framework such as Sphinx; a plugin for JSMath is already included, and a plugin for MathJax is also possible. Additionally Sphinx makes use of LaTeX to directly build PDF documents.

Another more lightweight solution consists in editing the reST template to include MathJax so that you can just use MathJax's (LaTeX-like) syntax in the document.

But usually I just use LaTeX directly for anything serious—the customization capabilities of these lightweight markup languages just aren't comprehensive enough for everything.

烟雨凡馨 2024-09-24 06:02:57

此项目来自 Docutils FAQ 描述了一种相对容易地包含 TeX 数学的方法。您首先为解释文本定义一个角色,将方程直接传递到 LaTeX:

.. role:: raw-latex(raw)
    :format: latex

然后您可以在文本中使用该角色

The area of a circle is :raw-latex:`$\pi r^2

此项目来自 Docutils FAQ 描述了一种相对容易地包含 TeX 数学的方法。您首先为解释文本定义一个角色,将方程直接传递到 LaTeX:

.. role:: raw-latex(raw)
    :format: latex

然后您可以在文本中使用该角色

。常见问题解答条目还提到了其他一些方法来执行此操作,包括可用于渲染文本的预处理器方法。生成 HTML 时对图像进行数学运算。

This item from the Docutils FAQ describes one way to relatively easily include TeX math. You first define a role for interpreted text that passes the equation through directly to LaTeX:

.. role:: raw-latex(raw)
    :format: latex

Then you can use that role in your text

The area of a circle is :raw-latex:`$\pi r^2

This item from the Docutils FAQ describes one way to relatively easily include TeX math. You first define a role for interpreted text that passes the equation through directly to LaTeX:

.. role:: raw-latex(raw)
    :format: latex

Then you can use that role in your text

The FAQ entry also mentions a few other ways to do this, including a preprocessor approach that can be used to render the math to an image when generating HTML.

池木 2024-09-24 06:02:57

Pandoc 的 Markdown 扩展允许内联或显示 LaTeX 数学;您只需将它放在(紧邻的)美元符号之间,如下所示: $\sqrt{-1}$ ,它就会按照您的预期进行处理。我一直在使用这个功能,并且认为它一点也不实用。请参阅手册 http://johnmacfarlane.net/pandoc/README.html#math

一旦解析了 Markdown,pandoc 就可以编写 HTML、LaTeX、ReST、RTF、DocBook、ODT、手册页等。如果 LaTeX 数学上下文不涉及花哨的 LaTeX 包,它甚至可以将数学解析为 MathML,因此它立即以 HTML 或 RTF 格式呈现。 (它提供了许多用于处理非 LaTeX 文档中的 LaTeX 数学的选项。)

Pandoc 还解析 ReST(和 HTML,以及不太花哨的 LaTeX)。因此,我希望给出一个涉及 ReSt 阅读器的解决方案,但它似乎没有特殊的机制来处理数学;请参阅问题 249 的讨论 http://code.google.com/ p/pandoc/issues/detail?id=249 我再仔细研究一下看看。

我认为还没有人为 Gedit 编写出 pandoc 插件(我知道,emacs 和 TextMate 中有详细的支持)——那太棒了!

Pandoc's markdown extension permits inline or display LaTeX math; you just put it between (immediately adjoining) dollar signs like so: $\sqrt{-1}$'s and it's treated as you'd expect. I use this feature all the time, and don't think it's impractical at all. See the manual http://johnmacfarlane.net/pandoc/README.html#math

Once the markdown is parsed, pandoc can write HTML, LaTeX, ReST, RTF, DocBook, ODT, manpages etc., etc. If a LaTeX math context does not involve fancy LaTeX packages, it can even parse the math into MathML, so it renders immediately in HTML or RTF. (It supplies a number of options for dealing with LaTeX math in non-LaTeX documents.)

Pandoc also parses ReST (and HTML, and not-too-fancy LaTeX). I had thus hoped to give a solution involving the ReSt reader, but it seems to have no special machinery for dealing with math; see the discussion of issue 249 http://code.google.com/p/pandoc/issues/detail?id=249 I will study again more closely to see.

I don't think anyone has cooked up a pandoc plugin for Gedit yet (there is elaborate support in emacs and in TextMate, I know) -- that would be awesome!

杀お生予夺 2024-09-24 06:02:57

请注意,0.8 版的 docutils(解析 reStructuredText)支持 LaTeX 数学。请参阅http://article.gmane.org/gmane.text.docutils.user/ 6255 有很多例子。 “--math-output”选项的选项有:MathML、HTML、MathJax 和 LaTeX。

Note that version 0.8 of docutils (which parses reStructuredText) has support for LaTeX math. See http://article.gmane.org/gmane.text.docutils.user/6255 for many examples. The choices for the "--math-output" option are: MathML, HTML, MathJax, and LaTeX.

何必那么矫情 2024-09-24 06:02:57

使用 rst2mathml.py 代替 rst2html。

它使用乳胶数学模式扩展了 docutils。该模式的内容被转换为MathML。

Instead of rst2html use rst2mathml.py.

It extends docutils with a latex-math mode. The contents of this mode is converted to MathML.

暗藏城府 2024-09-24 06:02:57

为了扩展之前的答案,这就是我如何让 MathJaxReSTDocutils 一起使用,而不需要 LaTeX:

  1. 使用 LaTeX-我的 ReST 源中的数学样式,例如:

    雷达后向散射系数为$\sigma^0$。
    
  2. 将其包含在文件中的某处(我将其放在底部):

    <前><代码>.. raw:: html

    <脚本类型=“text/x-mathjax-config”>
    MathJax.Hub.Config({
    扩展名:[“tex2jax.js”],
    jax:[“输入/TeX”,“输出/HTML-CSS”],
    tex2jax:{
    inlineMath: [ ['$','$'], ["\\(","\\)"] ],
    显示数学:[ ['$$','$$'], ["\\[","\\]"] ],
    进程转义:true
    },
    "HTML-CSS": { availableFonts: ["TeX"] }
    });

    <脚本类型=“text/javascript”src=“http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML”>

  3. 转换为 HTML:

    rst2html 报告.rst 报告.html
    
  4. 在现代浏览器中加载 HTML。 MathJax 脚本将被下载并在查看时应用于 HTML。一切对我来说看起来都很棒!

非常感谢 http://www.thales.math.uqam.ca/ ~labbes/Sage/rst2sws/ 寻求帮助!

更新:我注意到虽然输出的 HTML 在 Firefox 中运行良好,但 MathJax 在 Chrome 中运行需要很长时间,然后会出现错误。 更改

我通过将最后一行中的脚本源从src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML"

我相信原因是 FireFox 支持 MathML,而 Chrome(实际上是 Webkit)尚不支持(至少不完全支持;您可以测试浏览器的支持 此处)。

因为我不需要 MathML,只需要 TeX,所以我使用相应的 MathJax 版本,现在它可以在 Chrome 中快速加载,没有任何错误。此外,使用 TeX 风格输入数学比使用 MathML 风格输入要快得多。

To expand on the previous answers, this is how I got MathJax working with ReST and Docutils, without needing LaTeX:

  1. Use LaTeX-style math within my ReST source, such as:

    The radar backscatter coefficient is $\sigma^0$.
    
  2. Include this somewhere within the file (I put mine at the bottom):

    .. raw:: html
    
    <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['
    
  3. Convert to HTML:

    rst2html report.rst report.html
    
  4. Load the HTML in a modern browser. The MathJax script is downloaded and applied to the HTML upon viewing. Everything looks great for me!

Many thanks to http://www.thales.math.uqam.ca/~labbes/Sage/rst2sws/ for the help!

UPDATE: I noticed that while the output HTML works fine in Firefox, it takes a long time for MathJax to operate in Chrome, and then it gives an error. I fixed it by changing the script source in the last line from

src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

to

src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML".

I believe the reason is while FireFox supports MathML, Chrome (Webkit, really) doesn't yet (at least not fully; you can test your browser's support here).

Because I have no need for MathML, just TeX, I use the corresponding MathJax version and it now loads quickly in Chrome without any errors. Besides, it would be much faster for you to type math TeX-style than MathML-style.

,'
  • Convert to HTML:

    
    
  • Load the HTML in a modern browser. The MathJax script is downloaded and applied to the HTML upon viewing. Everything looks great for me!

  • Many thanks to http://www.thales.math.uqam.ca/~labbes/Sage/rst2sws/ for the help!

    UPDATE: I noticed that while the output HTML works fine in Firefox, it takes a long time for MathJax to operate in Chrome, and then it gives an error. I fixed it by changing the script source in the last line from

    src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

    to

    src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML".

    I believe the reason is while FireFox supports MathML, Chrome (Webkit, really) doesn't yet (at least not fully; you can test your browser's support here).

    Because I have no need for MathML, just TeX, I use the corresponding MathJax version and it now loads quickly in Chrome without any errors. Besides, it would be much faster for you to type math TeX-style than MathML-style.

    ], ["\\(","\\)"] ], displayMath: [ ['$','$'], ["\\[","\\]"] ], processEscapes: true }, "HTML-CSS": { availableFonts: ["TeX"] } }); </script> <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
  • Convert to HTML:

    
    
  • Load the HTML in a modern browser. The MathJax script is downloaded and applied to the HTML upon viewing. Everything looks great for me!

  • Many thanks to http://www.thales.math.uqam.ca/~labbes/Sage/rst2sws/ for the help!

    UPDATE: I noticed that while the output HTML works fine in Firefox, it takes a long time for MathJax to operate in Chrome, and then it gives an error. I fixed it by changing the script source in the last line from

    src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"

    to

    src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML".

    I believe the reason is while FireFox supports MathML, Chrome (Webkit, really) doesn't yet (at least not fully; you can test your browser's support here).

    Because I have no need for MathML, just TeX, I use the corresponding MathJax version and it now loads quickly in Chrome without any errors. Besides, it would be much faster for you to type math TeX-style than MathML-style.

    不打扰别人 2024-09-24 06:02:56

    从版本 0.8 开始,它就得到了原生支持:您不应该再使用任何解决方法。语法也非常简单。它与乳胶数学相同,但没有封闭的 $$

    因此您可以简单地为数学块编写以下内容

    .. math::
    
       \frac{ \sum_{t=0}^{N}f(t,k) }{N}
    

    或者如果您想内联编写,您可以使用它:

    :math:`\frac{ \sum_{t=0}^{N}f(t,k) }{N}`
    

    注意那里的分隔反引号。


    注意:如果您在第一个文档字符串中使用乳胶表示法,则需要使用数学元素的双反斜杠对反斜杠进行双重转义,因此它是 \\frac 而不是 \压裂

    Since version 0.8 it is supported natively: You shouldn't use any workaround anymore. The syntax is also very simple. It is the same as latex math, but without the enclosing $$

    So you can simply write the following for a math block

    .. math::
    
       \frac{ \sum_{t=0}^{N}f(t,k) }{N}
    

    Or if you want to write inline you can use this:

    :math:`\frac{ \sum_{t=0}^{N}f(t,k) }{N}`
    

    notice the delimiting backticks there.


    Note: if you're using latex notation in rst doc-strings, you will need to double escape the backslash using a double-backslash for the math elements, so then it's \\frac and not \frac

    給妳壹絲溫柔 2024-09-24 06:02:56

    对“数学”角色和指令有原生支持
    (使用 LaTex 输入语法)自版本 0.8 (2011-07-07) 起。

    There is native support for a "math" role and directive
    (using LaTex input syntax) since Release 0.8 (2011-07-07).

    书信已泛黄 2024-09-24 06:02:56

    这就是我所做的:

    1. 在 reST 文档的开头添加数学角色:

      .. 角色:: raw-math(raw)
          :format: Latex html
    2. 像这样写下你的数学

      :raw-math:`$ \frac{s}{\sqrt{N}} $`

      (如果您希望将其放在块中,请使用 $$ ... $$;如果您希望将其内联,请使用 $ ... $。)

    3. < p>生成如下 html 输出:

      rst2html --stylesheet=/path/to/my_beautiful_stylesheet.css my_file.rst \
      |整洁-q | /path/to/itex2MML > my_file.xhtml

      这会使用 rst2html 生成 html,使用 tidy 进行整理,然后使用 itex2MML 将乳胶数学转换为 MathML,并在 xhtml 上输出文件。

    注意:

    1. 样式表是可选的
    2. 对于itex2MML,请前往此处
    3. html 文件的扩展名应该是 xhtml 或 xml,否则数学将不会显示在浏览器中。

    This is what I do:

    1. Add a role for math at the beginning of your reST document:

      .. role:: raw-math(raw)
          :format: latex html
    2. Write your maths like

      :raw-math:`$ \frac{s}{\sqrt{N}} $`

      (use $$ ... $$ if you want it in a block, or $ ... $ if you want it inline.)

    3. Generate html output like this:

      rst2html --stylesheet=/path/to/my_beautiful_stylesheet.css my_file.rst \
      | tidy -q | /path/to/itex2MML > my_file.xhtml

      This generates the html with rst2html, tidies up with tidy and then converts the latex maths into MathML with itex2MML, outputting on an xhtml file.

    Notes:

    1. Style sheet is optional
    2. For itex2MML go here.
    3. The extension of your html file should be xhtml or xml, otherwise the maths won't show in your browser.
    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文