Skip to content

Commit a066a3d

Browse files
authored
More flexible capability to control showing reference (typst#646)
1 parent 8300f75 commit a066a3d

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

library/src/meta/heading.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ impl Synthesize for HeadingElem {
102102
self.push_numbering(self.numbering(styles));
103103
self.push_supplement(self.supplement(styles));
104104
self.push_outlined(self.outlined(styles));
105+
self.push_supplement(self.supplement(styles));
105106
Ok(())
106107
}
107108
}

library/src/meta/reference.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,62 @@ pub struct RefElem {
8585
/// A synthesized citation.
8686
#[synthesized]
8787
pub citation: Option<CiteElem>,
88+
89+
/// Content of the element, it should be referable.
90+
///
91+
/// ```example
92+
/// #set heading(numbering: (..nums) => {
93+
/// nums.pos().map(str).join(".")
94+
/// }, supplement: [Chapt])
95+
///
96+
/// #show ref: it => {
97+
/// if it.has("element") and it.element.func() == heading {
98+
/// let element = it.element
99+
/// "["
100+
/// element.supplement
101+
/// "-"
102+
/// numbering(element.numbering, ..counter(heading).at(element.location()))
103+
/// "]"
104+
/// } else {
105+
/// it
106+
/// }
107+
/// }
108+
///
109+
/// = Introduction <intro>
110+
/// = Summary <sum>
111+
/// == Subsection <sub>
112+
/// @intro
113+
///
114+
/// @sum
115+
///
116+
/// @sub
117+
/// ```
118+
#[synthesized]
119+
pub element: Option<Content>,
88120
}
89121

90122
impl Synthesize for RefElem {
91123
fn synthesize(&mut self, vt: &mut Vt, styles: StyleChain) -> SourceResult<()> {
92124
let citation = self.to_citation(vt, styles)?;
93125
self.push_citation(Some(citation));
126+
127+
if !vt.introspector.init() {
128+
self.push_element(None);
129+
return Ok(());
130+
}
131+
132+
// find the element content
133+
let target = self.target();
134+
let elem = vt.introspector.query_label(&self.target());
135+
// not in bibliography, but in document, then push the element
136+
if let (false, Ok(elem)) =
137+
(BibliographyElem::has(vt, &target.0), elem.at(self.span()))
138+
{
139+
self.push_element(Some(elem));
140+
} else {
141+
self.push_element(None);
142+
}
143+
94144
Ok(())
95145
}
96146
}

tests/ref/meta/ref.png

164 KB
Loading

tests/typ/meta/ref.typ

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,104 @@ As seen in @intro, we proceed.
1919

2020
// Error: 1-5 label occurs multiple times in the document
2121
@foo
22+
23+
---
24+
25+
#show ref: it => {
26+
if it.element != none and it.element.func() == figure {
27+
let element = it.element
28+
"["
29+
element.supplement
30+
"-"
31+
str(element.counter.at(element.location()).at(0))
32+
"]"
33+
// it
34+
} else {
35+
it
36+
}
37+
}
38+
39+
#figure(
40+
image("/cylinder.svg", height: 3cm),
41+
caption: [A sylinder.],
42+
supplement: "Fig",
43+
) <fig1>
44+
45+
#figure(
46+
image("/tiger.jpg", height: 3cm),
47+
caption: [A tiger.],
48+
supplement: "Figg",
49+
) <fig2>
50+
51+
#figure(
52+
$ A = 1 $,
53+
kind: "equation",
54+
supplement: "Equa",
55+
56+
) <eq1>
57+
@fig1
58+
59+
@fig2
60+
61+
@eq1
62+
63+
---
64+
#set heading(numbering: (..nums) => {
65+
nums.pos().map(str).join(".")
66+
}, supplement: [Chapt])
67+
68+
#show ref: it => {
69+
if it.element != none and it.element.func() == heading {
70+
let element = it.element
71+
"["
72+
emph(element.supplement)
73+
"-"
74+
numbering(element.numbering, ..counter(heading).at(element.location()))
75+
"]"
76+
} else {
77+
it
78+
}
79+
}
80+
81+
= Introduction <intro>
82+
83+
= Summary <sum>
84+
85+
== Subsection <sub>
86+
87+
@intro
88+
89+
@sum
90+
91+
@sub
92+
93+
---
94+
95+
#show ref: it => {
96+
if it.element != none {
97+
if it.element.func() == text {
98+
let element = it.element
99+
"["
100+
element
101+
"]"
102+
} else if it.element.func() == underline {
103+
let element = it.element
104+
"{"
105+
element
106+
"}"
107+
} else {
108+
it
109+
}
110+
} else {
111+
it
112+
}
113+
}
114+
115+
@txt
116+
117+
Ref something unreferable <txt>
118+
119+
@under
120+
#underline[
121+
Some underline text.
122+
] <under>

0 commit comments

Comments
 (0)