How to Set Text Format by User-Defined Function with Input as String

I have simple but annoying problem here. I guess it occurs to many Mathematica users.

The thing is that I wish to define a function to format the text in Mathematica. Yet, I don't know whether it is possible.

Say if I want to make a plot with FrameLabel set, like the following script:

Show[
Plot[x^2, {x, 0, 1}, PlotRange -> {{0, 1}, {0, 1}}], Frame -> True, AspectRatio -> 0.8,
FrameLabel -> {Style[ToExpression["x", TeXForm, HoldForm], 18,FontFamily -> Times, Bold],
Style[ToExpression["x^2", TeXForm, HoldForm], 18, FontFamily -> Times, Bold]},
RotateLabel -> False
]


which generate this plot

The thing is I have type this bothering format setting script again and again if I need to plot a lot, though the format I set every time is the very same.

Style[ToExpression["x^2", TeXForm, HoldForm], 18, FontFamily -> Times, Bold]}


Therefore, I am wondering if I can define a function to overpass such prolix script. For example,

SetStyle[input_] := Style[ToExpression[ToString[input, InputForm], TeXForm, HoldForm],18,FontFamily -> Times];


It works for superscript, but it fail in the most of the Latex input, like \\theta or \\frac{x}{2} and even just the subscript like x_0

anyone know how I can fix my code?

Thanks for your kind and generous suggestion.

• Have you see the MaTeX paclet? Jul 13 at 9:35
• One way is to define a function like st[txt_String] := Style[ToExpression[txt, TeXForm, HoldForm], 18, FontFamily -> Times, Bold] and then the option FrameLabel look as follows: FrameLabel -> {st["x"], st["x^2"]}. Jul 13 at 10:07
• @LukasLang Yap, i tried it before. I'm not sure whether i did it wrong, i experienced some problems when installing. Yet, I believe MaTex the first option of most of users. Thanks for your comment!
– Yuko
Jul 14 at 16:36
• @AlexeiBoulbitch Amazing! it is the solution i am looking for. This is really clever! I did not know such input argument until you show it! Thank you very much!
– Yuko
Jul 14 at 16:39

First, I would like to give this credit to Alexei Boulbitch. He made a little but critical amend in the input argument of my original code.

The function should be defined as following , instead,

st[txt_String] := Style[ToExpression[txt, TeXForm, HoldForm], 18, FontFamily -> Times, Bold]


In this way, the input of the function could be a string and the problem mentioned above could be perfectly solved!

For example, one can set the FrameLabel like given by Alexei

FrameLabel -> {st["x"], st["x^2"]}


It works even for a complicate LaTeX input, for example,

FrameLabel -> {st["\\frac{x}{C}"], st["\\phi_0+\\theta_1"]}


Once again, thank you @AlexeiBoulbitch