How can I apply different styles within one line of text?

Hello Forum,
I want to use different styles for word within 1 line of text, for example:

#set text(weight: "bold")
First words
#set text(weight: "regular")
Next words

But when I use this example I get them in 2 lines. It seems that after changing the weight of the text back to regular also a linebreak is added. Is there also a shortcut similiar to the italics where I can use the underscore to toggle it on and off?

Thank you very much in advance,
Marco

Hi Marco,

Like italics, strong emphasis (bold to laymen like me) has a Markdown shortcut: the * symbol:

*First words* next words

For more Markdown shortcuts, see the Syntax page.


Please see How to post in the Questions category.
Specifically the part about including code in your post:

If your question pertains to a bit of Typst markup you have problems with, put it in the question body! You can syntax-highlight Typst code by wrapping it in ` ```typ … ````. It will then look like this:

#set font(size: 12pt)

Hello from *Typst* at $ pi.var + 1 $ o'clock!

If you need to highlight a code or math mode snippet instead, the language tags typc and typm do that, respectively.

You would enter yours like this:

```
#set text(weight: “bold”)
First words
#set text(weight: “regular”)
Next words
```

and it will appear like this:

#set text(weight: “bold”)
First words
#set text(weight: “regular”)
Next words

If you are looking to avoid Markdown for some reason, this is also an option:

#text(weight: "bold")[First words] #text[next words]
2 Likes

Thank you for the fast reply. Excuse me for not using the ‘’’ for the coding block in my post. I just joined here last week and still ned to find out the basics.
Will using the asterisks generate the same weight as using bold? I will checkout the solution you posted at the end of your answer.

Not really.

#set text(font: "Noto Serif")
#strong[*First* words] next *words*

The *strong* increases the weight by 300 by default, that is regular → bold or semibold → black. You can configure the value with the delta parameter.

Variant Details
"thin" Thin weight (100).
"extralight" Extra light weight (200).
"light" Light weight (300).
"regular" Regular weight (400).
"medium" Medium weight (500).
"semibold" Semibold weight (600).
"bold" Bold weight (700).
"extrabold" Extrabold weight (800).
"black" Black weight (900).

(The above table is copied from Text Function – Typst Documentation)

1 Like

Will using the asterisks generate the same weight as using bold?

By default, yes, using * has the same effect as #strong() and the same effect as specifying a bold weight in #text(). This requires a couple pieces of information to prove:

  1. *text* is equivalent to #strong[text]
  2. strong() has a default delta of 300
  3. Regular text has a weight of 400
  4. #text(weight: "bold") has a weight of 700
  5. 700 - 400 = a delta of 300

Long story short, by default these are all equivalent:

*test*
#strong[test]
#text(weight: "bold")[test]
#text(weight: 700)[test]
1 Like

In the meantime I tested out the version with the Asterisks. But it still adds a new line after the second asterisk. So this Markdown-Syntax is not working for me. Now I’ll try out the other one with

#set text(weight: “bold”)
First words
#set text(weight: “regular”)
Next words

and I hope this works better. Can I freely use those commands with others like font size, color, alignment etc. without any order or do I need to use them like in HTML where I need to “nest” them?

Thanks again for helping a typst-newbie,
Marco

Could you share a screenshot of your current result?

I’ve tried your original typst code, and it just works.
Image

Do you have any additional code that you didn’t tell us? Did you use any template or import any package?

This doesn’t compile as the quotes are invalid. Only " work for strings.

Hello again,
I wrote a VBA script in Excel that reads data from a spreadsheet and generates a textfile with the typst-code which I then put into the typst-app. The only plugin I use is the in-dexer plugin.

Here is a code-snippet that works:

#set text(weight: "regular")
#set text(12pt)
#set align(center)
*Name1- Name2\ 
#index1["Name1"]
#index1["Name2"]
#set align(left)
#set text(8pt)
\<2\>\ *
#set align(left)
*Name: Name1*, Firstname1, <1698> <2>

I toogle-on bold in line 4 and off at the end of line 9. Then I again toggle-on in the last line and off after the first word there. If I try to remove the toggles in line 9 and at begin of the last line then it creates another linebreak after “Name1” in the last line. That’s confusing for me.

Another strange effect: If I remove the statement in line 10 (because I have the same one in line 7) then the following text is centered.

My question now: Are all “#set-Commands” only valid for the next single text-paragraph?

Thanks again,
Marco

It is set align(…) to be blamed. align controls block-level alignment, so you’re creating a new block by writing set align(…).

Quoting Align Function – Typst Documentation

The align function performs block-level alignment and thus always interrupts the current paragraph. To have different alignment for parts of the same line, you should use fractional spacing instead:

Set rules on text has nothing to do with your case…


Besides, you could put a string like #"<2>". This might be easier to type than putting a content and escaping everything like \<\2\>.

I’m afraid that’s not true. There is no variable index available in this block of code so it does not compile.


You are mixing Markdown and Code in a way that is surprising to me.
These are both equivalent:

*text*
#strong[text]

So when you include #set commands in the bolded text, it looks like this:

#strong[
  text1
  #set align(center)
  text2
]

This means that the effect of the #set exists only within this scope (everything between [ and ], here’s a Wikipedia article about it).

I would recommend being careful about what you include inside your *s. And any formatting (the #set commands) should be done beforehand.


Example of why alignment does not do what you expect
#set align(center)
Center aligned

*
  Center - bold
  #set align(left)
  Left - bold
*
Center aligned

image

1 Like

Thanks again for the fast answers. I only use typst since last friday and I still have to understand the syntax.
So, if I put certain parts of texts between brackets then all the #set-commands I made before will affect this text?

Well, the short answer is yes. Here’s an example:

#set text(10pt)
A // 10pt
#[
  B // 10pt
  #set text(12pt)
  D // 12pt
]
E // 10pt

Have you read the third part of the tutorial (Advanced Styling – Typst Documentation)? This page should have covered scoped styling.

I read it but some concepts I still don’t understand. I never used a tool like this before and my coding skills are also a little rusty but I think that typst will be ideal for my recent project. The last one I made using MS Word but having a text with 800+ pages at the end almost crashed it. I will rearrange the statements I generate and see which errors then get after following your tips.

Thanks again,
Marco

1 Like

After I re-arranged my statements now the code looks like it should. Thanks for all your help. I now can close this topic.

1 Like

Hi @Marco1, I’m glad you found a solution. The answer you choose should usually be the response that you found most correct/helpful/comprehensive for the question you asked, not the last message in the thread (the message you mark as answer appears at the bottom of the initial post in the thread, so that future readers can navigate directly to it).

Please either select one of the posts that contain the relevant information, or add the solution to your post and quickly describe it; see also How to post in the Questions category—Answering. For now, I have removed the checkmark from your post.

1 Like

My code now produces something like this:

#set text(12pt)
#set align(center)
*Name1 - Name2*\ 
#index1["Name1"]
#index1["Name2"]
#set align(left)
#set text(8pt)
*\<2\>\ *
#set align(left)
*Name: Name1*, Firstname1, <1698> <2>

I now add the asterisks each time I need a single word or phrase instead of trying to use them for larger pieces of text.

image

The second set align(left) doesn’t affect alignment. It breaks inline flow and adds vertical space by splitting text into 2 paragraphs.

In my case it works like I wanted: