1

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.

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
Phillip Dukes
  • 938
  • 5
  • 18
  • 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!

    – chris Apr 01 '13 at 07:03

2 Answers2

3
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

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
3

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

Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371