Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Again, I am writing homework sheets. I have tried

 a = "(portionOfCircle)";
 SectorArea =  a * Pi * r^2 
 SectorArea =  a * Pi * r^2 // TeXForm
 With[{a = "portionOfCircle"}, Defer[a * Pi * r^2 // TeXForm]]

I would like the output to be in LaTeX form such as $$\text{(portion of circle)}\cdot \pi r^2$$ instead my code will output $$\pi \text{(portion of circle)}\cdot r^2$$

Also, I would like

 a = RandomChoice[{Pi/6, Pi/4, 5 Pi/4}]/(2 Pi)
 SectorArea = a*Pi*r^2
 SectorArea = a*Pi*r^2 // TeXForm
 With[{a = a}, Defer[a*Pi*r^2]]

to output $$\frac{\pi/4}{2\pi}\cdot \pi r^2$$ instead, it simplifies the fraction hiding the concept i'm trying to emphasize.

Any help would be much appreciated.

share|improve this question
add comment

2 Answers

up vote 3 down vote accepted

Just to clarify my comment earlier, here is a solution for the second part of the question (no need to duplicate an answer to the first part) that I think serves the intended purpose:

Row[{Row[{RandomChoice[{Pi/6, Pi/4, 5 Pi/4}]}]/HoldForm[2 Pi], 
   " \[Times] ", HoldForm[Pi*r^2]}] // TraditionalForm

output

Here I added the \[Times] symbol just to show that you can add arbitrary decorations by means of Row, to make the output look even clearer.

share|improve this answer
    
very nice! this is exactly what I was looking for ... thank all of u for your help much appreciated! –  userX Mar 18 '13 at 2:13
    
Yes, this is nice. +1. –  Leonid Shifrin Mar 18 '13 at 8:29
add comment

What you want to use (for the first part) is HoldForm.

i.e.

TeXForm@With[{a = "portionOfCircle"}, HoldForm[a*Pi*r^2]]

which returns

\text{portionOfCircle} \pi  r^2

as desired.

The problem with using Defer is that it simply holds until the expression evaluated. In your example, when the output of Defer is fed to TeXForm it gets evaluated prior to formatting it for TeX output. HoldForm will preserve the form under all future manipulations until you explicitly call ReleaseForm.

share|improve this answer
    
thank you @covopsdaf2, i think I need to add HoldForm to my bag of tools.. I understand it thank you, does exactly what I needed. –  userX Mar 16 '13 at 23:19
    
HoldForm is the way I would go, too (+1). Maybe also combined with Row when you mix strings and numbers. –  Jens Mar 17 '13 at 3:22
    
@Jens HoldForm does not address the second part of the question, which (to me at least) was the main one. –  Leonid Shifrin Mar 17 '13 at 21:25
    
@LeonidShifrin It's so easy to do this with Row and HoldForm, I just don't think one should even start the way the OP did... –  Jens Mar 17 '13 at 22:06
    
@Jens The second part of the question was not about positioning, and I don't see how you solve that problem with Row and HoldForm. But, whatever, no big deal anyway. –  Leonid Shifrin Mar 17 '13 at 22:11
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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