Use $ symbol and mulitple colors in the text

Hi, what I am trying to do is:

  1. let the prog to do calculation based on subjects’ choices in the previous loop and stored in the generated excel named “budget”,
  2. in the instruction for next loop(block), I want to write “in the following block, you have $budget in total” in the text. The $budget will differ among subjects.

however I found the $ does not function in the text stim…
in my case, are there other possible solutions?

ha, I tried:

text='take a break!\nin the following block, you have %d s to spent.\npress"space" to continue' %(budget1),

and it worked…
but still curious about how to use $ in the text?

Q2: how to selectively set some words as bold or change the color into red in one text?
I tried:

'In this following block, you have' + '\033[1m' + '20s' + '\033[0;0m' +'in total to spend.\nTo start, please press "space"\n\nplease remember:\nleft : press "A"\nright: press "L"'

but does not work…
I found a post provided a solution, but not sure understand it correctly
http://stackoverflow.com/questions/21026487/text-with-multiple-colors-in-psychopy

Thanks a lot! :blush:

1 Like

FYI The dollar sign indicates that the entire box is python code, rather than indicating the name of a variable (that’s a common misconception). That’s why writing $budget inside a string doesn’t achieve what you expected.

To add a dollar symbol literally into the string you need to use \$ instead of just $

You can’t alter formatting within a string at the moment I’m afraid. The link you point to indicates that you have to create multiple objects for each font, which is obviously a pain.

2 Likes

I got it! Thanks a lot, Jon! :blush:

Hi Jon,

“You can’t alter formatting within a string at the moment I’m afraid.”

Is this still the case? I’m writing an experiment in which I have prompt words that signal a secondary task. I wanted to draw the prompt words red after waiting a short period. That seems very difficult, if not impossible. Am I right?

Yes.

This can be done, but you need a different text stimulus for each word.

Thanks, Michael. I had envisioned that solution, but decided it’s not worth the effort to get everything together and looking good :slight_smile:

Hi @jon we are on version 2021.2.1

When we put this in the textStim:
A) \$20,000 to just under \$40,000
It displays
A) /$20,000 to just under /$40,000
on the screen. It’s displaying a front slash. Is there anything we can do or did we find a bug?
Best,
David

I’m guessing this is some bug to do with trying to correct broken path names (windows file paths with \ that need to be /) that’s being done incorrectly to prevent escaping of characters. We’ll look into that.

As a workaround, could you do this (use the dollar write code and then insert dollars into the coded string:
$"$20,000 to just under $40,000"

By the way, I think this is not the same as the rest of the thread. In future it would be better to give this a new topic with a more suitable title :slight_smile:

Ah yep, I can see the exact line where this happens… We do a similar substitution for file type inputs (such as the “Image” param of an :image: Image component), but due to older experiments not having file type inputs marked as such, it needed to be done on string type inputs (like the “Text” param) too. I can see a way to handle this better, so will implement it for the next bug fix release :slight_smile: