2

I have a string of characters, like

"CDABOZPVRYXSWQEGNILUTHMKJF"

and want to convert it to a string in which the character at position p is bold.

After doing this I want to leave the result in a table that will be displayed in TableForm and then used for publications or export — e.g., copy as LaTeX — so the result needs to be (essentially) a formatted string with a single bold character.

Is there a way to accomplish this?

orome
  • 12,819
  • 3
  • 52
  • 100
  • Here you have more general solution: How to change the color of specified digits in a number?. You can use it like that: mark["CDABOZPVRYXSWQEGNILUTHMKJF", {{Red, ;; ;; 2}}] – Kuba Mar 03 '15 at 21:02
  • 2
    @Kuba That is certainly related. I wonder if it is a duplicate? Your Accepted answer there does not produce String output however. – Mr.Wizard Mar 03 '15 at 21:06
  • You could use something like "ABC\!\(\*StyleBox[\"D\",FontWeight->Bold]\)EFG". – Szabolcs Mar 03 '15 at 21:07
  • @Mr.Wizard Probably not a duplicate. The problem with keeping it as a string is that next time it is quite tough to specify position. Row is not a the same head too ;P – Kuba Mar 03 '15 at 21:08
  • @Szabolcs That also produces errors with "Copy as LaTeX" in Mathematica 10.0.2. Is this operation even possible? – Mr.Wizard Mar 03 '15 at 21:55
  • @Mr.Wizard: Indeed. It looks like Mathematica just generates garbage for LaTeX even in this simple case. It can't seem to recognized a bold character and just generate \textbf{x}. – orome Mar 03 '15 at 22:07
  • I suggest you either post a new question or edit this one to specifically address that problem. The Copy as LaTeX command probably will not be usable but other export may be, given adequate "massaging" of the expressions. – Mr.Wizard Mar 03 '15 at 22:21
  • @Mr.Wizard: Not sure what you mean: the question includes the LaTeX bit. That's the goal here: not merely to get it "bold-looking" but to have the result in a format that can be used as LaTeX. There's no separate question about just making it appear bold. – orome Mar 03 '15 at 22:24
  • Based on the way your question is written I assumed that if I returned a styled String it would be a solution. However since Mathematica 10.0.2 seems incapable of copying a styled string as LaTeX this is a much different problem. I suggest that you start with something like "How can I export a styled string to LaTeX?" instead. – Mr.Wizard Mar 03 '15 at 22:41
  • @Mr.Wizard: I see what you mean. I'll focus this on the formatting (since that's how the answers are focused) and ask a new question about getting the results into LaTeX. – orome Mar 03 '15 at 22:58

2 Answers2

4

This question is related to at least:

Fortunately it is simpler than the first one and we can apply the methods provided in the second one.

stringBold[s_String, pos_] :=
 "" <> MapAt[Style[#, Bold] ~ToString~ StandardForm &, Characters@s, pos]

stringBold["CDABOZPVRYXSWQEGNILUTHMKJF", 7]

enter image description here

The output is a String with and embedded Box form:

% // InputForm
"CDABOZ\!\(\*StyleBox[\"\\\"P\\\"\", Bold, Rule[StripOnInput, \
False]]\)VRYXSWQEGNILUTHMKJF"

Any specification that MapAt accepts can be used for parameter pos:

stringBold["CDABOZPVRYXSWQEGNILUTHMKJF", 2 ;; -3 ;; 3]

enter image description here

However the function cannot be applied to more than once, or to an already styled string:

stringBold[%, 3]  (* failure *)
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371
  • 1
    This seems to break LaTeX copying. All I get is \text{ . – orome Mar 03 '15 at 21:28
  • @rax working on it... – Mr.Wizard Mar 03 '15 at 21:33
  • Thanks. It would also be great if this could be applied again to the result (to set additional characters to bold of to other styles). – orome Mar 03 '15 at 21:39
  • @rax Are you expecting the Bold style to be preserved in LaTeX on copy? I do not see that working at all. If this is what you mean is Mathematica even capable of that? Or do you just mean that on copy you want a plain string in LaTeX? – Mr.Wizard Mar 03 '15 at 21:44
1
srF = StringReplacePart[#, ToString[Style[StringTake[#, {#2}], ##3], StandardForm], {#2, #2}] &;

str = "CDABOZPVRYXSWQEGNILUTHMKJF";
srF[str, #, Red, Bold, 16] & /@ {3,9}

enter image description here

Note: Copy as LateX does not work

kglr
  • 394,356
  • 18
  • 477
  • 896