Rust in Everything
基于 typst 0.15.1

图片处理、子图裁剪、figure 容器与交叉引用

主线位置:科研排版第二站 —— 全面掌握图片的缩放适应、局部裁剪、圆角阴影框、多子图 (a)(b) 并排、双栏通栏宽图与背景水印,并用统一的 figure 容器实现智能编号与 @ref 引用。

1. 图片处理与交叉引用三方对比

特性MarkdownLaTeXTypst (0.15.1)
插入图片与支持格式![说明](arch.svg)(无法控制宽高与页内浮动)\includegraphics[width=0.8\textwidth]{arch.pdf}(原生不支持 SVG!)#image("arch.svg", width: 80%)(原生支持 SVG、PNG、JPEG、GIF、PDF)
缩放适应模式(Fit)需手写内联 <img style="object-fit: cover">keepaspectratio#image("...", width: 4cm, height: 3cm, fit: "cover")(支持 "contain" / "cover" / "stretch")
原图无损裁剪与圆角无\includegraphics[trim=1cm 2cm 1cm 0cm, clip]{...}#box(clip: true, radius: 6pt, inset: (top: -1cm))[#image(...)]
多子图并排 (a) (b)无需加载 \usepackage{subcaption} 写嵌套 subfigure直接在 #figure 内放 #grid(columns: 2, ...)
双栏论文跨栏宽图无\begin{figure*}[t] ... \end{figure*}(常跳到下一页)#figure(scope: "parent", placement: top, ...)
图/表/代码统一浮动容器无图片用 figure,表格用 table,代码用 lstlisting统统用 #figure(..., placement: auto)!自动按内容切换计数器和「图/表」前缀
打标签与智能引用无\label{fig:arch} + \autoref{fig:arch} / \cref{fig:arch}末尾挂 <fig:arch>,正文写 @fig:arch(自动输出 Figure 1 或 图 1)
flowchart TB
  fig["#figure(body, caption: [...], placement: auto, scope: 'column' | 'parent') <label>"] --> kind{"自动检测 body 类型 (kind)"}
  kind -- "image(...) 或子图 grid" --> k_img["kind: image<br/>计数器: figure.where(kind: image)<br/>中文前缀: 图 1"]
  kind -- "table(...)" --> k_tbl["kind: table<br/>计数器: figure.where(kind: table)<br/>中文前缀: 表 1"]
  kind -- "raw(...)" --> k_raw["kind: raw<br/>计数器: figure.where(kind: raw)<br/>中文前缀: 代码 1"]

2. 图片缩放模式(fit)、无损裁剪、圆角框与内存动态 SVG

在 crates/typst-library/src/visualize/image/mod.rs 中,image 函数不仅支持读取磁盘文件,还允许直接传入内存字节 bytes(...) 动态渲染 SVG,并通过外层 box/block 实现裁剪与圆角装饰:

  1. 三种宽高适应模式(fit)(当同时指定 width 和 height 时生效):
    • fit: "contain"(默认):保持原图宽高比,完整缩放到目标框内;
    • fit: "cover":保持原图宽高比,填满整个目标框并自动裁掉多余边缘(极适合做头像或统一尺寸的卡片封面!);
    • fit: "stretch":忽略原图比例,强行拉伸填满目标框。
  2. 不借助 PS,直接在 Typst 内对图片做局部裁剪(Cropping)与圆角边框:
    • 将图片包在 #box(clip: true, radius: 6pt, stroke: 0.5pt + luma(200), ...) 中即可切成圆角卡片;
    • 如果原图四周有多余白边想裁掉,只需给外层 box(clip: true) 设置负数内边距 inset: (top: -15pt, bottom: -10pt),就能像 LaTeX trim + clip 一样无损裁切!
// 用内存中的 SVG 字符串生成一个测试矢量图标(无需外部图片文件即可直接编译验证!)
#let demo-svg = bytes("<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 80'><rect width='160' height='80' fill='#eff6ff'/><circle cx='45' cy='40' r='24' fill='#3b82f6'/><rect x='85' y='24' width='55' height='12' rx='3' fill='#1e40af'/><rect x='85' y='44' width='40' height='10' rx='3' fill='#60a5fa'/></svg>")

// 1. 圆角边框 + 阴影衬底卡片包裹图片
#box(
  radius: 6pt,
  clip: true,
  stroke: 0.8pt + rgb("#93c5fd"),
  image(demo-svg, width: 5cm),
)

// 2. 利用负 inset 裁剪掉原图四周空白(例如裁去右侧 1.5cm,仅保留左侧圆形图标区域)
#box(
  clip: true,
  radius: 4pt,
  stroke: 0.5pt + luma(180),
  inset: (right: -2.5cm),
  image(demo-svg, width: 5cm),
)

3. 多子图并排排版((a) (b) Subfigures)与跨双栏大图

在写实验对比章节时,最常见的需求是把两张或四张对比图并排放在同一个 figure 里,底下配 (a) 基准算法 和 (b) 优化算法 子标题:

#set text(lang: "zh")
// 国标规范:让所有表格的标题自动排在表格正上方,而图片标题保持在下方
#show figure.where(kind: table): set figure.caption(position: top)

#let sample-svg(color) = bytes(
  "<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 120 60'><rect width='120' height='60' rx='4' fill='" + color + "'/></svg>"
)

// 多子图并排:在 figure 内部使用 grid 排列子图与 (a)/(b) 子图题,并显式指定 kind: image
#figure(
  kind: image,
  placement: auto,
  caption: [不同编译架构下的增量重排延迟对比],
  grid(
    columns: (1fr, 1fr),
    gutter: 12pt,
    [
      #image(sample-svg("#dbeafe"), width: 100%)
      #text(size: 9pt)[(a) 全量重排流水线]
    ],
    [
      #image(sample-svg("#dcfce7"), width: 100%)
      #text(size: 9pt)[(b) `comemo` 记忆化流水线]
    ],
  ),
) <fig:subfigs>

如 @fig:subfigs 所示,子图 (b) 的热更新延迟降低了两个数量级。

双栏论文中的跨栏宽幅大图与页面斜向水印

  • 双栏跨栏通栏大图:在 #set page(columns: 2) 的双栏论文中,如果某张架构图很宽,只需在 #figure(...) 上加两个参数:scope: "parent", placement: top(或 bottom),这张图就会自动跨越左右两栏置顶排版,彻底告别 LaTeX \begin{figure*} 总是把图扔到下一页的痛点!
  • 全页背景图或斜向防伪水印:利用 page.background(位于正文底层)或 page.foreground(位于正文顶层),三行代码即可加上斜向半透明水印:
#set page(
  background: rotate(
    -30deg,
    text(size: 56pt, weight: "bold", fill: rgb(0, 0, 0, 12%))[内部资料 · 请勿外传],
  ),
)

4. 自定义第 4 种浮动编号容器(如「算法 1」)与动态引用

除了内置的 image(图)、table(表)和 raw(代码清单),只需传入自定义的 kind: "algorithm" 和 supplement: [算法],Typst 就会自动为它建立独立的编号计数器:

#let algo(body, caption: none) = figure(
  body,
  kind: "algorithm",
  supplement: [算法],
  caption: caption,
)

#algo(
  caption: [不动点自省收敛检测],
  block(width: 100%, inset: 8pt, stroke: 0.5pt + luma(180), radius: 3pt, align(left)[
    *输入:* 语法内容树 `Content`\
    *输出:* 收敛的 `PagedDocument`
  ])
) <alg:converge>

详见 @alg:converge。 // 自动渲染为「详见 算法 1。」

小结

  • 插入图片首选矢量图 #image("xxx.svg", width: 80%);需要圆角或局部裁剪时外包一层 #box(clip: true, radius: ..., inset: ...)。
  • 多子图并排直接在 #figure(kind: image, grid(columns: 2, ...)) 中组合;双栏论文跨栏大图加上 scope: "parent", placement: top。

💬 关联讨论

加载中...