1
$\begingroup$

Say I indent my code like this:

Which[
 {x1, y1 + 1} == {x2, y2 - 1},
 {
  pos1 = pos1 /. {{x1, y1} -> {x1 = 2, y1 = 2}},
  pos2 = pos2 /. {{x2, y2} -> {x2 = 2, y2 = 4}},
  pos3 = pos3 /. {{x3, y3} -> {x3 = 2, y3 = 0}},
  message = "Gameover", DotT = {2, (4 - .3)},
  DotL = {2, (2 + .3)}, DotB = {2, (0 + .3)}, Switch = False
  },
 {x1, y1 + 1} == {x2, y2},
 {
  pos1 = pos1 /. {{x1, y1} -> {x1 = x1, y1 = y1}},
  pos2 = pos2 /. {{x2, y2} -> {x2 = x2, y2 = y2}},
  pos3 = pos3 /. {{x3, y3} -> {x3 = x3, y3 = y3}},
  message = "Stay in place"
  },
 {x1, y1 + 1} == {x3, y3},
 {
  Which[
   Intersection[bound, {{x3, y3 + 1}}] == {{x3, y3 + 1}},
   {
    pos1 = pos1 /. {{x1, y1} -> {x1 = x1, y1 = y1}},
    pos2 = pos2 /. {{x2, y2} -> {x2 = x2, y2 = y2}},
    pos3 = pos3 /. {{x3, y3} -> {x3 = x3, y3 = y3}},
    message = "Stay in place"}, {x3, y3 + 1} == {x2, y2 - 1},
   {
    pos1 = pos1 /. {{x1, y1} -> {x1 = 2, y1 = 2}},
    pos2 = pos2 /. {{x2, y2} -> {x2 = 2, y2 = 4}},
    pos3 = pos3 /. {{x3, y3} -> {x3 = 2, y3 = 0}},
    message = "Gameover",
    DotT = {2, (4 - .3)}, DotL = {2, (2 + .3)}, DotB = {2, (0 + .3)},
    Switch = False
    },
   Intersection[bound, {{x3, y3 + 1}}] != {{x3, y3 + 1}},
   {
    pos1 = pos1 /. {{x1, y1} -> {x1 = x1, y1 = y1 + 1}},
    pos2 = pos2 /. {{x2, y2} -> {x2 = x2, y2 = y2 - 1}},
    pos3 = pos3 /. {{x3, y3} -> {x3 = x3, y3 = y3 + 1}},
    message = "Conga"
    }
   ]
  }
 ]

It's very clear and human-readable for me this way, however, when I copy and paste this into my notebook I get this:

Which[{x1, y1 + 1} == {x2, y2 - 1}, {pos1 = 
   pos1 /. {{x1, y1} -> {x1 = 2, y1 = 2}}, 
  pos2 = pos2 /. {{x2, y2} -> {x2 = 2, y2 = 4}}, 
  pos3 = pos3 /. {{x3, y3} -> {x3 = 2, y3 = 0}}, message = "Gameover",
   DotT = {2, (4 - .3)}, DotL = {2, (2 + .3)}, DotB = {2, (0 + .3)}, 
  Switch = False}, {x1, y1 + 1} == {x2, y2}, {pos1 = 
   pos1 /. {{x1, y1} -> {x1 = x1, y1 = y1}}, 
  pos2 = pos2 /. {{x2, y2} -> {x2 = x2, y2 = y2}}, 
  pos3 = pos3 /. {{x3, y3} -> {x3 = x3, y3 = y3}}, 
  message = "Stay in place"}, {x1, y1 + 1} == {x3, y3}, {Which[
   Intersection[bound, {{x3, y3 + 1}}] == {{x3, y3 + 1}}, {pos1 = 
     pos1 /. {{x1, y1} -> {x1 = x1, y1 = y1}}, 
    pos2 = pos2 /. {{x2, y2} -> {x2 = x2, y2 = y2}}, 
    pos3 = pos3 /. {{x3, y3} -> {x3 = x3, y3 = y3}}, 
    message = "Stay in place"}, {x3, y3 + 1} == {x2, y2 - 1}, {pos1 = 
     pos1 /. {{x1, y1} -> {x1 = 2, y1 = 2}}, 
    pos2 = pos2 /. {{x2, y2} -> {x2 = 2, y2 = 4}}, 
    pos3 = pos3 /. {{x3, y3} -> {x3 = 2, y3 = 0}}, 
    message = "Gameover", DotT = {2, (4 - .3)}, DotL = {2, (2 + .3)}, 
    DotB = {2, (0 + .3)}, Switch = False}, 
   Intersection[bound, {{x3, y3 + 1}}] != {{x3, y3 + 1}}, {pos1 = 
     pos1 /. {{x1, y1} -> {x1 = x1, y1 = y1 + 1}}, 
    pos2 = pos2 /. {{x2, y2} -> {x2 = x2, y2 = y2 - 1}}, 
    pos3 = pos3 /. {{x3, y3} -> {x3 = x3, y3 = y3 + 1}}, 
    message = "Conga"}]}]

Which is complete garbage for my eyes. How can I retain the beauty?

$\endgroup$
9
  • $\begingroup$ Reindenting slows my progress... $\endgroup$
    – Sam
    May 15, 2015 at 21:25
  • $\begingroup$ The thing is, I indented my code originally in the .nb file, but I didn't save it as a .nb file. I just copied this indented code and pasted it into notepad and saved it as a .txt file, closed the .nb file without saving, and then later copied and pasted my indented .txt code into a new .nb file and then reindented, but this process is becoming monstrous. If I save as a .nb file. then will it retain the indentation when I reopen the file? The reason I did this .txt business was because I was accessing Mathematica on a remote machine and had no way of saving files to my local machine. $\endgroup$
    – Sam
    May 15, 2015 at 21:29
  • $\begingroup$ I doub't MMA's formatting can be completely overridden, though if it can someone is sure to respond. Nonetheless, requiring indentation for readability tends to be a symptom of using non-functional idioms... Also, what's with Switch=False? You do realize Switch is an MMA reserved symbol, yes? $\endgroup$
    – ciao
    May 15, 2015 at 21:30
  • $\begingroup$ I think pasting into a notebook filters the incoming data and reformats it. Like newlines... $\endgroup$
    – Sam
    May 15, 2015 at 21:34
  • 1
    $\begingroup$ @Sam why did you switch to Input ? Code style exists specifically to have your own indentation and allows to run code is Input cell does. $\endgroup$ May 17, 2015 at 9:07

1 Answer 1

4
$\begingroup$

I know a very awkward way to make an Input cell which behaves the way you are looking for. It has different editing properties.

I came across such cells when importing notebooks from very long ago. They were from the mid 1990's either Mathematica 3 or Mathematica 4 and they had editing properties, which I still miss very much today.

Here I show you a normal InputCell and a modified one:

normal InputCell and a modified one without BoxData

I really do not know what makes the difference. The modified cell uses InputForm

Cell>ConvertTo-Properties

and it does not show syntax highlighting, it also avoids half spaces (probably because I changed everything to monospaced Courier font and also suppressed less than one space appearing in things written in a monospaced font. Unfortunately I do not remember which of the zillions of OptionInspector Settings it was).

How to make a cell with these fancy properties (unfortunately awkward to do)

  1. Make a normal input cell as you always do.
  2. press Shift Ctl E. You get something like

    Cell[BoxData["[IndentingNewLine]"], "Input", CellChangeTimes->{3.6631740883686132`*^9}]

    (the strange number is the timestamp of when you made the cell. If you edit it later, the time stamps of all these changes yield a list of edit timestamps.)

  3. Now modify this to (i.e. get rid of BoxData[], but leave the quotation marks)

    Cell["", "Input", CellChangeTimes->{3.6631740883686132`*^9}]

  4. Now press Shift Ctl E again.

  5. Now you may enter what you like into this cell. I show you an example in my favourite intentation style: all matching braces, brackets, curly braces and the colons from the same level go into the same column, if they all do not fit onto a single line. This type of indentation makes it easy to check balance, however we get such strange things as lines starting with a colon, since I regard the colonsin some construct [ ..., ..., ...] to be on the same level as the brackets (same holds for braces of any type). Here an example:

InputCell with my favourite indentation

Advantages of these modified InputCells:

  1. You may indent as you like it best.
  2. You may use tab to offset a comment from your code.
  3. you may use tab even in the code itself.

Disadvantage

  1. Awkward to produce (unless you make one cell of this type and split it using Schift Ctl D).
  2. No Syntax Highlighting
  3. Bound to InputForm. (I got used to that.)

They behave identical to normal InputCells, as far as I have observed. One can make them e.g. InitializationCells and so forth.

It would be nice, if someone out in this forum could shine some light in what really happens under the hood and possibly show us a way to create such cells with a single keystroke. I got the impression that using Cell>ConvertTo only exposes you to the hazard of loosing all your thoroughly written comments but does not give you cells where space and tab behave like in Mousepad, Notepad or any other text editor for monospaced text.

$\endgroup$

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.