7
$\begingroup$

Some of the built-in Stylesheets (e.g. PastelColor) connect together input and output cells by setting the CellFrame and CellMargins of the bottom of the input cell and the top of the output cell to zero. However, when there is no output the input cell looks ugly because it is missing the bottom part of its frame. There is a style defined in the Stylesheet called InputOnly which has a full frame and looks much better, but I have to apply it manually with the Format/Style menu (select Other and type "InputOnly" into the input field).

enter image description here

Is there a way to get Mathematica to automatically use the InputOnly style for input cells with no corresponding output?

$\endgroup$
2
  • $\begingroup$ Thanks for the Accept. Let me know if you have any problems with that method. $\endgroup$
    – Mr.Wizard
    Jun 9, 2012 at 14:55
  • $\begingroup$ Added notebooks tag because I had trouble finding this question and for some reason I didn't think to search for stylesheet and I couldn't remember InputOnly. $\endgroup$
    – Mr.Wizard
    Dec 19, 2013 at 12:30

2 Answers 2

5
$\begingroup$

Wouldn't it be better to simply redefine the styles so that the frame line is hidden?

If after setting the style to PastelColor I open the private style definitions (Edit Stylesheet...) and paste each of these lines separately, allowing Mathematica to "interpret the text":

Cell[StyleData["Input"], CellFrame -> 2]

Cell[StyleData["Output"], CellMargins -> {{66, 10}, {7, -2}}]

I get this behavior:

enter image description here

Is this not what you desire?

$\endgroup$
1
  • $\begingroup$ Thanks, this is perfect. I hadn't thought of using a negative margin width to conceal the bottom of the input cell's frame. Clever! $\endgroup$ Jun 9, 2012 at 14:58
5
$\begingroup$

This may not be a completely satisfying answer, but perhaps someone else can build upon it. I don't think Mathematica has the capability to put compositional conditions into the styles of cells, to check "what's around them".

What you could do instead is to set InputOnly to be your default style, and include a costum CellEvaluationFunction that switches the style to input in cases where the return value of the compution is not Null.

Here is an example outlining the idea, however it could be improved.

styleSwich[] := (SelectionMove[EvaluationNotebook[], All, 
    EvaluationCell]; 
  NotebookWrite[EvaluationNotebook[], 
   NotebookRead[EvaluationNotebook[]] /. "InputOnly" -> "Input", 
   After])

inputOnlyEvaluator[expression_, 
  StandardForm] := (Block[{res = ToExpression[expression]}, 
   If[res === Null, Null, styleSwich[]; 
    NotebookWrite[EvaluationNotebook[], res];]])

CellPrint[
 ExpressionCell[5, "InputOnly", 
   CellEvaluationFunction -> inputOnlyEvaluator]]

To implement this in the style sheet, you just need to add CellEvaluationFunction -> inputOnlyEvaluator to the stylesheet definition, and then put styleSwitch and inputOnlyEvaluator into your init file to make sure they are there.

This is a one way trip however, and after you have evaluated a cell that produces an output cell, the cell will continue to be Input style. If you wanted both to be able to switch between each other, you'd need to make a new CellEvaluationFunction in a similar style for the Input cell.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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