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.

Say I have a number $n_{max}$. I want a command to generate a table like

1 1
2 2
3 3
4 4
nmax nmax

where the second column is editable, and this table can be evaluated so that Mathematica treats the table as the set

replacement = { 1 -> 1, 2 -> 2, ...}

(The point of this is so that I can replace the original list using /.replacement).

So for example, after generating the table above, I can edit the second column as follows

1 A
2 B
3 C
4 D
nmax FF

and this output re-evaluates into set

replacement = { 1-> A, 2-> B, 3-> C, F-> D, ... , nmax-> FF}
share|improve this question
What do you mean by editable? Just use Part. tmp = Transpose[{Range[4], Range[4]}]. tmp[[All, 2]] = {A, B, C, D}. But don't use capitalised symbols. –  Mike Honeychurch Feb 6 at 21:18
@MikeHoneychurch Probably something like a = RandomInteger[10,{5,2}]; TableView[Dynamic[a]]. WARNING: TableView in undocumented, it might not work, and it has already crashed my Mathematica ... save your work before trying! –  Szabolcs Feb 6 at 21:23
@Szabolcs It is unclear (to me) whether he wants to edit like a spreadsheet or "edit" programmatically, but I thought the latter. TableView changed in V9. It is now a TableViewBox. Before that it was built from other boxes. –  Mike Honeychurch Feb 6 at 21:28
Sorry, I don't think I was unclear, but I wanted the former: just a table where the user can change any entry in the second column and have the table reevaluated as input. –  user12226 Feb 6 at 21:32
I think it would be better if you explained why you want this... This is really a cumbersome way of doing it (although it might make perfect sense to you) and you will probably end up having to write more patchwork code for when things go wrong. If you explained your intention, then perhaps we can point you to the right way of doing it. –  rm -rf Feb 6 at 22:04

2 Answers 2

How about this. Generate a table of length nmax

nmax = 10; x = Table[{i, RandomInteger[]}, {i, 1, nmax}];

and use InputField to update the second column of the table

   Table[With[{i = i}, {x[[i, 1]], 
      InputField[Dynamic[x[[i, 2]]], FieldSize -> Tiny]}], {i, 1, 
  replacement = Dynamic@x;

( The column is there just in case you want to remove the ; and view the current value of replacement.)

Mathematica graphics

The symbol replacement is updated whenever the data in the second column is changed, and you can access a set of replacement rules using:

{#[[1]] -> #[[2]]} & /@ replacement[[1]]
(* {{1 -> 2}, {2 -> 3}, {3 -> 4}, {4 -> 5}, {5 -> 6}, {6 -> 0}, {7 -> 1}, {8 -> 0}, {9 -> 0}, {10 -> 1}} *)
share|improve this answer
nMax = 10;
Block[{tab, boxes},
 tab = Table[
   {i, InputField[i]}
   {i, nMax}

 boxes = ToBoxes@TableForm[tab];

 boxes[[2]] = 
  Function[e, Print["replacement is updated"]; 
   replacement = #[[1]] -> #[[2, 1]] & /@ e];

   ] // CellPrint

Prints an input cell that looks like

enter image description here

which evaluates to

{1 -> 1, 2 -> 2, 3 -> 3, 4 -> 4, 5 -> 5}

and sets the variable replacement to be the result of evaluation above.

This is very similar to bobthechemists answer, but it relies less on Dynamic, which I think is nice. A downside of the code is that it assumes that the box generated by ToBoxes@TableForm@tab is a TagBox with a certain structure. That is why I used box expression in my previous answer, but the new code looks much cleaner.

I like the use of InputFields suggested by bobthechemist. Note that this allows one to "tab" through the table.

share|improve this answer

Your Answer


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.