2
$\begingroup$

For reasons unknown, TeXForm seems to produce different results when acting on symbols that are one character long versus longer characters:

TeXForm[f]
f
TeXForm[fc]
\text{fc}

The question is is there a way to suppress this adding of the \text{} wrapper around symbols that are more than one character long, either in general, or can I at least redefine it for a particular symbol?

$\endgroup$
  • 1
    $\begingroup$ It's a bad idea to do this because TeX will then treat each letter in the symbol as a separate variable name (even when there aren't any spaces). That affects the typesetting. Is this really what you want? Or do you just want the symbol to be italicized in the TeX output while still being treated as a unit? $\endgroup$ – Jens Aug 4 '18 at 20:30
  • $\begingroup$ I know what TeX does, and whether there should be a \text{} wrapper around a symbol depends on the context, and it may or may not be a bad idea. In the particular context that I'm working in, having \text{} appear in several hundred places (which I then have to manually erase) doesn't help. $\endgroup$ – Bojan Aug 6 '18 at 12:43
  • $\begingroup$ Then this could be considered a duplicate of the more general Q&A: Format and TeXForm does not work as expected. It also explains the code in Carl's answer. $\endgroup$ – Jens Aug 6 '18 at 16:23
2
$\begingroup$

TeXForm works by converting the expression to TraditionalForm boxes, and then processing those boxes. The TraditionalForm boxes for fc is "fc", while the TraditionalForm boxes for a string like "fc" is \""fc\"". The following code modifies the TeX conversion rules for strings that come from symbols:

System`Convert`TeXFormDump`maketex[s_String] /; !stringQ[s] := Replace[
    s,
    {
        n_ /; StringMatchQ[n, NumberString] :> n
    }
]

stringQ[s_] := StringMatchQ[s, "\""~~___~~"\""] || !SyntaxQ[s, TeXForm]

Your example:

ToString[fc, TeXForm]

"fc"

$\endgroup$
  • $\begingroup$ Perfect, thanks! Can't say that I understand what the code does :-), but it does the job :-)! $\endgroup$ – Bojan Aug 6 '18 at 12:48

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.