I’m used to writing my equations like this:
This way I can explain further what I did from one step to another and the bar provides an overview where the actual equation continues.
How can I achieve this in typst?
I’m used to writing my equations like this:
This way I can explain further what I did from one step to another and the bar provides an overview where the actual equation continues.
How can I achieve this in typst?
Welcome to the community!
You can use &
to align across lines.
#let note-line = box(width: 0.8em, $stretch(arrow.b, size: #200%)$) + h(1em)
$
"RT" & = (rho + a/V^2) (V - b) = rho v + dots.c \
& #note-line (a b) / V^2 approx 0 \
& = rho V + a/V - rho b.
$
Thanks for the quick reply! My code looks now like this:
#let eqbar = h(.2em) + math.stretch(math.bar.v, size: 300%) + h(1em)
$
R T & = (p + a / v^2)(v - b) = p v + a / v - p b - frac(a b, v^2) = R T \
& #eqbar frac(a b, v^2) approx 0 \
& = p v + a / v - p b \
& #eqbar v approx R T / p \
& #eqbar => a / v = (a p ) / (R T) = a p / (R T) \
& = p v + (a p ) / (R T) - p b \
$
Is there a way to connect the two lines when spanning over multiple lines?
You can box the line such that its height is no longer considered for the line spacing:
#let eqbar = h(.2em) + box(height: 0.75em, align(horizon, math.stretch(math.bar.v, size: 300%))) + h(1em)
Alternatively, you could create a helper function (“barred
”) which generates a bar across multiple lines:
#let barred(..lines, block: true) = context {
let lines = std.block(math.equation(
block: block,
{
for line in lines.pos() {
$ & #line\ $
}
}
))
let height = measure(lines).height
h(.2em)
math.stretch(math.bar.v, size: height + 1em)
h(1em)
lines
}
$
R T & = (p + a / v^2)(v - b) = p v + a / v - p b - frac(a b, v^2) = R T \
& barred( frac(a b, v^2) approx 0 ) \
& = p v + a / v - p b \
& barred(
v approx R T / p ,
=> a / v = (a p ) / (R T) = a p / (R T)
) \
& = p v + (a p ) / (R T) - p b \
$
Edit: If we’re using a block anyway, we can simply add a stroke and forgo measuring:
#let barred(..lines, block: true) = {
h(0.4em)
std.block(
stroke: (left: 0.5pt + black),
inset: (left: 1em, y: 0.5em),
math.equation(
block: block,
for line in lines.pos() {
$ & #line\ $
}
)
)
}
barred
) are separated by ,
. If you want a literal ,
or ;
, you need to escape them as \,
and \;
.This is more what I was looking for! But there’s a weird behavior where the format completely breaks when there’s a semicolon ; in a line.
Also would it be possible to make a function that just accepts a whole block of math code, so I don’t have to separate the lines by comma?
This is how it looks with a semicolon:
I think that once you have a block there, then the alignment points in inner and outer equation no longer connect, so they can be removed in the inner equation?
You would need to escape the semicolon, like this: \;
that just ensures it’s a regular semicolon symbol in math mode and don’t have any special functionality (see the mat
function for how semicolon is used).
No, they are needed to make the lines in the block left-aligned.
This is possible, but it requires calling as #barred[$...$]
and you need to add the &
inside, so I think having lines be comma-separated is faster to type.
#let barred(line) = {
h(0.4em)
std.block(
stroke: (left: 0.5pt + black),
inset: (left: 1em, y: 0.5em),
line
)
}
$
R T & = (p + a / v^2)(v - b) = p v + a / v - p b - frac(a b, v^2) = R T \
& barred( frac(a b, v^2) approx 0 ) \
& = p v + a / v - p b \
& #barred[$
& v approx R T / p \
& => a / v = (a p ) / (R T) = a p / (R T)
$] \
& = p v + (a p ) / (R T) - p b \
$
So there’s no way of writing it like this?
#barred[
v approx (R T) / p \
=> a / v = (a p) / (R T)
]
No, as soon as we have #[
we are no longer in math mode, so we need to add $
for the equation. Additionally, we need to add &
at the start of each line to fix the alignment.
I see thanks for your help!
We can do the following:
#let barred(body, block: true) = context {
let lines = std.block({
show math.equation: set align(start)
math.equation(block: block, body)
})
let height = measure(lines).height
h(.2em)
math.stretch(math.bar.v, size: height + 1em)
h(1em)
lines
}
$
R T & = (p + a / v^2)(v - b) = p v + a / v - p b - frac(a b, v^2) = R T \
& barred( frac(a b, v^2) approx 0 ) \
& = p v + a / v - p b \
& barred(
v approx R T / p \
=> a / v = (a p ) / (R T) = a p / (R T)
) \
& = p v + (a p ) / (R T) - p b \
$
And it will work and have the same result as the previous version - but it doesn’t help much with the semicolon issue.
The following is unrelated, but personally I would be tempted to polish on the typesetting by evening out the paranthesis size like this
#let lrmid(body) = {
set math.lr(size: 1.5em)
body
}
$
R T & = lrmid((p + a / v^2)(v - b))
$
That’s exactly what was looking for! Thanks a lot!