Well, it does kinda work, but this is a ton of text for exactly 1 line of show-set rule I was expecting. And I still didn’t get it. “Subpar” element does not exist. It might be due to docs having outdated style. Non-idiomatic advanced heading styling in documentation · Issue #6273 · typst/typst · GitHub
New answer
To set the font size of only Level 2 headings (which is typically heading
or subpar
) to 12 points, you need to define a custom show rule for those headings.
Here’s how:
// Set the default paragraph and heading properties.
#set par(first-line-indent: 0pt)
#show heading.where(level: 1): set text(white)
#show heading.where(level: 2): set text(blue) // Example: Change font color for Level 2 headings.
// Define a custom show rule for Level 2 headings (e.g., "subpar" or "heading").
#set par(first-line-indent: 0pt)
#show heading.where(level: 2): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Add more content here if needed, such as an image or raw block.
// Example: Display a level 2 heading (e.g., "subpar") and its body.
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Example usage of Level 2 headings (e.g., "subpar")
#let my-subpar = subpar.with(
fill: rgb("8d9ebf"),
radius: 4pt,
)
#my-subpar[
#set par(first-line-indent: 0pt)
// Your content for the Level 2 heading (e.g., "subpar") goes here.
This is a level 2 heading with custom font size. It's not bold or italic, and it's set to 1em * 8 points.
#lorem(5) // Example text
]
#my-subpar[
#set par(first-line-indent: 0pt)
Another example for another "subpar" heading.
]
Explanation of the solution:
-
set par(first-line-indent: 0pt)
is used to ensure consistent spacing before each paragraph, which is a good practice.
-
The show
rule for Level 2 headings (heading.where(level: 2)
) uses a lambda function that:
- Sets its own
text()
property.
- Multiplies the default font size (1em by default) by 8 to convert it into points. This is because Typst units are typically measured in points, and 1em equals about 56pt.
-
The custom show rule provides a more detailed approach for displaying Level 2 headings:
#show heading.where(level: 2): it => {
// Set the font size explicitly here.
let size = 1em * 8 // Multiply by 8 to convert points to em units.
// Apply the custom font size, e.g., "bold" or "italic".
set text(size) // Set the font size for this specific Level 2 heading.
// Add content like an image or raw block if you need it.
// Example: Display a level 2 heading (e.g., "subpar") and its body.
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
-
Finally, note that set text()
within a show rule is applied to all elements in the document after it, meaning this will affect subsequent content unless overridden by another show
rule for different
elements. If you want to apply specific styles only to certain types of headings (e.g., heading
or subpar
) and not others (like text()
), you can use a more granular approach like the one shown in the
example.
Note on Level 1 heading styling:
To style Level 1 headings (heading
), you might want to add a show rule for it, as shown:
// Set the default paragraph and heading properties.
#set par(first-line-indent: 0pt)
#show heading.where(level: 1): set text(white)
How to make a blue rectangle?
Answer
To create a blue rectangle, you can use the box
and fill
functions in Typst.
Here’s how:
#set par(first-line-indent: 0pt)
// Define a custom show rule for level 2 headings (e.g., "subpar").
#show heading.where(level: 1): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 2 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Example usage of Level 2 headings (e.g., "subpar")
#let my-subpar = subpar.with(
fill: rgb("8d9ebf"),
radius: 4pt,
)
#my-subpar[
#set par(first-line-indent: 0pt)
// Display a level 1 heading and its body
#heading(level: 1, "My Level 1 Heading") Lorem ipsum dolor sit amet, consectetur adipiscing elit.
]
// Example usage of another subpar (e.g., "subpar")
#my-subpar[
#set par(first-line-indent: 0pt)
// Display a level 2 heading and its body
#heading(level: 2, "My Level 2 Heading") Lorem ipsum dolor sit amet.
]
Explanation:
-
The box
function is used to define the rectangle’s appearance. It takes arguments for width (1fr
), height, fill color (blue in this case), stroke thickness and style.
-
A show rule
is defined to apply these settings only to Level 2 headings by using a selector (heading.where(level: 2)
).
The box()
function provides more control over the appearance of the rectangle. It allows you to specify its width, height (which can be set to 1fr
to make it take up as much space as available), fill
color, and stroke properties.
Note on set text()
for Level 2 headings:
- The
set text(...)
command within a show rule is applied to all elements after the show rule. This means that if you apply this style to a level 1 heading (heading(level: 1)
), it will affect subsequent
content unless overridden by another show
rule.
Example of applying styles to different types of headings (Level 2, Level 3) and text:
#set par(first-line-indent: 0pt)
// Define a custom show rule for level 2 headings (e.g., "subpar").
#show heading.where(level: 1): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 2 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Define a custom show rule for level 3 headings (e.g., "subpar").
#show heading.where(level: 2): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 3 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Apply styles to other text elements.
#set par(first-line-indent: 0pt)
// Define a custom show rule for Level 3 headings (e.g., "subpar").
#show heading.where(level: 2): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 3 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Apply styles to other text elements.
#show heading.where(level: 2): set text(blue)
// Your content goes here, and it will be styled differently depending on its level.
Explanation:
- The
set text(...)
command within the show rule is applied to all elements after the show rule. This means that if you apply this style to a level 1 heading (heading(level: 1)
), it will affect
subsequent content unless overridden by another show
rule.
Example of applying styles to different types of headings (Level 2, Level 3) and text:
// Set the default paragraph and heading properties.
#set par(first-line-indent: 0pt)
// Define a custom show rule for level 2 headings (e.g., "subpar").
#show heading.where(level: 1): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 2 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Define a custom show rule for level 3 headings (e.g., "subpar").
#show heading.where(level: 2): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 3 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Apply styles to other text elements.
#show heading.where(level: 2): set text(blue)
// Your content goes here, and it will be styled differently depending on its level.
= This is a Level 1 Heading (set with `heading`)
== This is a Level 2 Heading
=== This is a Level 3 Heading
Lorem ipsum dolor sit amet.
Explanation:
- The
show rule
for heading.where(level: 2)
applies the custom styling only to level 2 headings. For other types of headings (like heading
) or text, you would need separate show rules.
Example of applying styles to different types of headings (Level 1, Level 3) and text:
// Set the default paragraph and heading properties.
#set par(first-line-indent: 0pt)
= This is a Level 1 Heading
== This is a Level 2 Heading
=== This is a Level 3 Heading
Lorem ipsum dolor sit amet.
// Define a custom show rule for level 1 headings (e.g., "subpar").
#show heading.where(level: 1): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 1 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Define a custom show rule for level 3 headings (e.g., "subpar").
#show heading.where(level: 2): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 3 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Apply styles to other text elements.
#show heading.where(level: 2): set text(blue)
Lorem ipsum dolor sit amet.
// Define a custom show rule for Level 3 headings (e.g., "subpar").
#show heading.where(level: 1): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 3 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Apply styles to other text elements.
#show heading.where(level: 2): set text(blue)
Lorem ipsum dolor sit amet.
Explanation:
- The
set text(...)
command within a show rule is applied to all elements after the show rule. This means that if you apply this style to a level 1 heading (heading(level: 1)
), it will affect subsequent
content unless overridden by another show
rule.
Example of applying styles to different types of headings (Level 2, Level 3) and text:
// Set the default paragraph and heading properties.
#set par(first-line-indent: 0pt)
= This is a Level 1 Heading
== This is a Level 2 Heading
=== This is a Level 3 Heading
Lorem ipsum dolor sit amet.
// Define a custom show rule for level 1 headings (e.g., "subpar").
#show heading.where(level: 1): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 1 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Define a custom show rule for level 3 headings (e.g., "subpar").
#show heading.where(level: 2): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 3 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Apply styles to other text elements.
#show heading.where(level: 2): set text(blue)
Lorem ipsum dolor sit amet.
// Define a custom show rule for Level 3 headings (e.g., "subpar").
#show heading.where(level: 1): it => {
let size = 1em * 8 // Multiply by 8 to convert points to em units.
set text(size) // Set the font size for this specific Level 2 heading.
// Display a level 3 heading (e.g., "subpar") and its body
it.body
// Example: A horizontal line below the heading
h(1fr)
line(length: 100%, stroke: .5pt + black) // Adjust length as needed
}
// Apply styles to other text elements.
#show heading.where(level: 2): set text(blue)
Lorem ipsum dolor sit amet.
=== This is a Level 3 Heading
Lorem ipsum dolor sit amet.
Explanation:
- The
set text(...)
command within a show rule is applied to all elements after the show rule. This means that
Aaand it runs forever again. It’s fixated on headings now, even though I didn’t ask about them. So I will have to clear the context after every prompt?
The cleared run is more successful.
Answer
To create a blue rectangle in Typst, you can use the rect
function along with a custom color or specify a predefined color like blue
. Here’s how you can do it:
-
Using a Custom Color:
- You can define a custom color using
rgb
.
#set text(blue)
#rect(width: 2cm, height: 2cm) // This will be blue by default.
-
Specifying the Blue Color Directly:
If you want to ensure that your rectangle is exactly blue (and not a color similar to it), you can use the blue
predefined color:
#rect(width: 2cm, height: 2cm, fill: rgb("0095bd")) // This will be blue.
-
Using a RGB Value for Exact Color:
If you prefer to use an exact RGB value (e.g., #0095bd
), make sure it’s enclosed in double quotes:
#rect(width: 2cm, height: 2cm, fill: rgb("#0095bd"))
These methods will give you a rectangular block with the desired color.扫黑
Though at the end it included Chinese, I wonder why.