1
$\begingroup$

Simple example:

k = 5;
list = {};
For[i = 1, i <= k - 2,
 AppendTo[list, 1/(1 + Cos[i Pi/k])];
 i = i + 2];
Print[list];

{1/(1+1/4 (1+Sqrt[5])),1/(1+1/4 (1-Sqrt[5]))}

I want the expression to be kept and printed as:

{1/(1 + Cos[Pi/5]), 1/(1 + Cos[3 Pi/5])}

I have tried Hold and its several versions, but nothing seems to do the trick.

$\endgroup$
1
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$
    – chris
    Apr 1, 2013 at 7:03

2 Answers 2

3
$\begingroup$
k = 5;
list = {};
For[i = 1, i <= k - 2, 
  With[{i = i, k = k}, 
   AppendTo[list, HoldForm[1/(1 + Cos[(i \[Pi])/k])]]];
  i = i + 2];
Print[list];

Mathematica graphics

$\endgroup$
3
  • 1
    $\begingroup$ Perhaps Table[With[{i = i, k = k}, HoldForm[1/(1 + Cos[i Pi /k])]], {i, 1, k - 2, 2}] is more Mathemaica-ish $\endgroup$ Mar 31, 2013 at 23:22
  • $\begingroup$ @belisarius I know, but that's not what the OP wanted. $\endgroup$ Apr 1, 2013 at 8:31
  • $\begingroup$ Sjoerd: I know you know :). I was "talking" to the OP. Sorry for the confusion $\endgroup$ Apr 1, 2013 at 8:46
3
$\begingroup$

As stated HoldForm is your friend here. Also see Defer if you intend to reuse the output.

Equivalent methods:

Table[With[{i = i, k = k}, HoldForm[1/(1 + Cos[i Pi/k])]], {i, 1, k - 2, 2}]

Table[{i, k} /. {i_, k_} :> HoldForm[1/(1 + Cos[i Pi/k])], {i, 1, k - 2, 2}]

Table[HoldForm[1/(1 + Cos[# Pi/#2])] &[i, k], {i, 1, k - 2, 2}]

Mathematica graphics

$\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.