1
$\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$
  • $\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 Mar 3 '15 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 Mar 3 '15 at 21:06
  • $\begingroup$ You could use something like "ABC\!\(\*StyleBox[\"D\",FontWeight->Bold]\)EFG". $\endgroup$ – Szabolcs Mar 3 '15 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 Mar 3 '15 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 Mar 3 '15 at 21:55
3
$\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$
  • 1
    $\begingroup$ This seems to break LaTeX copying. All I get is \text{ . $\endgroup$ – orome Mar 3 '15 at 21:28
  • $\begingroup$ @rax working on it... $\endgroup$ – Mr.Wizard Mar 3 '15 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 Mar 3 '15 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 Mar 3 '15 at 21:44
1
$\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$
  • $\begingroup$ This generates non standard LaTeX, such as \unicode. $\endgroup$ – orome Mar 3 '15 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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