技术写作与文档2019github.com
用 Python 生成图表撰写 LaTeX 论文的技巧
Tips and tricks to write LaTeX papers in with figures generated in Python
导读
这是一份 GitHub 上的 LaTeX 论文写作技巧合集,重点讲如何把 Python 生成的图表纳入论文:保存生成命令、统一图表样式、让数据与编译流程联动。对写作者的价值在于,图表是论文叙事的一部分,把图表生产流程脚本化、可复现,能避免截止日前手忙脚乱地改图。
可以直接用的写作要点
- 01
把生成每张图的脚本或命令随论文一起保存(甚至注释在 LaTeX 源码旁),保证任何一张图都能一键重跑。
- 02
在生成阶段就用统一配置(如 matplotlib 的 rcParams、seaborn 的 set)锁定字体、配色、尺寸,而不是逐图手动调整。
- 03
图表优先保存为矢量格式(SVG/PDF),后期组合、加标注在 Inkscape 等工具里完成,不必回到原始代码重画。
- 04
把数据更新和论文编译绑定(如 Makefile 或 knitr),让编译时自动带入最新数据,减少版本错位。
HN 评论精选
用 knitr/Makefile 把数据到图表的流程自动化,既统一风格又保证数据最新。
作者建议把生成图表的命令保存在 LaTeX 文件里。我最近改用 knitr,省掉了这层间接:数据直接放进论文仓库,Makefile 里写 `%.tex: graphs/%.Rnw` 再调用 knit 即可。好处正是作者说的那两点——更容易让所有图表保持统一外观,并在编译过程中自动纳入最新数据。
akshayn查看英文原文
> We also recommend to save the command used to generate a figure in the LaTeX file An approach I have adopted recently is Knitr[1], so this layer of indirection goes away. With knitr, my data goes directly into the paper repository, and then my Makefile has something like this: %.tex: graphs/%.Rnw Rscript -e "library(knitr); knit('$?')" The nice thing is exactly what the authors recommend: it's much easier to enforce a standard appearance across all the figures, and automatically incorporate more recent data into the paper as part of the compilation process. [1] https://yihui.name/knitr/
用矢量工具做后期组合与标注,用 seaborn 统一美化,能省掉大量重复劳动。
补充一点:Inkscape 对做图非常有用。一次存成 SVG,之后想导出什么格式都行,我通常从 Inkscape 导出 PDF 给 pdflatex 用。它对示意图几乎不可或缺,我也常遇到要把已有图组合起来、或加个标签的情况;Inkscape 能导入 PNG,做这些很轻松,不必回原代码重画,也不用为小改动折腾 LaTeX。对 matplotlib 生成的图,强烈推荐再加 seaborn:它是对 matplotlib 的封装,一个 import 加一个 set 命令就能美化图表,也能直接画图,并支持 pandas 的 DataFrame,用更少的力气得到更好看的结果。
abhgh查看英文原文
I'd also add that for figures Inkscape is invaluable [1]. Save as svg once, and export it as whatever later. I typically export it to PDF (from within Inkscape) for pdflatex. While its typically indispensable for schematics, I often seem to run into the use case of combining previously generated plots or figures, or adding a label/text. Since Inkscape can import pngs, this is a breeze with it. I don't have to go back to the original code to regenerate plots, or fiddle around with latex to make minor adjustments. For stuff generated via matplotlib, I'd strongly recommend seaborn as an additional library [2]. This is a wrapper over matplotlib. It can prettify plots with just an import and a 'set' command. You can, of course, use it to plot too, and for stuff doable in matplotlib using the seaborn alternative is much easier and looks better with little or no work. And they support pandas dataframes. [1] https://inkscape.org/ [2] https://seaborn.pydata.org/
让图表与正文同源(tikz/pgfplots),风格统一且高度可定制。
如果认真想做漂亮的 LaTeX 图表,我强烈推荐 tikz 和 pgf-plots。从 Python 自动生成 tikz 代码很容易(它本来就是给人读写的),图表的各个方面都能方便地定制。我用 tikz 和 pgf 生成自动报告,图表漂亮、易读,效果很成功。有兴趣的话我在 GitHub 上传了一个从两个 numpy 列表生成 XY 图的示例脚本,代码不算好,只是分享给想尝试的人。
sigurdjs查看英文原文
If you are serious about making beautiful figures in latex, I would seriously recommend using tikz and pgf-plots. It is quite easy to automatically generate tikz-code from python (after all it is supposed to be read and written by humans) and all aspects of the figure can easily be customized. I have been quite successful in generating automated reports with pretty and easily readable figures using tikz and pgf. If anyone is interested I have uploaded a sample script for generating XY-plots from two numpy lists to github. The code is by no means very good, but I just wanted to share in case anyone wants to try this approach. https://github.com/sigurdjs/python-tikz
同类还值得读
编写更好文档的框架
A Framework for Writing Better Documentation
这是 Divio 的文档写作框架,把技术文档按「读者的目标」分成四类:教程(学习导向)、操作指南(问题导向)、参考(信息导向)、解释(理解导向)。
技术写作与文档2021 · 637 分 · 76 评4 条写作要点编辑精选写好一份设计文档
Writing a good design document
这篇 HN 帖子标题为《Writing a good design document》,主题是如何写好一份设计文档;对写作者来说,它值得作为技术写作和结构化说明文的参照。
技术写作与文档2025 · 581 分 · 147 评3 条写作要点编辑精选技术写作课程
Technical Writing Courses
这是 Google 官方免费技术写作课程(两门),讲受众定义、主动语态、短句、文档结构等基础方法。
技术写作与文档2020 · 571 分 · 73 评4 条写作要点编辑精选写出更好的错误信息
Write better error messages
这篇帖子讨论如何写出更好的错误信息,评论补充了开发者场景下的具体做法和真实反面案例。
技术写作与文档2022 · 566 分 · 253 评4 条写作要点编辑精选写一本技术书的经济账
The Economics of Writing a Technical Book
一位技术书作者拆解写书的真实经济账:版税收入往往远低于投入的时间成本。
技术写作与文档2018 · 519 分 · 149 评4 条写作要点编辑精选