4
$\begingroup$

When using ArcTan, the corresponding TeXForm is tan^{-1} and not \arctan which I would prefer for aesthetic reasons. How to fix that?

$\endgroup$
6
$\begingroup$

Using the code from my answer to Format and TeXForm does not work as expected (which I include at the bottom of this answer:

Format[ArcTan[x_], TeXForm] := arctan[x]

Then:

1 + ArcTan[x] //TeXForm

$\arctan (x)+1$

Here is the code:

Initial /: Verbatim[TagSetDelayed][Initial[sym_], lhs_, rhs_] := With[
    {
    new = Block[{sym},
        TagSetDelayed[sym, lhs, rhs];
        First @ Language`ExtendedDefinition[sym]
    ],
    protect = Unprotect[sym]
    },

    sym;
    Replace[new,
        Rule[values_, n:Except[{}]] :> (
            values[sym] = DeleteDuplicates@Join[n, values[sym]]
        ),
        {2}
    ];
    Protect@protect;
]

Initial[Convert`TeX`ExpressionToTeX] /: 
    Convert`TeX`ExpressionToTeX[e__] /; !TrueQ@$TeX := Block[
	{$TeX = True},
        Convert`TeX`ExpressionToTeX[e]
    ]

Initial[Format] /: TagSetDelayed[sym_Symbol, Verbatim[Format][x_, TeXForm], rhs_] := With[
    {fmt = TraditionalForm},

    Initial[sym] /: MakeBoxes[x, fmt] /; $TeX := MakeBoxes[rhs, fmt]
]

Initial[Format] /: SetDelayed[Verbatim[Format][x_, TeXForm], rhs_] := With[
    {s = getTagSymbol[Format[x, TeXForm]], fmt = TraditionalForm},
    Replace[s,
        HoldForm[tag_] :> (
            Initial[tag] /: MakeBoxes[x, fmt] /; $TeX := MakeBoxes[rhs, fmt]
        )
    ]
]

SetAttributes[getTagSymbol, HoldFirst]

getTagSymbol[Format[x_, TeXForm]] := Module[{dummy, t},
    extractTag[Hold[Message[Format::tag, HoldForm@Format, _, tag_], False]] := t = tag;
    Internal`HandlerBlock[{Message, extractTag},
        Quiet[dummy[1] /: Format[x, TeXForm] := 1]
    ];
    t
]

System`Convert`TeXFormDump`maketex[s_String] /; !StringMatchQ[s, "\""~~___~~"\""] && SyntaxQ[s, TeXForm] := Replace[
    s,
    {
        n_ /; StringMatchQ[n, NumberString] :> n,
        w_?wordQ :> "\\operatorname{"<>w<>"}"
    }
]

wordQ[s_String] := Length @ StringSplit[s, WordBoundary] == 1
| improve this answer | |
$\endgroup$
  • $\begingroup$ Looks good but it changes my $\text$ blocks into $\operatorname$... $\endgroup$ – Kutsubato May 31 '19 at 7:55
4
$\begingroup$

Usually the simplest methods tend to be the best. Try the code

expr = ArcTan[x/y] - ArcTan[z/w];
StringReplace[ToString[expr, TeXForm], "\\tan ^{-1}" -> "\\arctan"]

which returns

\arctan\left(\frac{x}{y}\right)-\arctan\left(\frac{z}{w}\right)

and in display form is

$$ \arctan\left(\frac{x}{y}\right)-\arctan\left(\frac{z}{w}\right) $$

| improve this answer | |
$\endgroup$
  • $\begingroup$ Should work but tends to be slow $\endgroup$ – Kutsubato May 31 '19 at 7:55

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.