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.

I am trying to create a string of the form

$0 < d_1 < d_2 < \ldots < d_n$

For a given n.

My closest attempt so far is

eqn[n_] :=
  For[i = 1; str = "0", i <= n, i++, 
   str = str <> "<" <> ToString[Subscript[d, i]]];

But for, say n=6, this gives me

"0<d
 1<d
 2<d
 3<d
 4<d
 5<d
 6"

Where it seems to have interpreted the string as "0<d\n 1<d\n 2<d\n 3<d\n 4<d\n 5<d\n 6".

Why is it doing this? How can I achieve the result I am looking for?

share|improve this question
3  
Use ToString[..., StandardForm]. –  b.gatessucks Jul 23 '13 at 10:31
    
Thanks, that fixed it! –  Madeleine W Jul 23 '13 at 13:31

3 Answers 3

Probably best to use something like Fold and get used to functional style

eqn[n_] := TraditionalForm[Fold[#1 <> " < " <> 
     ToString[Subscript[d, #2], StandardForm] &, "0", Range[n]]]

enter image description here

share|improve this answer
    
@JacobAkkerboom The first use of ToString is unnecessary actually. I'll make an edit –  Mike Honeychurch Jul 23 '13 at 11:40
    
Okido, +1 and deleting my comment :). –  Jacob Akkerboom Jul 23 '13 at 11:55

Here's my entry:

string[n_] := ToString[0 < ## & @@ Array[Subscript[d, #] &, n], StandardForm]

string[7]

enter image description here

This assumes that none of the Subscript[d, _] expressions are assigned.

Here is a different method converting to strings as early as possible, using the single-argument form of Subscript:

string2[n_] := "0" <> Array[" < d" <> ToString[Subscript@#, StandardForm] &, n]

The spacing is a bit wider with this one. You could use narrower spaces, e.g.:

string2[n_] := 
  "0"<>Array["\[MediumSpace]<\[MediumSpace]d"<>ToString[Subscript@#,StandardForm]&, n]

One more, quite concise:

string3[n_] := "0" <> Array[" < \!\(d\_" <> ToString@# <> "\)" &, n]
share|improve this answer

Most important thing is what b.gatessucks has said. This answer is just a variation of the method:

eq[0] = "0";
eq[n_] :="0 < " <> StringJoin @@ 
               Riffle[ToString[Subscript[d, #], StandardForm] & /@ Range[n], " < "]

If you do not need string but an expression which looks like this, then it is good to remember Row's 2nd argument:

Row[{"0"}~Join~(ToString[Subscript[d, #], StandardForm] & /@ Range[4]), " < "]
share|improve this answer

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.