2
$\begingroup$

To help illustrate my problem, consider the following cell:

Goal

First thing to notice is that there is syntax highlighting. Second, I can pretty print mathematics; that is, I see $x^2$ instead of x^2. Finally, the equal signs are aligned "nicely." I will expand on what I mean by "nicely" in a second.

As of now, I have no idea how to create the cell I illustrated above. Let me elucidate what I mean by this. By default the "Style of newly typed cells" is Input and the "Format type of new input cells" is StandardForm (in V9 you can see this in the Preferences -> Evaluation menu).

menu

To create a cell that is similar, but not exact, to the cell I have shown above, I need to type the following:

variable Space = Space 1 ; Enter

var Space x 13 = Space x Ctrl + 6 2 ;

This produces the following. Notice that the equal signs are not quite aligned.

standardform

Hitting Space 13 times is not what I call a "nice" way of aligning the equal signs. What I expect, is to hit Space 6 times to align the equal signs since this is a monospace font.

Question: Is there a way to modify the FrontEnd so that the sequence of keyboard commands:

variable Space = Space 1 ; Enter

var Space x 6 = Space x Ctrl + 6 2 ;

produces the first image above and I am able to evaluate it?

Let me explain what I have tried so far.

(Attempt 1) Changing "Format type of new input cells" to InputType. Using the second set of keyboard commands above, I get what is desired, but evaluating the cell gives me an error.

MakeExpression::boxfmt: InputForm in MakeExpression[FormBox[RowBox[{RowBox[{variable,=,1}],;}],InputForm],InputForm] is not a box formatting type. A box formatting type is any member of $BoxForms. >>

(Attempt 2) Changing "Format type of new input cells" to Raw InputType. I try using the second set of keyboard commands above and can align the equal signs, but I lose the ability to pretty print math and lose syntax highlighting. I am able to evaluate the cell.

rawinputform

(Attempt 3) Keeping "Format type of new input cells" as StandardForm. I use the first set of keyboard commands to produce the third image. Then I highlight the cell bracket and convert to InputForm (via Shift + Ctrl + I). This gives the following.

converted

I can then add a few spaces to align the equal signs.

aligned

Then I change the x^2 to a $x^2$. Unlike in (Attempt 1), I can evaluate this cell. Even though this gives me what I desire, it is a tedious way to go about it.

$\endgroup$
5
  • $\begingroup$ Using the ManualInput style proposed in that answer does not solve my problem. Although you do mention the behavior of the Code style. $\endgroup$ Apr 5, 2013 at 16:18
  • $\begingroup$ James, sorry, you're right, it's a related but different question. I'll remove my comment above. Also, would you like me to give an example of a custom style sheet or are you familiar with that? $\endgroup$
    – Mr.Wizard
    Apr 5, 2013 at 16:22
  • $\begingroup$ I am not familiar with that. If you could give an example that would be great. $\endgroup$ Apr 8, 2013 at 20:34
  • $\begingroup$ James, I have added instruction and an example to my answer. Please take a look and let me know if you have further questions. $\endgroup$
    – Mr.Wizard
    Apr 12, 2013 at 1:17
  • $\begingroup$ Possible duplicate: (13006) $\endgroup$
    – Mr.Wizard
    Jun 19, 2015 at 21:40

2 Answers 2

4
$\begingroup$

It sounds to me like you want a Code (style) cell without the gray background.

  • Select to Format > Style > Code or press Alt+8.

  • Select Format > Background Color > None

If this is correct you can create a new custom style in a private style sheet that has these characteristics by default.


As requested here is how to create a private style sheet:

  1. In your working Notebook go to Format > Edit Stylesheet...

  2. Paste the code below, and if asked to "Interpret the text" click YES

  3. Make additional changes by selecting the created cell and applying styles to it,
    or by using Show Expression and editing the code directly.

  4. Close the Stylesheet and access the new style from the Format > Style menu.

If you wish to use this style elsewhere you will want to install the stylesheet. You can do that by saving the private style sheet to a .nb file and then using File > Install...

Here is the code that you will paste. It is a simple example intended to be built upon:

Cell[StyleData["CustomCode", StyleDefinitions -> StyleData["Code"]],
 InitializationCell -> False,
 Background -> None,
 MenuPosition -> 1455
]

The line StyleData["CustomCode", StyleDefinitions -> StyleData["Code"]] creates a new style named CustomCode and imports the settings from the existing style named Code.

InitializationCell -> False and Background -> None override two properties of the Code style that you probably do not want: the gray background and auto-evaluation (with a prompt) when the Notebook is opened.

Finally, MenuPosition -> 1455 determines the position of the style in the menu. On my system this value places the new style right after Code.

For additional instruction in style sheets I found this resource helpful:

David Park's StyleSheet creation notes (.zip file)

$\endgroup$
0
0
$\begingroup$

For the record, the following approach doesn't really work:

screen grab

You insert text alignment markers EscamEsc before the = sign, then choose Format > Text Alignment > To Alignment Marker.

I believe that some people align their maths equations like this... However, if you want both lines to be aligned at the left margin as well, you now have the same problem; you have to insert spaces — albeit before the var, rather than after it.

$\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.