6
$\begingroup$

How to convert the output of the following code

In[43]:= Func[x_]:=Select[Range[x],Total[IntegerDigits[#]^3]==#&];
Func[1000]

which is

Out[44]= {1,153,370,371,407}

to the following form

enter image description here

such that I can copy it and paste to my LaTeX document without significant hassle.

Edit

I want the output of Mathematica to be consumable for LaTeX engine.

1^3 & 1\\
1^3+5^3+3^3 & 153\\
3^3+7^3+0^3 & 370\\
3^3+7^3+1^3 & 371\\
4^3+0^3+7^3 & 407\\
$\endgroup$
6
  • $\begingroup$ Do you want to generate the first column or are you after the $LaTeX$ export part? $\endgroup$
    – Yves Klett
    Dec 30, 2013 at 17:04
  • $\begingroup$ @YvesKlett: The table contents must be generated by Mathematica so I can just copy and paste. $\endgroup$ Dec 30, 2013 at 17:06
  • $\begingroup$ You are aware of TeXForm? E.g. Grid[{{a, b, c}, {x, y^2, z^3}}, Frame -> All] // TeXForm ... $\endgroup$
    – Yves Klett
    Dec 30, 2013 at 17:08
  • $\begingroup$ @YvesKlett: Please show me how can you decompose each element of the list into the sum of cubes and make sure it is not evaluated. $\endgroup$ Dec 30, 2013 at 17:11
  • $\begingroup$ Please add that info to your question to help understand it better. $\endgroup$
    – Yves Klett
    Dec 30, 2013 at 17:14

1 Answer 1

10
$\begingroup$

Modding the plus part from here, this could be a (somewhat awkward because you get more array than you asked for) start:

Func[x_] := Select[Range[x], Total[IntegerDigits[#]^3] == # &];
nums = Func[1000];

plus[args__] := Grid[{Riffle[{args}, "+"]}]

g = Grid[{plus @@ (IntegerDigits[#] /. x_Integer :> HoldForm[x^3]) & /@
      nums, nums} // Transpose]

Mathematica graphics

TeXForm[g] 

returns unnecessarily complicated output, which compiles just fine (and looks indeed much better than standard frontend output).

enter image description here

As for further styling, this is perhaps better done in your document - unless you have to do that many times over, in which case you could roll your own MyOwnTeXForm.

Side note: If everything else fails, you can install the Computer Modern fonts and use them with Style to do all the formatting in the frontend - and then export an image (not pretty, but it works).

$\endgroup$
2
  • $\begingroup$ Strange thing that Row[{args}, "+"] produces errors during converting to TeX. Moreover, Dividers result in not good TeX output. But your code is ok and adding dividers in TeX is not a big deal I suppose. $\endgroup$
    – Kuba
    Dec 30, 2013 at 17:58
  • $\begingroup$ @Kuba yes, that was the reason to switch over to Grid. $\endgroup$
    – Yves Klett
    Dec 30, 2013 at 18:07

Your Answer

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

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