1
$\begingroup$

I am summing some terms by first transforming it to TeXForm.

Example:

Sum[TeXForm[x^i],{i,1,50}]

enter image description here I want to break the line in the notebook output (or in OutputForm) starting with + instead of a trailing + as in the previous output. I can set PageWidth with TeXForm but this does not help. Mathematica does it to further process this output (if any). Otherwise a leading + would mean each of these lines are separate expression (which is not true and unwanted)!

My question is, just for displaying purposes can I show the line break with a leading + instead of a trailing "+"?

The expected output will look something like the below:

  x + x^2 + x^3 + x^4 + x^5 + x^6 + x^7 + x^8 + x^9 + x^10 + x^11 + x^12 + x^13 + x^14 + x^15 
+ x^16 + x^17 + x^18 + x^19 + x^20 + x^21 + x^22 + x^23 + x^24 + x^25 + x^26 
+ x^27 + x^28 + x^29 + x^30 + x^31 + x^32 + x^33 + x^34 + x^35 + x^36 + x^37 + x^38 
+ x^39 + x^40 + x^41 + x^42 + x^43 + x^44 + x^45 + x^46 + x^47 + x^48 + x^49 + x^50

Note: The expected output is not meant to be used for further manipulation in the Notebook. It is for convenience for display and also if I want to take (copy) the output into LaTeX where a leading + is preferred instead of a trailing +. Also, I am not interested in using TeXForm[Sum[x^i, {i, 1, 50}]].

$\endgroup$
2
  • $\begingroup$ TeXForm is meant for generating Latex to be typeset by your Latex compiler. Why are you using it to display things in the notebook? Why not use TraditionalForm instead? $\endgroup$
    – Nasser
    Commented Dec 15, 2023 at 21:56
  • $\begingroup$ I understand by using Sum I am doing a Mathematica manipulation for which a trailing + is needed. My goal is to prepare a custom TeXForm where complicated and long expressions can be nicely written directly from Mathematica. Hence the Sum comes since I am splitting the expression into pieces, beautifying each piece and finally summing it together. As a result TraditionalForm won't work either. $\endgroup$
    – BabaYaga
    Commented Dec 15, 2023 at 22:12

1 Answer 1

1
$\begingroup$
ToBoxes[Sum[TeXForm[x^i], {i, 1, 50}]];
ReplacePart[%, 
  Position[%, "+"][[10 ;; -1 ;; 10]] -> "\n+"] // RawBoxes

x+x^2+x^3+x^4+x^5+x^6+x^7+x^8+x^9+x^{10}
+x^{11}+x^{12}+x^{13}+x^{14}+x^{15}+x^{16}+x^{17}+x^{18}+x^{19}+x^{20}
+x^{21}+x^{22}+x^{23}+x^{24}+x^{25}+x^{26}+x^{27}+x^{28}+x^{29}+x^{30}
+x^{31}+x^{32}+x^{33}+x^{34}+x^{35}+x^{36}+x^{37}+x^{38}+x^{39}+x^{40}
+x^{41}+x^{42}+x^{43}+x^{44}+x^{45}+x^{46}+x^{47}+x^{48}+x^{49}+x^{50}

ToBoxes[Sum[TeXForm[x^i], {i, 1, 50}]];
ReplacePart[%, Position[%, "+"][[5 ;; -1 ;; 5]] -> "\n+"] // RawBoxes

x+x^2+x^3+x^4+x^5
+x^6+x^7+x^8+x^9+x^{10}
+x^{11}+x^{12}+x^{13}+x^{14}+x^{15}
+x^{16}+x^{17}+x^{18}+x^{19}+x^{20}
+x^{21}+x^{22}+x^{23}+x^{24}+x^{25}
+x^{26}+x^{27}+x^{28}+x^{29}+x^{30}
+x^{31}+x^{32}+x^{33}+x^{34}+x^{35}
+x^{36}+x^{37}+x^{38}+x^{39}+x^{40}
+x^{41}+x^{42}+x^{43}+x^{44}+x^{45}
+x^{46}+x^{47}+x^{48}+x^{49}+x^{50}
$\endgroup$

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.