Message Boards Message Boards

1
|
3972 Views
|
4 Replies
|
5 Total Likes
View groups...
Share
Share this post:

How to color multiple selections of text in list?

Posted 3 years ago

I have a number say

a=ToString[2^554]

and a list of numbers

dp=ToString/@{863, 877, 881, 883, 887}

and I wish locate all instances of those numbers in the string "a" and change the text colour of those numbers to Red.

I have tried using Style and FontColor but the first StringReplace inserts Tildes either side of the replaced number, it then won't save the replacement as a string so the second replacement and beyond fails, any help would be appreciated.

The following shows the tildes.

StringReplace[a, dp[[1]] -> Style[dp[[1]], FontWeight -> "Bold", Red]]
POSTED BY: Paul Cleary
4 Replies
Posted 2 months ago

Hello Everyone,

Both solutions to this thread work really nice for me in the front end. However if I export as an xls I get this in my cells

With Richard solution:

TemplateBox[List[StyleBox["\"Key\"", Rule[FontColor, GrayLevel[0]], Rule[StripOnInput, False]], Rule[FrameStyle, None], Rule[ContentPadding, False], Rule[FrameMargins, 0], Rule[Background, RGBColor[0.99609375`, 0.92578125`, 0.328125`]]], "Highlighted"]

and with Rohit solution: VFJW

InterpretationBox[Cell[BoxData[FormBox[StyleBox["\"4\"", RGBColor[1, 0, 0], Rule[StripOnInput, False]], TextForm]], "InlineText"], Text[Style["4", RGBColor[1, 0, 0]]]]

My goal is to have something like VFJW4 with the (4) in red or other color in the same cell.

I will do this later with
"The cat is Blue". and will like to have "Blue" in the same sentence so that the user can pick up the differences easy.

Does anyone know what is going on with the Export?

Rodrigo

POSTED BY: Rodrigo Amor
Posted 3 years ago

Thank you Rohit and Richard, both solutions were ideal and solved my problem.

POSTED BY: Paul Cleary

There's a ResourceFunction that does something similar: https://resources.wolframcloud.com/FunctionRepository/resources/HighlightText

enter image description here

POSTED BY: Richard Hennigan
Posted 3 years ago

Hi Paul,

The reason for this is that Style is not a String. Conver it to String.

StringReplace[a, dp[[1]] -> ToString[Style[dp[[1]], Red], StandardForm]]

To color all of them

StringReplace[a, n : dp :> ToString[Style[n, Red], StandardForm]]
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract