3
$\begingroup$

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?

$\endgroup$
10
  • $\begingroup$ 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}}] $\endgroup$
    – Kuba
    Commented Mar 3, 2015 at 21:02
  • 2
    $\begingroup$ @Kuba That is certainly related. I wonder if it is a duplicate? Your Accepted answer there does not produce String output however. $\endgroup$
    – Mr.Wizard
    Commented Mar 3, 2015 at 21:06
  • $\begingroup$ You could use something like "ABC\!\(\*StyleBox[\"D\",FontWeight->Bold]\)EFG". $\endgroup$
    – Szabolcs
    Commented Mar 3, 2015 at 21:07
  • $\begingroup$ @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 $\endgroup$
    – Kuba
    Commented Mar 3, 2015 at 21:08
  • $\begingroup$ @Szabolcs That also produces errors with "Copy as LaTeX" in Mathematica 10.0.2. Is this operation even possible? $\endgroup$
    – Mr.Wizard
    Commented Mar 3, 2015 at 21:55

3 Answers 3

5
$\begingroup$

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 *)
$\endgroup$
4
  • 1
    $\begingroup$ This seems to break LaTeX copying. All I get is \text{ . $\endgroup$
    – orome
    Commented Mar 3, 2015 at 21:28
  • $\begingroup$ @rax working on it... $\endgroup$
    – Mr.Wizard
    Commented Mar 3, 2015 at 21:33
  • $\begingroup$ 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). $\endgroup$
    – orome
    Commented Mar 3, 2015 at 21:39
  • $\begingroup$ @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? $\endgroup$
    – Mr.Wizard
    Commented Mar 3, 2015 at 21:44
2
$\begingroup$
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

$\endgroup$
1
  • $\begingroup$ This generates non standard LaTeX, such as \unicode. $\endgroup$
    – orome
    Commented Mar 3, 2015 at 21:35
0
$\begingroup$
str = "CDABOZ";

p = {{2}, {4}};

A variant of Mr.Wizards answer using ReplaceAt (new in 13.1)

StringJoin @ ReplaceAt[x_ :> ToString[Style[x, Bold], StandardForm], p] @ Characters[str]

enter image description here

ReplaceAt has the advantage that we can easily impose conditions:

str = "CDAbOZ";

StringJoin @
 ReplaceAt[x_?UpperCaseQ :> ToString[Style[x, Bold], StandardForm], p] @ 
   Characters[str]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.