我正在使用 typst 来写我的 Facharbeit(德国高中的科学论文)。正式标准之一是所有数字都必须附加在最后。
然而,对于以数字方式阅读文本,这非常不方便,因为您必须一直向下滚动才能获得相关上下文,而我的一位老师说她想要一份带有内嵌数字的文档的打印版本,即不在末尾插入文本内容。手动移动它们是重复性的且容易出错,尤其是在最后移动数字时会丢失信息。
是否可以实现这样的切换,即#let inline-figures = true | false,并自动将数字移动到它们所属的位置?理想情况下,我可以按原样使用 figure-command 而无需对其进行任何更改,但如果可以有一个新函数“#myfigure”来完成我为我描述的功能,那也很好。
提前致谢!
从 Typst 的文档和问题跟踪器来看,似乎无法以编程方式修改渲染图形的位置(有关某些背景,请参见https://github.com/typst/typst/issues/553)。
另一种方法是使用不同的编程语言对文档进行后处理,以便移动源代码中的图形,然后可以重新编译。
以下 Python 程序搜索
#figure
标签,并提取以下代码,直到 < >
之间的平衡右括号和可能的标签。然后它通过从正文中删除所有图形块并在末尾添加它们来重构代码。
import re
fin = open("inlinefigs.typ")
fulltext = fin.read()
fin.close()
# find all indices of the start of a figure with the preceding newline
figures_start = [m.start()-1 for m in re.finditer('#figure\(', fulltext)]
figures_end = []
# find the balanced closing parenthesis for each figure
for start in figures_start:
stack = 1
# skip \n#figure( when looking for closing parenthesis
i = start + 9
while stack > 0:
if fulltext[i] == ')':
stack -= 1
if fulltext[i] == '(':
stack += 1
i += 1
# check for possible label
if fulltext[i+1] == '<':
i = fulltext.find('>', i) + 1
# store end position of current figure in second list
figures_end.append(i)
prev_idx = 0
newtext = ""
allfigs = ""
# remove figure spans from the text
for i in range(len(figures_start)):
allfigs += fulltext[figures_start[i]:figures_end[i]] + "\n"
newtext += fulltext[prev_idx:figures_start[i]]
prev_idx = figures_end[i]
# add text after last figure
newtext += fulltext[prev_idx:]
fout = open("endfigs.typ", "w")
# write text without figures
fout.write(newtext)
# write figures at the end
fout.write("= All figures\n")
fout.write(allfigs)
fout.close()
此Python代码可应用于以下Typst文档:
@glacier shows a glacier. Glaciers
are complex systems.
#figure(
image("glacier.jpg", width: 30%),
caption: [A curious figure.],
) <glacier>
Molecular testing is also very complex, as seen in @molecular.
#figure(
image("molecular.jpg", width: 30%),
caption: [
The molecular testing pipeline.
],
) <molecular>
Trees are simple acyclic graphs.
#figure(
image("graph.png", width: 30%),
caption: [
A (binary) tree.
],
)
There is much more to say about these images, but that is left for future work.
生成的文档
endfigs.typ
,编译后如下所示: