Continuing the theme of minor interface annoyances, one of the unique features of Mathematica as a text editor is the automatic adjustment of text location as you type. This could be a bit disorienting...Any way to make it stop?
3 Answers
I interpret this question differently: you want an Input cell that does not reflow text such that manual line breaks and indentation are preserved.
This is the behavior of the built-in Code cell style, but that by default comes with baggage such as making the cell an Initialization Cell and a gray background. It also turns off all auto-spacing, meaning that an expression like a = 2 + 2
is rendered as a=2+2
unless you manually space it out. An alternative is to create your own cell style or modify the Input style using custom Style Sheets.
As an example open a new Notebook, then Format > Edit Stylesheet... and paste this at the bottom of the file:
Cell[StyleData["ManualInput", StyleDefinitions -> StyleData["Input"]],
PageWidth -> Infinity,
CellHorizontalScrolling -> True,
PageBreakWithin -> False,
GroupPageBreakWithin -> False,
LineBreakWithin -> False,
AutoIndent -> False,
MenuPosition -> 1540
]
If asked to "Interpret text" click Yes.
Now close the Style Sheet and create a new Cell with Style ManualInput using the Format > Style menu. This gives you a cell you can paste code into without it reflowing, or manually indent your code. If you want to use this style in place of the default Input you could use the code above but replace StyleData["ManualInput", StyleDefinitions -> StyleData["Input"]]
with StyleData["Input"]
. If you want to always use this style you can save this Style Sheet and then point Mathematica to it as the default:
-
$\begingroup$ Sorry for being a bit vague, but you did interpret my question correctly. I find it disorienting to have to keep looking for the code I am writing while I am writing it. This solves the issue, thanks! $\endgroup$– MosheCommented Oct 14, 2012 at 19:17
If you mean InputAutoReplacements
in text cells you can switch these off in the options inspector.
But this means doing it every time you want to switch it off best to add it to your stylesheet programmatically:
SetOptions[EvaluationNotebook[],
StyleDefinitions ->
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Text"], InputAutoReplacements -> {}]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"]]
Or evaluating this:
CreateDocument[
Notebook[{Cell[StyleData[StyleDefinitions -> "Default.nb"]],
Cell[StyleData["Text"], InputAutoReplacements -> {}]},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"]]
... and installing this change as a new stylesheet. If that is not what you mean by "automatic adjustment of text location" can you explain/clarify/expand?
-
$\begingroup$ I think this refers to what I had in mind. How do I switch it off in input cells? $\endgroup$– MosheCommented Oct 13, 2012 at 22:17
-
$\begingroup$ replace
StyleData["Text"]
withStyleData["Input"]
$\endgroup$ Commented Oct 13, 2012 at 22:22 -
$\begingroup$ Excellent, I will try that, thanks! $\endgroup$– MosheCommented Oct 13, 2012 at 22:23
-
$\begingroup$ When I do this for documentation pages I can't use Reference.nb but full
Cell[StyleData[ StyleDefinitions -> FrontEnd`FileName[{"Wolfram"}, "Reference.nb", CharacterEncoding -> "WindowsANSI"]]]
. $\endgroup$– KubaCommented Apr 23, 2015 at 6:55
I know, this is not the recommended way, but it is a quick one:
I made a copy of Default.nb in a place, where I can edit it with Mathematica.
Then I opened my copy of Default.nb. I located the prototype for Input under Styles for Input and Output Cells.
Then I used Shift+Ctl+E and got the cell expression for it. I edited it such that it looks like this:
Cell[StyleData["Input"],
StyleKeyMapping->{"=" -> "WolframAlphaShort", "*" -> "Item"},
AutoSpacing->False,
FontSize->13]
and then I applied Shift+Ctl+E again.
Then I stored the notebook under the name Default1.nb in my private folder.
Then I renamed the original one in its old place to maintain a backup of it (logged in as an administrator and doing it as sudo) and finally renamed Default1.nb to Default.nb in my private folder and finally copied my modified Default.nb (under sudo) to the folder, where it originally was (/usr/local/Wolfram/Mathematica/10.1/SystemFiles/FrontEnd/StyleSheets/Default.nb).
After all this I started a new notebook to test the feature:
Between the left and the right picture there is exactly one space added before the lower comment. Before AutoSpacing->False as Property of the InputCell in Default.nb, the comments would advance half a space for each space added before them.
This is exactly what I was looking for for long time: being able to place comments where I like to do so and to indent code as I like (rather than as Mma likes to do).
However, it has one flaw (and hopefully, from WRI someone might shine some light onto this or silently correct an error, if it is one): It only works for notebooks created after changing Default.nb. Even in a private stylesheet's InputCell, the option is not inherited from Default.nb. It is like Hypehantion->False, which also does not find its way to its descendants.
In order to make AutoSpacing->False work for existing notebooks, one has to re-apply Default.nb to them and possibly one looses all modifications made in their private StyleSheets (I have not tried yet).
The other way I found to effectively apply AutoSpacing->False is by adding this option to the Cell expression of each Input cell.
-
$\begingroup$ My answer was a bit short: Even in a monospaced font like Courier, the operators +-*/!<>@% and all parentheses ()[]{} seem to be 10% wider than normal characters. There seem to be some additional thin spaces around them. The backslash is even stranger: Every second has some extra space, they come in pairs. The & sign has yet another thin space with it, less than the other operators. That's all really strange! $\endgroup$ Commented Nov 21, 2016 at 19:53