Skip to content

Commit d1be9a6

Browse files
committed
Final cleanup for nested-boxes option
1 parent aca830f commit d1be9a6

4 files changed

Lines changed: 33 additions & 120 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This package provides two functions:
1616
- `style-dep`: the styling function that will be applied to the dependencies. Optional: Default is `content => text(style: "italic", content)`.
1717
- `style-formula`: the styling function that will be applied to the formula. Optional: Default is `content => content`.
1818
- `style-rule`: the styling function that will be applied to the rule. Optional: Default is `content => text(style: "bold", content)`.
19+
- `nested-boxes`: instead of using indentation lines, use nested boxes. Optional: Default is `false`.
1920

2021
`ded-nat-boxed` is a function that returns the deduction in a `box` and expects:
2122
- `stcolor`: the stroke color of the indentation guides. Optional: Default is `black`.
@@ -27,6 +28,7 @@ This package provides two functions:
2728
- `style-dep`: the styling function that will be applied to the dependencies. Optional: Default is `content => text(style: "italic", content)`.
2829
- `style-formula`: the styling function that will be applied to the formula. Optional: Default is `content => content`.
2930
- `style-rule`: the styling function that will be applied to the rule. Optional: Default is `content => text(style: "bold", content)`.
31+
- `nested-boxes`: instead of using indentation lines, use nested boxes. Optional: Default is `false`.
3032

3133

3234
> Note: depending on your layout, this functions may fail to compile due to a high enough amount of indentation (due to the recursive implementation of the layout).

examples/example.pdf

2.85 KB
Binary file not shown.

examples/example.typ

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,21 @@
1515

1616
Default usage of `ded-nat-boxed`:
1717

18-
#ded-nat-alt(arr:(
18+
`nested-boxes`: `false`
19+
20+
#ded-nat-boxed(arr:(
21+
(0, $p -> (q -> r)$, "P"),
22+
(1, $q$ , "P"),
23+
(2, $p$ , "P"),
24+
(2, $q -> r$ , $e->,3,1$),
25+
(2, $r$, $e->,2,4$),
26+
(1, $p -> r$, $i->,"3-5"$),
27+
(0, $q -> (p -> r)$ , $i->,"2-6"$),
28+
))
29+
30+
`nested-boxes`: `true`
31+
32+
#ded-nat(nested-boxes: true, arr:(
1933
(0, $p -> (q -> r)$, "P"),
2034
(1, $q$ , "P"),
2135
(2, $p$ , "P"),
@@ -90,7 +104,7 @@ Elegir 10 ejercicios del libro de Falguera y Vidal, de las páginas 318, 319, 32
90104

91105
+ pg. 321, ejercicios VI, I. 62)
92106

93-
#ded-nat-alt(stcolor: black, arr: (
107+
#ded-nat(nested-boxes: true,stcolor: black, arr: (
94108
("1", 0, $forall x (S x b) and not forall y (P y -> Q b y)$, "PR"),
95109
("2", 0, $forall x forall y (Q x y -> not Q y x)$, "PR"),
96110

lib.typ

Lines changed: 15 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- `style-dep`: the styling function that will be applied to the dependencies. Optional: Default is `content => text(style: "italic", content)`.
99
- `style-formula`: the styling function that will be applied to the formula. Optional: Default is `content => content`.
1010
- `style-rule`: the styling function that will be applied to the rule. Optional: Default is `content => text(style: "bold", content)`.
11+
- `nested-boxes`: instead of using indentation lines, use nested boxes. Optional: Default is `false`.
1112
1213
*/
1314

@@ -160,118 +161,6 @@
160161
)
161162
}
162163

163-
#let ded-nat-alt(
164-
stcolor: black,
165-
arr: array,
166-
style-dep: derive-it-internal-default-style-dep,
167-
style-formula: derive-it-internal-default-style-formula,
168-
style-rule: derive-it-internal-default-style-rule,
169-
) = context {
170-
assert(type(arr) == array, message: "The input must be an array.")
171-
172-
// validate the input and obtain metrics
173-
let (
174-
has-dependencies,
175-
line-size,
176-
max-dep-width,
177-
max-indent-level,
178-
max-formula-width,
179-
arr,
180-
) = derive-it-internal-array-checks(arr, style-dep, style-formula, style-rule)
181-
182-
let maxWidth = 0pt
183-
let tupArr = ()
184-
let i = 0
185-
while i < arr.len() {
186-
if not has-dependencies {
187-
arr.at(i).insert(0, none)
188-
}
189-
let (dep, indent, formula, rule) = arr.at(i)
190-
191-
// +1 -> more indented
192-
// -1 -> less indented
193-
// 0 -> equally indented
194-
let prev-indentation-diff = if (i == 0) { -1 } else {
195-
// previous line arrays always have 4 elements (dep=none)
196-
arr.at(i - 1).at(1) - indent
197-
}
198-
let next-indentation-diff = if (arr.len() <= i + 1) { -1 } else {
199-
let ix = if has-dependencies { 1 } else { 0 }
200-
arr.at(i + 1).at(ix) - indent
201-
}
202-
203-
let bl = formula
204-
205-
///////////////////////// FOR /////////////////////////////
206-
for indent-index in range(0, max-indent-level + 1) {
207-
let stroke = (top: 0pt, right: 0pt, bottom: 0pt, left: 0pt)
208-
let strength = if (indent >= max-indent-level - indent-index) {
209-
1pt
210-
} else { 0.0pt } // 'else' branch for debugging
211-
// current level of indentation needs box stroke
212-
let stroke-line-config = strength + stcolor
213-
214-
if (indent == (max-indent-level - indent-index)) {
215-
// we're in the box that corresponds to the indentation of this formula
216-
if prev-indentation-diff <= -1 {
217-
// if the previous formula is in a lower indentation level
218-
stroke.top = stroke-line-config
219-
}
220-
if next-indentation-diff <= -1 {
221-
// if the next formula is in a lower indentation level
222-
stroke.bottom = stroke-line-config
223-
}
224-
}
225-
stroke.left = stroke-line-config
226-
stroke.right = stroke-line-config
227-
228-
let rect-width = if (indent-index == 0) {
229-
max-formula-width + 10pt // 5pt each for left & right
230-
} else {
231-
auto
232-
}
233-
234-
let extra-spacing = if (indent-index == max-indent-level) { 0em } else {
235-
1em
236-
}
237-
238-
bl = [#box(inset: (x: extra-spacing, y: 0pt))[#rect(
239-
stroke: stroke,
240-
inset: if indent-index == 0 { 5pt } else { 0pt },
241-
width: rect-width,
242-
)[#bl]]]
243-
}
244-
///////////////////////// FOR /////////////////////////////
245-
246-
let line-index-content = box(width: 1.5em)[#(i + 1).]
247-
248-
let numInset = (x: 0pt, y: 5pt)
249-
let line = if has-dependencies {
250-
let dependency = box(width: max-dep-width + 1em)[#dep]
251-
let l = box[#box(inset: numInset)[#dependency #line-index-content] #bl]
252-
maxWidth = calc.max(maxWidth, measure(l).width)
253-
l
254-
} else {
255-
let l = box[#box(inset: numInset)[#line-index-content] #bl]
256-
maxWidth = calc.max(maxWidth, measure(l).width)
257-
l
258-
}
259-
260-
rule = box(baseline: -0.5em, rule)
261-
tupArr.push((line, rule, indent))
262-
i += 1
263-
}
264-
tupArr = tupArr.map(a => [#box(width: maxWidth, a.at(0)) #h(1em) #a.at(1)])
265-
266-
block(
267-
align(
268-
start,
269-
stack(..tupArr),
270-
),
271-
)
272-
}
273-
274-
275164
#let ded-nat(
276165
stcolor: black,
277166
arr: array,
@@ -366,30 +255,37 @@
366255
}
367256

368257
let rect-inset = if indent-index == 0 { 5pt } else { 0pt }
369-
bl = box(stroke: 0.1pt+blue, inset: external-box-inset, rect(
258+
// add stroke for debugging
259+
bl = box(/* stroke: 0.0pt + blue, */ inset: external-box-inset, rect(
370260
stroke: stroke,
371261
inset: rect-inset,
372262
width: rect-width,
373263
bl,
374264
))
375265
}
376-
///////////////////////// FOR /////////////////////////////
266+
///////////////////////// END FOR /////////////////////////////
377267

378268
let line-index-content = box(width: 1.5em)[#(i + 1).]
379-
let (ins, numInset) = if not nested-boxes and indent == 0 {
269+
let (line-inset, dep-and-number-inset) = if (
270+
not nested-boxes and indent == 0
271+
) {
380272
((y: 0.5em), 0pt)
381273
} else {
382274
(0pt, (y: 5pt))
383275
}
276+
384277
let line = if has-dependencies {
385278
let dependency = box(width: max-dep-width + 1em)[#dep]
386-
let l = box(inset: ins)[#box(
387-
inset: numInset,
279+
let l = box(inset: line-inset)[#box(
280+
inset: dep-and-number-inset,
388281
)[#dependency #line-index-content] #bl]
389282
maxWidth = calc.max(maxWidth, measure(l).width)
390283
l
391284
} else {
392-
let l = box(inset: ins)[#box(inset: numInset)[#line-index-content] #bl]
285+
let l = box(inset: line-inset)[#box(
286+
inset: dep-and-number-inset,
287+
line-index-content,
288+
) #bl]
393289
maxWidth = calc.max(maxWidth, measure(l).width)
394290
l
395291
}
@@ -420,6 +316,7 @@
420316
- `style-dep`: the styling function that will be applied to the dependencies. Optional: Default is `content => text(style: "italic", content)`.
421317
- `style-formula`: the styling function that will be applied to the formula. Optional: Default is `content => content`.
422318
- `style-rule`: the styling function that will be applied to the rule. Optional: Default is `content => text(style: "bold", content)`.
319+
- `nested-boxes`: instead of using indentation lines, use nested boxes. Optional: Default is `false`.
423320
424321
*/
425322

0 commit comments

Comments
 (0)