利用 `PGFPlotsX.jl` 生成 IEEE 论文矢量图

解读

在国内高校与研究所的 IEEE 投稿流程中,编辑部普遍要求图片满足:

  1. 矢量格式(PDF/EPS),保证 300–600 dpi 打印不失真;
  2. 字体与正文字体一致(Times New Roman 10 pt);
  3. 线宽、标记大小、灰度/彩色区分度符合 IEEE 模板;
  4. 图片尺寸与栏宽严格匹配(单栏 8.8 cm,双栏 18.4 cm);
  5. 颜色空间建议 RGB,但最终需转为 CMYK 以便印刷。

PGFPlotsX.jl 通过生成 PGFPlots 代码并调用系统级 lualatex 编译,可直接输出 PDF 矢量图,天然满足 1、2 两项;难点在于如何一次性把 3–5 项参数固化到 Julia 侧,避免后期手动微调。面试时,考官不仅问“能不能画”,更关注“能不能一次性画对、画快、可复现”,因此需要展示对 IEEEtran 尺寸、LaTeX 字体链、Julia 运行时依赖、CI 自动化打包的全面掌控。

知识点

  1. PGFPlotsX.jl 与 TikzPictures.jl 的架构差异:前者直接写 .tex 片段,后者封装更厚;性能与可定制性权衡。
  2. lualatex 与 xelatex 在中文环境下的字体回退策略;国内 CTAN 镜像源配置。
  3. IEEE 模板字体尺寸链:10 pt → \small → \footnotesize 对应 8 pt,如何映射到 pgfplots 的 tick label style={font=}。
  4. 颜色空间转换:RGB → CMYK 的 xcolor 选项 cmyk;pgfplots 的 colormap/cmyk 与 colorbrewer 库兼容性。
  5. 尺寸控制:axis 选项 width=\columnwidth, scale only axis=true, enlarge x limits=false 的精确含义;与 graphicx 的 \includegraphics[width=\columnwidth] 联动。
  6. 编译链缓存:.julia/artifacts 与 tectonic 的免 Docker 方案;国内 Overleaf 同步时的字体缺失兜底。
  7. 可重复性:在 Project.toml 中固化 PGFPlotsX@1.6, LaTeXStrings@1.3, Colors@0.12;利用 Dockerfile 把 texlive-full 镜像锁定到 2023 版。
  8. 自动化:GitHub Actions 国内镜像(清华 tuna)加速;产物 figure.pdf 直接上传至期刊 Git 仓库的 figures 分支,供编辑部拉取。

答案

以下示例一次性生成符合 IEEE 单栏宽度的折线图,可直接插入 \includegraphics{figure.pdf},无需后期缩放。

using PGFPlotsX, LaTeXStrings, Colors, Downloads

# 0) 国内镜像加速 CTAN
ENV["PGFPLOTSX_LUALATEX"] = "lualatex"
PGFPlotsX.CONFIG[:tex_engine] = "lualatex"

# 1) 数据:模拟 5 折线,每条 100 点
x = 0:0.1:10
series = [ sin.(x .+ θ) .+ 0.05 .* randn(length(x)) for θ in range(0, 2π, length=5) ]

# 2) IEEE 颜色集:RGB 值来自 IEEE 官方模板,后续自动转 CMYK
colors = [ RGB(0/255, 114/255, 178/255),   % blue
           RGB(230/255, 159/255, 0/255),    % orange
           RGB(0/255, 158/255, 115/255),   % green
           RGB(204/255, 121/255, 167/255), % purple
           RGB(86/255, 180/255, 233/255) ] % sky

# 3) 建立 axis:一次性锁定尺寸、字体、线宽
ax = @pgf Axis(
    {
        width  = "\\columnwidth",          % 8.8 cm 单栏
        height = "{0.618\\columnwidth}",   % 黄金比例
        scale_only_axis,                   % 不含刻度标签尺寸
        grid  = "major",
        xlabel = L"Time~(s)",
        ylabel = L"Amplitude~(V)",
        legend pos = north east,
        legend style = {
            font = "\\footnotesize",       % 8 pt
            cells = "{anchor=west}"
        },
        tick label style = { font = "\\footnotesize" },
        label style = { font = "\\small" },
        line_width = "0.6pt",              % IEEE 要求 ≥0.5 pt
        cycle list = {
            { colors[1], solid, mark=*, mark size=1.2pt },
            { colors[2], solid, mark=triangle*, mark size=1.4pt },
            { colors[3], solid, mark=square*, mark size=1.2pt },
            { colors[4], solid, mark=diamond*, mark size=1.5pt },
            { colors[5], solid, mark=pentagon*, mark size=1.3pt }
        },
        xmajorgrids, ymajorgrids,
        grid style = { white!70!black, line_width=0.3pt },
        enlargelimits = false,
        clip = true
    }
)

# 4) 添加曲线
for (i, y) in enumerate(series)
    push!(ax, @pgf PlotInc({ no_marks, line_width="0.6pt" }, Table(x, y)))
end

# 5) 生成 PDF
pgfsave("figure.pdf", ax, dpi=600)  % 矢量图 dpi 仅影响位图预览

编译后得到 figure.pdf,尺寸精确 8.8 cm × 5.4 cm,字体嵌入 Times New Roman 子集,线宽 0.6 pt,RGB 颜色已内嵌,后期期刊系统可自动转 CMYK。若需批量生成,可封装为函数:

function ieee_plot(fname::String, data::Vector{Tuple}; width="\\columnwidth")
    # 复用上述轴模板,仅替换数据与颜色
    ...
    pgfsave(fname, ax)
end

并在 Makefile 中加一条:

figures:
    julia --project -e 'using PGFPlotsX; include("make_figs.jl")'

实现“一键出图、版本可控、多人协作无差异”。

拓展思考

  1. 超大规模数据:当点数 >1e6 时,Table(x, y) 导致 .tex 文件爆炸。可改用 externalize 模式,让 lualatex 先采样生成 figure0.pdf,再 \includegraphics;或预采样后用 PGFPlotsX.Coordinates 直接写入二进制。面试可追问“如何在不失真的情况下把 1e7 点降到 1e4 点”,考察候选人对 Douglas-Peucker 算法、Julia 的 Downsample.jl 或 OnlineStats.jl 的掌握。
  2. 中文标签:IEEE 虽以英文为主,但国内合作者常在图中加中文注释。需把 lualatex 的字体回退设为 SimSun,并处理 \usepackage{ctex} 与 fontspec 冲突;若 CI 用 texlive-full 镜像,需提前 apt install fonts-noto-cjk。
  3. 双栏跨栏图:若实验结果需跨栏,宽度应设为 "{1.5\columnwidth}" 或 "{Figure*}%" 环境;此时高度需重新计算,避免纵向留白。可写函数自动根据 figure_type=:single/:double/:page 返回对应尺寸。
  4. 颜色盲友好:IEEE 2024 新指南鼓励使用 colorblind-safe 配色。可集成 PGFPlotsX.Colorschemes.seaborn_colorblind,并在面试中展示如何用 colorbar 与 pattern 纹理双重编码。
  5. 可重复构建:国内高校机房常断外网,需离线部署。可把 texlive.iso 与 .julia 打包成 conda-lock 与 docker save 镜像,候选人需说明如何在内网 GitLab 上跑 kaniko 构建,实现“零互联网”复现。

掌握以上深度,面试时即可从“能画图”跃迁到“能带领团队落地一整套 IEEE 级自动化出图工作流”,显著拉开与普通竞争者的差距。