I have a simple prime counting function using the Cyclotomic function.

myPi[n_] := Sum[KroneckerDelta[Cyclotomic[m, 1], m], {m, 1, n}]
myPi[1001] == PrimePi[1001]


When I copy as latex I get, $$\text{myPi} n\text{:=}\sum _{m=1}^n \delta _{C_m(1),m},$$ where I have to place $()$ around the $n$, which seems to be a bug.

However, my question: Is there an option to cause the $C$ to become the traditional $\Phi$, as below?

$$\text{myPi}(n)\text{:=}\sum _{m=1}^n \delta _{\Phi_m(1),m}.$$

• How are you using the Latex code? Are you not copying it to your Latex editor to use? Then why not simply in the editor, do global search and replace? Replace C_m with \Phi_m and that should do it? – Nasser Oct 8 '16 at 8:59
• @Nasser, that's what I do. This also occurs with a few other symbols, so I was wondering if I could set an option to use a substiture character. – Fred Kline Oct 8 '16 at 9:15
• I do not think there is a way to do it in Mathematica itself (I mean to tell it to change how the Latex is generated), but I could be wrong. I always did the replacements in the Latex editor each time. – Nasser Oct 8 '16 at 9:30
• @Nasser, I was thinking there might be a symbol synonym list to select from. – Fred Kline Oct 8 '16 at 9:32

A simple solution would be to define the function

\[CapitalPhi][m_, k_] := Cyclotomic[m, k]


myPi[n_] := Sum[KroneckerDelta[\[CapitalPhi][m, 1], m], {m, 1, n}]
myPi[1001] == PrimePi[1001]

(* Out[505]= True *)


Copying the first line as Latex gives the string

\text{myPi}(\text{n$\_$})\text{:=}\sum _{m=1}^n \delta _{\Phi (m,1),m}


which shows up as

$$\text{myPi}(\text{n\_})\text{:=}\sum _{m=1}^n \delta _{\Phi (m,1),m}$$

• I didn't think about puting a wrapper around it! +1 – Fred Kline Oct 8 '16 at 11:04

Conversion to $\TeX$ internally uses TraditionalForm boxes. $C$ is already used there:

Cyclotomic[m, 1] // TraditionalForm


$C_m(1)$

In definition of Cyclotomic:

?? Cyclotomic
(* ...
Cyclotomic/:MakeBoxes[Cyclotomic[BoxForma$_,BoxFormb$_],TraditionalForm]/;BoxFormsufficientVersionQ[6.1]:=
TemplateBox[{MakeBoxes[BoxForma$,TraditionalForm],MakeBoxes[BoxFormb$,TraditionalForm]},Cyclotomic]
... *)


we can see that conversion to TraditionalForm boxes use a TemplateBox with "Cyclotomic" style.

Inspection of "Cyclotomic" style:

CurrentValue[{StyleDefinitions, "Cyclotomic"}]
(* {TemplateBoxOptionsDisplayFunction ->
(RowBox[{SubscriptBox["C", #1], "(", #2, ")"}] &),
TemplateBoxOptionsTooltip -> Automatic} *)


reveals DisplayFunction, that is used by TemplateBox with this style, containing "C" that you'd like to change.

You can create your own StyleSheet with modified DisplayFunction:

Export[
FileNameJoin@{$UserBaseDirectory, "SystemFiles", "FrontEnd", "StyleSheets", "MyCyclotomic.nb"}, Notebook[{ Cell[StyleData[StyleDefinitions -> "Default.nb"]], Cell[StyleData["Cyclotomic"], TemplateBoxOptions -> { DisplayFunction -> (RowBox[{SubscriptBox["Φ", #], "(", #2, ")"}] &), Tooltip -> Automatic }] }, StyleDefinitions -> "PrivateStylesheetFormatting.nb"] ] (* ".../.Mathematica/SystemFiles/FrontEnd/StyleSheets/MyCyclotomic.nb" *)  and set it as StyleSheet of current notebook: SetOptions[EvaluationNotebook[], StyleDefinitions -> "MyCyclotomic.nb"]  Now any conversion to$\TeX\$ of Cyclotomic will give expected Φ:

Cyclotomic[m, 1] // TeXForm
(* \Phi _m(1) *)

• +1, Very nice! Is this something that would carry on to child notebooks? – Fred Kline Oct 8 '16 at 14:19
• @FredKline Export step creates stylesheet notebook in place that is searched by Mathematica, so it should be "avalilable" for all notebooks. SetOptions[EvaluationNotebook[], ...] sets this stylesheet permanently, but only for currently used notebook (you could also do it by using menu Format > Stylesheet > MyCyclotomic). If you want to use this stylesheet, by default, in other notebooks, you could change value of DefaultStyleDefinitions option. – jkuczm Oct 8 '16 at 15:40

Perhaps this slight workaround, or some elements of it, will be useful. First, save your expression in a variable myTradFcn

myTradFcn = TraditionalForm@Style[#, Black] &@ HoldForm[Sum[KroneckerDelta[Cyclotomic[m, 1], m], {m, 1, n}]]


Then ReplaceAll to swap the C for a φ

myNewFcn = myTradFcn /. {Cyclotomic[a_, b_] -> HoldForm[Subscript[Φ, a][b]]}


Once you've done this, you can Export it in png format (to produce the output as shown above)

tempimage = Rasterize[
myNewFcn,
RasterSize -> 1600, ImageSize -> {Automatic, 300}, ImageFormattingWidth -> 1600];
Export[
"~/Desktop/image.png",
Framed[tempimage, FrameMargins -> 8, FrameStyle -> Directive[White]]]


or Export as "TeX"

Export[
"~/Desktop/texformula",
Framed[myNewFcn, FrameMargins -> 8, FrameStyle -> Directive[White]], "TeX"]


When I open the resulting texformula file in LaTeX and typeset, this last image displays with the summation indices in a compact form, but in LaTeX you can replace \sum_{m=1}^n with \sum\limits_{m=1}^n` to force the indices to be above and below the summation sign.

• (at)zentient Good idea to work in TraditionalForm[] variables. But instead of using the bypass Export you can simply CopyAsLatex the output of MyNewFcn. – Dr. Wolfgang Hintze Oct 8 '16 at 12:20