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$ 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$ Commented Mar 3, 2015 at 21:06
  • $\begingroup$ You could use something like "ABC\!\(\*StyleBox[\"D\",FontWeight->Bold]\)EFG". $\endgroup$ 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$ 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$ 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$ Commented Mar 3, 2015 at 21:28
  • $\begingroup$ @rax working on it... $\endgroup$ 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$ 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$ 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$ 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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.