2
$\begingroup$

I have a list with strings:

list = {"USA", "EU", "BRICS", "OECD", "NAFTA"};

Now I want to use StringReplaceto add a footnote with Superscript:

StringReplace[list, "EU" -> Superscript["EU", "Footnote"]]

but the outcome is enter image description here

What is the problem here?

$\endgroup$
1
  • 2
    $\begingroup$ StringReplace replaces within strings and your replacement does not procude a string. Use ReplaceAll if you want to replace a whole string. $\endgroup$
    – Kuba
    Jan 27 '20 at 14:34
2
$\begingroup$

A basic example from the documentation of StringReplace

Replace every occurrence of "ab" with "X":

StringReplace["abbaabbaa", "ab" -> "X"]

(*XbaXbaa*)

So StringReplace doesn't replace whole strings, but rather patterns within strings themselves. Since strings are atomic in Mathematica, this allows you to sidestep, for example, first converting a string to a list of its characters (with Characters) and then going about your manipulations that way.

Since you're not working with a single string, I would use ReplaceAll as

{"USA", "EU", "BRICS", "OECD", "NAFTA"} /. "EU" -> Superscript["EU", "Footnote"]

{"USA", Superscript["EU","Footnote"], "BRICS", "OECD", "NAFTA"}

$\endgroup$
2
$\begingroup$

You can post-process the output to remove the heads StringExpression:

StringReplace[list, "EU" -> Superscript["EU", "Footnote"]] /.
    StringExpression->Identity

enter image description here

StringReplace[list, 
  "EU" -> Superscript["EU", Style["Footnote", 16, Red, FontFamily -> "Felipa"]]] /. 
  StringExpression -> Identity

enter image description here

Alternatively, you can wrap the rhs of the replacement rule with ToString[#,StandardForm]&

StringReplace[list, 
 "EU" -> ToString[Superscript["EU", "Footnote"], StandardForm]]

enter image description here

StringReplace[list, 
  "EU" -> ToString[Superscript["EU",  Style["Footnote", 16, Red, FontFamily -> "Felipa"]], 
   StandardForm]]

enter image description here

The second method works as is for replacing parts of strings:

list2 = {"USA", "EUROPE", "BRICS", "OECD", "NAFTA"};
StringReplace[list2, "EU" -> ToString[Superscript["EU", "Footnote"], StandardForm]]

enter image description here

StringReplace[list2, 
 "EU" -> ToString[Superscript["EU", Style["Footnote", 16, Red, FontFamily -> "Felipa"]], 
   StandardForm]]

enter image description here

For the first method to work we need to change the replacement rule in ReplaceAll to

rule = StringExpression -> (StringJoin[ToString[#, StandardForm] & /@ {##}] &);

Then

StringReplace[list2, "EU" -> Superscript["EU", "Footnote"]] /. rule

enter image description here

StringReplace[list2,  "EU" -> 
  Superscript["EU", Style["Footnote", 16, Red, FontFamily -> "Felipa"]]] /. rule

enter image description here

$\endgroup$

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.