2
$\begingroup$

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.

$\endgroup$

2 Answers 2

6
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ very nice! this is exactly what I was looking for ... thank all of u for your help much appreciated! $\endgroup$
    – userX
    Commented Mar 18, 2013 at 2:13
  • $\begingroup$ Yes, this is nice. +1. $\endgroup$ Commented Mar 18, 2013 at 8:29
3
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ 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. $\endgroup$
    – userX
    Commented Mar 16, 2013 at 23:19
  • $\begingroup$ HoldForm is the way I would go, too (+1). Maybe also combined with Row when you mix strings and numbers. $\endgroup$
    – Jens
    Commented Mar 17, 2013 at 3:22
  • $\begingroup$ @Jens HoldForm does not address the second part of the question, which (to me at least) was the main one. $\endgroup$ Commented Mar 17, 2013 at 21:25
  • $\begingroup$ @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... $\endgroup$
    – Jens
    Commented Mar 17, 2013 at 22:06
  • $\begingroup$ @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. $\endgroup$ Commented Mar 17, 2013 at 22:11

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.