3

I need to remove all \text generated by TeXForm in Mathematica.

What I am doing now is this:

MyTeXForm[a_]:=StringReplace[ToString[TeXForm[a]], "\\text" -> ""]

But the result keeps the braces, for example: for a=fx,

the result of TeXForm[a] is \text{fx}

the result of MyTeXForm[a] is {fx}

But what I would like is it to be just fx

Eleteroboltz
  • 131
  • 1
  • 2
  • 13

3 Answers3

2

You should be able to use string patterns. Based on http://reference.wolfram.com/mathematica/tutorial/StringPatterns.html, something like the following should work:

MyTeXForm[a_]:=StringReplace[ToString[TeXForm[a]], "\\text{"~~s___~~"}"->s]

I don't have Mathematica handy right now, but this should say 'Match "\text{" followed by zero or more characters that are stored in the variable s, followed by "}", then replace all of that with whatever is stored in s.'

UPDATE:

The above works in the simplest case of there being a single "\text{...}" element, but the pattern s___ is greedy, so on input a+bb+xx+y, which Mathematica's TeXForm renders as "a+\text{bb}+\text{xx}+y", it matches everything between the first "\text{" and last "}" --- so, "bb}+\text{xx" --- leading to the output

In[1]:= MyTeXForm[a+bb+xx+y]
Out[1]= a+bb}+\text{xx+y

A fix for this is to wrap the pattern with Shortest[], leading to a second definition

In[2]:= MyTeXForm2[a_] := StringReplace[
          ToString[TeXForm[a]],
          Shortest["\\text{" ~~ s___ ~~ "}"] -> s
        ]

which yields the output

In[3]:= MyTeXForm2[a+bb+xx+y]
Out[3]= a+bb+xx+y

as desired. Unfortunately this still won't work when the text itself contains a closing brace. For example, the input f["a}b","c}d"] (for some reason...) would give

In[4]:= MyTeXForm2[f["a}b","c}d"]]
Out[4]= f(a$\$b},c$\$d})

instead of "f(a$\}$b,c$\}$d)", which would be the proper processing of the TeXForm output "f(\text{a$\}$b},\text{c$\}$d})".

Michael
  • 1,367
  • 10
  • 18
  • Ok... I get it... But it is not working in the case that I have many `\text{_}` in the equation. Can you help me with that? – Eleteroboltz Aug 02 '13 at 18:17
  • What does happen when you run it? For example, what is the output of MyTeXForm when a="x=5\\text{m/s}+2\\text{m/s}"? Am I correct that you would like the output to be "x=5m/s+2m/s"? – Michael Aug 02 '13 at 19:00
  • I've updated my answer with a solution that will hopefully work in many cases, but without knowing the reason why you want to do this, I'm not sure I can help much further off the top of my head... Likely regular expressions can help out with an even more complete solution if your text will contain "}" characters. – Michael Aug 02 '13 at 21:01
  • Michael, please consider directing users to [the new site.](http://mathematica.stackexchange.com/) (+1 on this answer, btw) – Mr.Wizard Aug 05 '13 at 21:25
  • Mr.Wizard, yeah, I should have thought of that! Will do so next time I'd answer something like this on SO... Is there a way to move the question over, or should I just comment that they might want to ask again over there? – Michael Aug 06 '13 at 17:07
0

This is what I did (works fine for me):

MyTeXForm[a_] := ToString[ToExpression[StringReplace[ToString[TeXForm[a]], "\\text" -> ""]][[1]]]
Zoe
  • 27,060
  • 21
  • 118
  • 148
paolo
  • 1
0

This is a really late reply, but I just came up against the same issue and discovered a simple solution. Put a space between the variables in the Mathematica expression that you wish to convert using TexForm.

For the original poster's example, the following code works great:

a=f x
TeXForm[a]

The output is as desired: f x

Since LaTeX will ignore that space in math mode, things will format correctly.

(As an aside, I was having the same issue with subscripted expressions that have two side-by-side variables in the subscript. Inserting a space between them solved the issue.)

Loki-Freya
  • 21
  • 3