When working with complicated expressions I would like to easily export specific expressions to LaTex.

The ideal solution for me is the TeXForm command, but many times when using it I would like to customize the output.

For example TeXForm produces enter image description here There are several things I would like to change about the about output

  • removing \text around the variables
  • changing \bar to \overline when needed.
  • removing \left and \right
  • changing \varepsilon to \epsilon

My question is

Is there a general way that I can parse the output of TeXForm (or any LaTex output method) so that it is format it to my preferences?

  • 4
    $\begingroup$ probably it is not possible to do all of these, but for some, you can check this is-it-possible-to-change-customize-some-conversions-done-by-texform $\endgroup$ – Nasser Jan 24 '18 at 18:35
  • $\begingroup$ @Nasser Is there a general method of parsing strings in Mathematica? e.g. The mathematica equivalent to docs.python.org/2/library/string.html $\endgroup$ – AzJ Jan 24 '18 at 18:47
  • 2
    $\begingroup$ Yes, you can use either StringPatterns or you can also use standard RegularExpression as well. $\endgroup$ – Nasser Jan 24 '18 at 18:49
  • $\begingroup$ \[Epsilon] // TeXForm gives \epsilon; \[CurlyEpsilon] // TeXForm gives \varepsilon. You get what you inserted. Moreover, what's wrong with \left and \right? $\endgroup$ – corey979 Jan 24 '18 at 18:54
  • $\begingroup$ Nothing is wrong with \left and \right but sometimes I prefer that they are removed as they make very long Tex expressions hard to read. $\endgroup$ – AzJ Jan 24 '18 at 19:12

I came up with a work around for my problem, please comment or edit this question if you have suggestions.

For example I starting with the expression: enter image description here

I can use the a variety of replacement rules and a function

exper // TraditionalForm
repRules = {
   "\\varepsilon" -> "\\epsilon",
   "\\epsilon " -> "\\epsilon",
   "\\delta " -> "\\sigma",
   "\\text{B1}" -> "B_1",
   "\\text{B2}" -> "B_2",
   "\\text{K1}" -> "K_1",
   "\\text{K2}" -> "K_2",
   "\\text{d1}" -> "d_1",
   "\\text{r1}" -> "r_1",
   "\\text{r2}" -> "r_2",
   "\\text{d1}" -> "d_1",
   "\\text{d2}" -> "d_2",
   "\\left" -> "",
   "\\right" -> "",
   "i " -> "I "};
ToTeX[equation_] := 

to get my desired output enter image description here

Is there any way to change it so that Mathematica outputs plain text?(this eliminates an extra step)


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.