3
$\begingroup$

Motivation / examples:

  • on a spreadsheet environment, each cell doubles as input and output. If we click on it, it shows the input; if we hit Enter, it shows the output;
  • on this answer, I tried to mimic this kind of behaviour, but in the middle of a text cell (which is kind of an extension of the behaviour I'm asking in this post)

enter image description here

  • the template WRI uses for many of their presentations, typically makes use of a function named LargeOverviewMouseover, where the content of the cell changes on a mouseover event, but it forces the editor to delete the input so to have a nice formatted presentation (when I use it, sometimes I end up directly editing the cell expression... ctr+shift+E, so that I don't need to retrieve the original input..., since just hiding the input generally leaves too much dead space, and is highly depending on the stylesheet...):

enter image description here

Question: is there a simple way of creating a Cell that doubles as Input and Output? Converting from one to the other, either on mouseover, or on clicking, or with a smarter way (so to still allow for manipulation of the output content, like dynamics, etc., without passing directly to the Input form...)?

EDIT

Ideally, a solution where the Input aspect of the cell is really an Input cell, would be best (I mean, that has all the code formatting, etc...).

$\endgroup$
  • 1
    $\begingroup$ I would have thought that the ability to close a cell group, alternatively on the Input cell or reverse-closing on an Output cell, would have been pretty close to what you're looking for without trying to invent a new interface. Certainly I see reverse-closing as a very common way of hiding implementation in notebooks. $\endgroup$ – John Fultz Dec 31 '15 at 0:51
  • $\begingroup$ @JohnFultz Imagine I have a 700 pages notebook (I have several of them). Many of the input should be printed as a report, but most of them do not need to show. I've been finding it difficult to keep track of this, either with the reverse-closing, or with hiding. A tagging system with some automatic parsing should be possible (for hiding, it's easy, but formatting is not great, deleting on duplicated nb is slow and odd). But I was wondering it there was some sort of hybrid cell type possible. Yes, from the answers, it does fill like reinventing the interface. But asking doesn't hurt... Right? $\endgroup$ – P. Fonseca Dec 31 '15 at 9:48
  • $\begingroup$ @JohnFultz That's why I insisted with the stylesheet solution, because if there was a way to create a style that acted like that, it wouldn't feel odd, and the nb writer would just need to choose the input cell formatting as (standard) input or hybrid. From then on, no need to keep track of what would need to be closed or opened, etc. and WYSIWYG. But thank you for your comment, since, if you say it would be like reinventing the interface, then, I guess that is answered... and I thank all the answers for their imaginative tries, that for sure revealed interesting ways of text cells inline code $\endgroup$ – P. Fonseca Dec 31 '15 at 9:55
  • $\begingroup$ Sometimes people ask questions which, even if properly and successfully answered, wouldn't represent the best solution to the actual problem they're experiencing. That's why I want to understand your problem, and why one existing solution to that problem was not sufficient for you. Given your problem, I would have gravitated toward a tagging-based solution...perhaps to the end of automatically reverse-closing the appropriate cell groups. It's unclear from your description whether you would find such a solution adequate and, if not, why not. $\endgroup$ – John Fultz Dec 31 '15 at 19:42
  • 1
    $\begingroup$ What is LargeOverviewMouseover? p.s. what about a new styles, where input-like has Input options in Working environment + is invisible in e.g Slideshow. And which produces Output-like cell which is visible in Slideshow (may or may not be seen in Working, depends what you like). $\endgroup$ – Kuba Jan 3 '16 at 8:59
3
$\begingroup$

Update

Adjusted solution uses "DownArrowKeyDown"+"ShiftKey" EventHandler instead of Mouseover so you can interact with the output. Some of the code or output has to have focus for the switch to trigger. You still have to hit Enter in the InputField for the new code to register before you hit the down arrow.

DynamicModule[{code = Null, codeInput, visible, modeSwitch = 1},
 codeInput = InputField[Dynamic[code],
   Hold[Expression], FieldHint -> "Type code",
   FieldSize -> {50, 25}];
 visible = codeInput;
 EventHandler[Dynamic@visible,
  {"DownArrowKeyDown" :> If[CurrentValue["ShiftKey"],
    (modeSwitch++; 
     visible = If[EvenQ[modeSwitch], ReleaseHold@code, codeInput])]}
  ]]

This makes it a lot less jittery as well. I also made the InputField bigger.

Try it with Plot3D[x^2 - y^2, {x, 0, 10}, {y, 0, 10}]


Original post

With this solution the cell you are inputting into is an output cell but you can enter code into it and it does not create a new cell when it is executed.

One caveat is that you should hit Enter when finished entering your code instead of Shift+Enter.

DynamicModule[{code = Null},
 Mouseover[Dynamic@ReleaseHold@code,
  InputField[Dynamic[code],
   Hold[Expression], FieldHint -> "Type code"]]
 ]

Try it with Table[i, {i, 10}], or Plot[x, {x, 0, 10}], or anything else you can think of.

Hope this helps.

$\endgroup$
  • $\begingroup$ This is kind of closer to what I want. What is missing is the natural feeling... I mean: no code formatting, no multi-line, no auto-complete, no possibility of having dynamic contents based on mouseovers (which puts aside the example I had on the WRI presentation format), etc.. Without all this, can we still call it an Input Cell? I really think that, if what I'm asking is possible, it will be based on a completely different direction... Stylesheet based? Code transferred to cell tag (or other hidden form) and retrieved back?, etc. $\endgroup$ – P. Fonseca Dec 30 '15 at 15:14
  • $\begingroup$ @P.Fonseca See the update. I think I'm going to delete the other answer now. $\endgroup$ – Edmund Dec 30 '15 at 15:25
  • $\begingroup$ Very interesting technique. I still think that there's possibly a more natural real Input / real Output way of doing it, but, the way you have done it, already allows for some very interesting things. Thank you. $\endgroup$ – P. Fonseca Dec 30 '15 at 15:44
  • $\begingroup$ @P.Fonseca I don't think the code highlighting and auto-complete in an InputField are never going to happen. That is what you have Mathematica for. I don't think full code in an InputField will work in CDFs either as it would kill WRI's business if anyone could essentially deploy a full version of Mma through a CDF. $\endgroup$ – Edmund Dec 30 '15 at 15:44
  • $\begingroup$ That's why I think that a solution passing through some StyleSheet side effect might be closer to a real Input/Output cell. (Just to clarify, the purpose of this is to facilitate some document formatting, by the addition of a new kind of style..., and not really the CDF aspect that might come with it) $\endgroup$ – P. Fonseca Dec 30 '15 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.