15
$\begingroup$

I'm putting together a Demonstration which utilizes a function which operates on a (manipulated) String, and I'd like the InputField to be of the form InputField[col,String,FieldSize->3]. Using the default format gives something like this:

Manipulate[col, {{col, "AB"}}]

Mathematica graphics

which requires use of quotation marks.

I'm able to hack the function to take expressions and convert them to strings

f[column_String] := column
f[column_] := ToString@HoldForm@column

but then the InputField looks strange for I or E:

Mathematica graphics

I found another possible workaround on MathGroup:

col="AB"
Manipulate[Grid[{{InputField[Dynamic[col], String, FieldSize -> 3], f@col}}]]

Mathematica graphics

However, this requires pre-initializing col and messes up multiple Manipulate instances.

Is there a way to have Manipulate assume that what's entered in an InputField is a String?

$\endgroup$

1 Answer 1

19
$\begingroup$

I thought this was undocumented, but actually it just seems to be obscure. The documentation for ControlType mentions, under the "More Information" section,

Arbitrary controls can be set up in Manipulate by giving control specifications of the form {u, func}.

So,

Manipulate[InputForm[col], {col, "AB", InputField[#, String] &}]

gives (with a different string entered for demonstrative purposes):

picture of a Manipulate with a string-type InputField

$\endgroup$
3
  • $\begingroup$ I completely missed that in the "More Information" section. Sorry for the RTFM question. $\endgroup$
    – Eli Lansey
    Commented Apr 9, 2012 at 21:07
  • 2
    $\begingroup$ @EliLansey: I think nearly everyone misses that information. $\endgroup$
    – orome
    Commented Mar 15, 2015 at 15:21
  • $\begingroup$ Note that if you need to additionally specify the label, then the syntax will be {{col, "AB", "My control"}, InputField[#, String] &} — note the placement of the extra braces inside. $\endgroup$
    – Ruslan
    Commented Apr 14, 2020 at 10:09

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.