Make one letter bold in text component

Dear all,

Is there a way to make one part of the text in the text component in the builder bold? Couldn’t find the right information on how to do this. Is there a way (in the component or otherwise in a new code component?)?

Best regards, Sanne

If this template helps then use it. If not then just delete and start from scratch.

OS (e.g. Win10):
PsychoPy version (e.g. 1.84.x):
Standard Standalone? (y/n) If not then what?:
What are you trying to achieve?:

What did you try to make it work?:

What specifically went wrong when you tried that?:
Include pasted full error message if possible. “That didn’t work” is not enough information.

Unfortunately there is not, bold is a property of the entire text component, not of each letter, so you can only set it to be either all bold or all not. Now that we have a Textbox component which deals with each letter as it’s own “glyph”, this is more possible to implement, but the Textbox is still very new and only in Beta.

You could create a separate component just for the letter you want to be bold and line it up to precisely where it should be, but you’d have to be very careful with Position, especially if you’re conducting this experiment across multiple different displays.

Thank you for your response. Good to know then I will not spend any more time looking for solutions. I will try and otherwise just not make it bold, the option with space sounds as if they could cause errors/ugly screens.

However, if I want to make text bold, how can I do this in the text component? I tried different codes but the programm did not react. Or should this be in a code component?

If you replace your Text component with the new Textbox component then that has an attribute called “bold”, currently it’s under Format but (especially as it’s in Beta) we may be moving it soon.

Addendum: in the new TextBox2 you can make a single letter bold as well! I’ve set it to use the old-school html tags of <b></b> around the letters you want bold (and similarly <i></i> for italics). Note that this is a “fake” bold/italic where we don’t actually use the font designer’s bold character. Instead we take the regular character and just make it 10% wider than usual!

@ TParsons and jon

That sounds great. Where can I find this new text component? I only see one in the Builder… (version 3.2.4)

I think, you’d need to use a code component. I couldn’t find online documentation, but from the interactive shell in the coder view, you can read the docstring with:

from psychopy import visual
help(visual.TextBox2)
1 Like

This is added in 2020.2 and documentation is to follow

1 Like

Hi Jon,

Is there a way to also get this to work with text pulled from a conditions file or code? I’m getting a syntax error whenever there’s a $ in the textbox.

Ah, that’s a bit of confusion over the use of $

$ doesn’t quite mean “this is a variable” it means “treat this (all) as code”

In your case the code you want would be one of the following (they are equivalent):

  • "<i> "+Word+" </i> - "+captured_string
  • "<i> %s </i> - %s" %(Word, captured_string)
  • "<i> {} </i> - {}".format(Word, captured_string)
  • f"<i> {Word} </i> - {captured_string}"

If you need JavaScript to work then you should use the first one. The last one is the neatest but requires Python3 (which you probably have) and you need to note the little f at the beginning of the quotation. These are known in python as f-strings or formatted strings.

Take one of those options and precede the whole thing with $ in your dialog box like this:
$f"<i> {Word} </i> - {captured_string}"

2 Likes