如何为代码块制作标题和设置编号样式

问题描述 投票:0回答:1

以下是我的演示文档。

#set heading(numbering: "A.")

#show raw.where(lang:"python"): it=>{
par(justify:false,block(fill:rgb("#f0fef0"),inset:1.5em,width:99%,text(it)))}

= first section
== first.first section
#figure(
```python
print("hello,world")
```)<program-hello-world>
= second section
== second.first section
In the program (@program-hello-world), a demo program was given.

blabla

我想为上面的每个代码块添加自动编号的标题,并且希望数字包含章节号。我想要的结果是

typst
1个回答
0
投票

在显示规则后将其粘贴到您的文档中:

#let filename(target, loc) = if target.func() == raw and target.lang == "python" {
  context {
    let hcount = counter(heading).at(loc).first()
    let figcount = counter(figure).at(loc).first()
    [Code-#numbering("A-1", hcount, figcount)\.py]
  }
} else {
  none
}

#show figure: it => {
  let label = filename(it.body, it.location())
  
  if label != none {
    block(
      above: 20pt,
      below: 20pt,
      strong("Filename: " + label) + it.body,
    )
  } else {
    it 
  }
}

#show ref: it => {
  let target = query(it.target).last()
  let label = filename(target.body, target.location())

  if label != none {
    label
  } else { it }
}

这是一个使用图形主体和位置来查询标题和图形计数器并格式化文件名的函数,如示例中所示。我们单独传递位置,而不是在

target
上调用它,因为从图中显示规则来看,它将是
none
上的
it.body
。相反,我们分别传递图中的位置(通过显示规则和查询获得)。

最后,我们可以使用

#show figure
自定义图形并通过
#show ref
参考。

结果将如下所示:

© www.soinside.com 2019 - 2024. All rights reserved.