使用 LaTeX 生成公式的笔记
Notes on using LaTeX to generate formulae

原始链接: https://eli.thegreenplace.net/2025/notes-on-using-latex-to-generate-formulae/

## LaTeX 数学渲染:总结 本文详细介绍了一种使用 LaTeX(主要在 Linux 上)渲染数学公式和文档的工作流程。作者主要将 LaTeX 用于两个目的:创建包含大量数学公式的博客文章(用 reStructuredText 编写)和编写个人数学笔记。 **编辑与转换:** TeXstudio 因其便捷的预览功能和与底层 LaTeX 工具的透明性而被选为本地编辑的首选。Pandoc 是将 LaTeX 转换为其他格式的关键,特别是转换为 reStructuredText 以用于博客文章——简化了合并复杂公式的过程。 **独立公式/图表:** LaTeX 允许将单个公式或 TikZ 图表渲染为图像。这涉及将 `.tex` 文件(使用 `pdflatex`、`dvisvgm` 或 `dvipng`)编译为 PDF、SVG 或 PNG 格式。像 `latexmk` 这样的工具可以提供实时预览更新。 **Docker 选项:** 对于那些不想直接安装工具的用户,`texlive` Docker 镜像提供了一种便捷的替代方案。 **文本集成:** 在文本中嵌入公式时,了解公式的高度和深度(从 `dvisvgm` 等工具获得)对于正确的对齐和视觉呈现至关重要。这些值用于设置作者博客中的图像元素样式。

Hacker News 新闻 | 过去 | 评论 | 提问 | 展示 | 工作 | 提交 登录 使用 LaTeX 生成公式的笔记 (thegreenplace.net) 15 分,ibobev 发表于 1 天前 | 隐藏 | 过去 | 收藏 | 2 条评论 sxp 发表于 1 天前 [–] 另一种选择是使用 MathJax 和在线编辑器将 LaTeX 转换为 SVG。 例如,https://latexeditor.lagrida.com/ 如果你想将其与格式化文本或 HTML 混合,Markdeep 是我首选的文档系统:https://casual-effects.com/markdeep/features.md.html#embedde... 回复 abdullahkhalids 发表于 1 天前 | 父评论 [–] 在 MacOS 上,可以使用 LaTeXit [1]。它是一个用于创建 LaTeX 公式图片的 GUI。具有历史记录功能,并可以导出为多种格式。它作为 TeX Live 的一部分提供。[1] https://pierre.chachatelier.fr/latexit/ 回复 考虑申请 YC 的 2026 年冬季批次!申请截止日期为 11 月 10 日 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请 YC | 联系方式 搜索:
相关文章

原文

This post collects some notes on using LaTeX to render mathematical documents and formulae, mostly focused on a Linux machine. For background, I typically use LaTeX for one of two (related) purposes:

  1. Render math for my blog posts, which are usually written using reStructuredText. This sometimes includes diagrams generated using TikZ.
  2. Write personal (unpublished) notes on math-y subjects entirely in LaTeX. These are typically short (up to 10-20 pages), single-subject booklets.

I don't currently use LaTeX for either precise typesetting or for authoring very large, book-sized documents.

Editing

For day-to-day authoring, I find TeXstudio to be excellent. It has everything I need for local editing with a convenient preview window. I really like that TeXstudio doesn't hide the fact that it's just a graphical veneer on top of command-line LaTeX tooling, and lets you examine what it's doing through logs.

Note that web-based solutions like Overleaf exist; I can see myself using that, especially if collaborating with others or having to author LaTeX from a diverse set of computers and OSes, but for local editing of git-backed text files, TeXstudio is great.

Converting to other formats with pandoc

pandoc is very capable for converting documents from LaTeX to other formats. Recently I find that it's easier to write math-heavy blog posts in LaTeX, and then convert them to reStructuredText with pandoc.

For example, the recent post on Hilbert spaces was written like this and then converted using this command:

$ pandoc -f latex -s -t rst hilbert.tex

The resulting reStructuredText is very readable and requires very little tweaking before final publishing. pandoc supports many formats, so if you use Markdown or something else, it should work similarly well.

Rendering standalone formulae or diagrams

A useful feature of LaTeX tooling is the ability to render a specific formula in standalone mode to an image. We can write the formula into its own file (call it standaloneformula.tex):

\documentclass[preview]{standalone}
\usepackage{amsmath}
\begin{document}
\(
\int_{-\infty}^\infty e^{-x^2}\,dx=\sqrt{\pi}
\)
\end{document}

In case you were wondering, this is the Gaussian integral:

Once we have that standalone .tex file, there's a number of things we can do. First, the texlive package should be installed . Using apt:

$ sudo apt install texlive-full

Now, we can run the tools from texlive, for example pdflatex:

$ pdflatex standaloneformula.tex

This creates a PDF file that's useful for previews. To convert the .tex file to an image in SVG format, we'll use a two-step process:

$ latex standaloneformula.tex

... generates standaloneformula.dvi

$ dvisvgm standaloneformula.dvi

... generates standaloneformula.svg

If we want a PNG file instead of SVG:

$ dvipng -D 300 standaloneformula.dvi

The latexmk tool can build a .tex file into a PDF whenever the input file changes, so running:

$ latexmk -pvc standaloneformula.tex

And opening the PDF in a separate window, we can observe live refreshes of edits without having to recompile explicitly. While useful in some scenarios, I find that TeXstudio already does this well.

The same tooling flow works for TikZ diagrams! A standalone LaTeX document containing a single tikzpicture element can also be rendered to a SVG or PNG using the same exact commands.

Docker versions

If you'd rather not install all these tools directly but use Docker instead, the texlive image can be used to do the same things:

$ docker pull texlive/texlive:latest

And now we can use the same invocations, just through docker:

$ docker run --rm -u $(id -u):$(id -g) \
    -v "$PWD":/workdir -w /workdir texlive/texlive:latest \
    latex standaloneformula.tex

$ docker run --rm -u $(id -u):$(id -g) \
    -v "$PWD":/workdir -w /workdir texlive/texlive:latest \
    dvisvgm standaloneformula.dvi

Positioning of formulae embedded in text

When a formula like is embedded in text, it should be aligned properly to look good with the surrounding text. The information required to do this is emitted by tools like dvisvgm and dvipng; for example:

$ dvisvgm standaloneformula.dvi
pre-processing DVI file (format version 2)
processing page 1
  computing extents based on data set by preview package (version 14.0.6)
  width=81.267395pt, height=9.86894pt, depth=4.388947pt
  graphic size: 81.267395pt x 14.257887pt (28.562223mm x 5.011074mm)
  output written to standaloneformula.svg
1 of 1 page converted in 0.147623 seconds

Note the height=..., depth=... line in the output. The height is the total height of the formula, and depth is its height below the "baseline" (how much down it should stick out from the line). In my blog, these two are translated to attributes on the image element embedding the SVG. Height is translated to style="height: ... and depth to vertical-align: ....


联系我们 contact @ memedata.com