4
$\begingroup$

I want to do something converse to this. Namely, I want to force the output of TeXForm[] to be always presented with a division symbol / instead of \frac{}{}.

Can one do it using TraditionalForm as in the answer to the question I posted?

$\endgroup$

2 Answers 2

1
$\begingroup$

You can add conversion rules like this:

Convert`TeX`ExpressionToTeX[
 a/beta,
 "BoxRules" :> Append[
   System`Convert`TeXFormDump`$GreekWords, 
   FractionBox[num_, den_, ___] :> 
    System`Convert`TeXFormDump`MakeTeX[num] <> "/" <> 
     System`Convert`TeXFormDump`MakeTeX[den]]]

(* "a/\\beta" *)

The problem is that \frac doesn't parenthesize, so it does not work on something like a/(b+c). Neither does it use TeXForm. If you need TeXForm, you have to permanently change $GreekWords.

$savedGreekWords = System`Convert`TeXFormDump`$GreekWords;
Protect[$savedGreekWords];
parenthesizeBoxes // ClearAll;
parenthesizeBoxes[boxes_RowBox] := 
  "\\left(" <> System`Convert`TeXFormDump`MakeTeX[boxes] <> "\\right)";
parenthesizeBoxes[boxes_] := System`Convert`TeXFormDump`MakeTeX[boxes];
myTeXBoxRules = 
  Join[System`Convert`TeXFormDump`$GreekWords, {FractionBox[num_, den_, ___] :>
      parenthesizeBoxes[num] <> "/" <> parenthesizeBoxes[den]}];

Using it with TeXForm:

System`Convert`TeXFormDump`$GreekWords = myTeXBoxRules;
a/(b + c) + (a + c)/b // TeXForm
System`Convert`TeXFormDump`$GreekWords = $savedGreekWords;

(* a/\left(b+c\right)+\left(a+c\right)/b *)
$\endgroup$
4
  • $\begingroup$ This is a great solution, thank you. Is it also possible to replace \left(\right) brackets with usual () brackets? $\endgroup$ Feb 25 at 19:00
  • $\begingroup$ @PawełKorzeb Sure, just change the string "\\left(" to "(" and similarly for the other (in parenthesizeBoxes). Whatever you put there will end up in the TeX form output. Note that the parentheses won't scale if you leave off "\\left" and "\\right". If you don't need that, then omit them. Theoretically, one could even cycle through delimiters (e.g. (), [], {}) for nested parenthesization. $\endgroup$
    – Goofy
    Feb 25 at 20:49
  • $\begingroup$ Unfortunately, it works only partially. In the case of consecutive brackets \left(\right) is still produced in the output $\endgroup$ Feb 26 at 18:01
  • $\begingroup$ @PawełKorzeb I don't know what you mean. Other TeX rules, other than the one for fractions above, use \left and \right. The built-in rules Mathematica almost always put them in, letting TeX figure out whether the delimiters need to be increased in size. Is that what you mean? You might have to change quite a few rules, assuming none of them are hardcoded in internal functions. It would be easier to get the TeX string and remove them afterwords, if you want to remove all instances. $\endgroup$
    – Goofy
    Feb 26 at 20:10
8
$\begingroup$

It is much easier to do this directly in your latex document where you will compile the latex generated by Mathematica TeXForm.

Once you copy your Mathematica generated Latex to your Latex document, just add this command in the preample, which will automatically convert all \frac{}{} to /

Lets say you did

TeXForm[a/c + Exp[r/d]]

Which generates

\frac{a}{c}+e^{\frac{r}{d}}

In your Latex document load \usepackage{nicefrac} and renew the command \frac to automatically use nicefrac, like this

\documentclass{book}
\usepackage{amsmath}
\usepackage{nicefrac}
\renewcommand{\frac}[2]{\nicefrac{#1}{#2}}

\begin{document}

\begin{align*} 
\frac{a}{c}+e^{\frac{r}{d}}
\end{align*} 
\end{document}

Which gives

enter image description here

If you change your mind later, and do not want this, just comment the renew command and now you will get back the original output

enter image description here

$\endgroup$
1
  • $\begingroup$ This is a very nice answer, thank you. However, I explicitly wanted TexForm[] to generate the expression with division signs / because the latex expressions I have are very lengthy, and I want to use dmath environment from breqn package to automatically break the line. \frac{}{} prevented dmath from automatic line breaking because of the fraction structure. Your solution indeed prints / instead, but raw code still has \fracs, and line breaking with dmath still doesn't work. Can one tune this solution so it works with dmath? $\endgroup$ Feb 25 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.