2
$\begingroup$

When editing a package (*.wl or *.m) file, the "Style" of each newly-created cell is "Code" by default. However, when writing code (and in particular, usage messages) in these cells, the text will scroll off the screen instead of being wrapped to the next line. (The "format code" button at the top is both inconvenient and insufficient for long strings, such as usage messages.) So, I find it nicer to change the Style of each cell to Input (and then make them Initialization Cells). So I'm wondering:

  1. Is it a good idea to make the style of cells in packages Input instead of Code? Are there any hidden pitfalls of doing this?

  2. If it is a good idea, is there a way to make the default cell style Input instead of Code? (If not, is there a way to make a style which appears (and wraps) like the Input style while avoiding those pitfalls?)

This might be straightforward, but I'm not sure how to do it. Thanks!

$\endgroup$
0

3 Answers 3

1
$\begingroup$
  1. I prefer "Code" style just because I can format the code manually. If you need automatic line-wrapping, you won't get terrible side effects from "Input" style InitializationCell.

    1. (If yes) You should change the style definition of "Notebook" to DefaultNewCellStyle -> "Input" in the stylesheet Package.nb. Then similarly set InitializationCell -> True for "Input" style.
    2. (If not) Set CellHorizontalScrolling -> False for "Code" style.
$\endgroup$
5
  • $\begingroup$ This seems like what I'd like to do! However, I'm having trouble editing any of the cells in Package.nb, even when making a copy and putting it in my $UserBaseDirectory instead of the application itself. Is there something I need to do to edit these cells appropriately? I'm unfortunately unfamiliar with editing stylesheets. $\endgroup$
    – thorimur
    Sep 2, 2021 at 5:32
  • $\begingroup$ Ok, I figured it out—apparently, selecting a cell in the stylesheet and then choosing Format > Option Inspector ... is meant to let me change these options, but that wasn't working for me either. So, I just opened it in a plain text editor and changed things manually—it worked! Thanks so much! :) $\endgroup$
    – thorimur
    Sep 2, 2021 at 5:57
  • 1
    $\begingroup$ Note: I found that I needed to change the style definition of "Working" instead of "Notebook", as that was the one which had DefaultNewCellStyle -> "Code". $\endgroup$
    – thorimur
    Sep 2, 2021 at 5:58
  • $\begingroup$ @thorimur 1. To edit a stylesheet, we'd better use the menu command Cell > Show Expression and File > Save As as those stylesheets are not Editable and Savable. Another text editor is also OK. $\endgroup$
    – rnotlnglgq
    Sep 3, 2021 at 9:34
  • $\begingroup$ 2. That may be my mistake. $\endgroup$
    – rnotlnglgq
    Sep 3, 2021 at 9:36
1
$\begingroup$

Don't do this

Cells formatted as Input will not be executed when using <<file` or Get["file.wl"]. See e.g. this example:

enter image description here

<< file` 
(* Code cell *)

Note how the outut of the input cell is missing. To see why this happens, we can look at the contents of the file in a text editor:

(* ::Package:: *)

Print@"Code cell"


(* ::Input:: *)
(*Print@"Input cell"*)

As you can see, any non-code cells are commented out, and will thus not be evaluated.

$\endgroup$
1
  • $\begingroup$ good tip in general! however, I am sure to convert my input-style cells to initialization cells. I think this remedies the problem. $\endgroup$
    – thorimur
    Sep 1, 2021 at 23:05
1
$\begingroup$

If the problem is with usage messages, you can use a \ at the end of a line, and then use a linebreak:

asdf = "asdf\
asdf";

asdf //InputForm

"asdfasdf"

Notice that the asdf variable has no newline.

$\endgroup$

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.