# Best Practice to write ::usage for own package functions

I'm writing an own package with Mathematica, that I would like to make available for others. In order to do that (and for my own usage of the package), I would like to write ::usage-strings for all my public functions.

I would like to use a formated string for ::usage in order to refer to some mathematical background in the explanations, e.g. if one of the inputs is a matrix, the ::usage may contain $\mathbf{M}\in\mathbb Z^{d\times d}$ or something like that.

The way I'm generating these up to now is, to take a cell and change it's style to text, then write the text and copy it into the ::usage="..." cell of the package. On saving the package, that gets transformed into the classic commands, like in the following example:

DirichletKernel::usage = "DirichletKernel[\!$$\*StyleBox[\"mM\",\nFontWeight->\"Bold\"]$$]
provides a dirichlet kernel with respect to the regular integral matrix mM
in Fourier coefficients. The same options as for the function \!$$\* StyleBox[\"deLaValleePoussinKernel\", \"Code\"]$$\!$$\* StyleBox[\"[\", \"Code\"]$$\!$$\*StyleBox[\"]\", \"Code\"]$$ apply.";


Where the last part at least seems a little messed up.

There are two questions arising:

1) Is there an easier way to write such a usage message? because if I want to change something, I usually have to write the whole text again, because the format is quite verbose. What workflow do you use to write ::usage messages?

2) Sometimes the copy&paste stuff generates errors (hmpf, if you want to demonstrate something, of course, it works, I'll edit this, if I can get the actual message), that occur in the message window. Since Mathematica 9 that already happens if the autocomplete-box occurs. So what has to be taken into account copying formated cells (text with math formula and e.g. bold face and code parts) into strings - in order to not get messed up (erroneous) strings?

-
not sure, but I think this is at least related: mathematica.stackexchange.com/q/3941/169 –  Albert Retey Dec 17 '12 at 11:23
Though I'm not using Workbench, the hint, that nonescaped quotes mess things up, is quite helpful :) –  Ronny Dec 17 '12 at 11:53
Oh, thanks for the hint. I think I'll rework some of the naming, because i have several functions starting with Upper case letters. For DirichletKernel[] at least, the reason is, that Dirichlet is a personal name, so i would always tend to write that upper case; where for example for de La Vallée Poussin it's mere chance, that that's not starting upper case...; for my other functions you're right, i should rework some. –  Ronny Dec 17 '12 at 15:48
@Nasser i just checked some other packages (non standard Mathematica but from the Library online) and many delevopers use functions starting with capital letters, e.g. IntegerSmithForm. I don't know, which would be better Mathematica Programming Practice... –  Ronny Jan 4 '13 at 9:46
I still consider the question little unanswered. Would like to get see comments on how to step and format short comments on the available options such that they would look nice when using ? etc. –  Johu Jun 18 at 13:47

## 3 Answers

In Mathematica, usage messages are typically for conveying a short description of how to call the function. Your text that talks about the options would be better placed in package documentation in the Details section. You can use Wolfram Workbench to generate package documentation that shows up in the Documentation Center.

-
Workbench includes a set of documentation build tools that compile the source documentation notebooks into something that has the right look and feel for the documentation center, and adds to the help index as well. Not sure how hard it would be to duplicate this functionality a different way. –  Joel Klein Dec 18 '12 at 0:46
Of course, writing a complete Documentation would be realy nice. I red some questions here about writing an own Documentation page, and that seemed very complicated (at least prior to Mathematica 9) for me. And I haven't used WB up to now. –  Ronny Dec 18 '12 at 6:14
There are a lot of nice screencasts for Workbench to show you how to do things. This one is for doc builds: wolfram.com/broadcast/screencasts/workbench/documentation –  Joel Klein Dec 18 '12 at 6:53
Though both other answers were great, too, I think this one fits best, due to the idea to use Workbench for the documentation and the link to the Screencast in the links. –  Ronny Dec 21 '12 at 12:24

First, I want to say that I don't like heavily formatted usages messages. A usage message should be a short description in a form of a simple ascii message, so that it can be viewed even without a front-end.

Nevertheless, let me try to give you a hint here. I would do the following:

1. write your usage messages in a separate package-notebook in the Mathematica front-end, where you can look at it as formatted text and not as string-expression.
2. store this notebook as package Usage.m side by side to your implementation package.
3. load this package in the Kernel/init.m of you package.

## 1. Package notebook

When you use the Mathematica front-end for editing, you can input any special box form without caring about the underlying, complicated string-expression.

## 2. Store the package

After you saved the package, it is stored on disk as

(* ::Package:: *)

BeginPackage["YourContext"];
f::usage="f[x] calculates \!$$\*SubsuperscriptBox[\(\[Integral]$$, $$min$$,\
$$max$$]\)f[x]\[DifferentialD]x."
EndPackage[];


but you don't have to care about it, because 1. you never see this in e.g. the Wolfram Workbench because your implementation is in another file and 2. you edit this package always in the front-end.

## 3. Loading the usages

Just load this in the init.m. It's probably the best, when you look how it is done in the VectorFieldPlots package in the AddOns/Packages path. Their init.m looks like

(* initialization file for the vector field plots package VectorFieldPlots *)

Get["VectorFieldPlotsUsage"];
Get["VectorFieldPlotsVectorFieldPlots"];
Get["VectorFieldPlotsVectorFieldPlots3D"];


and the file structure is

.
├── Kernel
│   └── init.m
├── PacletInfo.m
├── Usage.m
├── VectorFieldPlots3D.m
└── VectorFieldPlots.m

-
That's a neat idea, because now I'm storing it in the package-m File and it sometimes gets messed up due to missing escaped ". And you're right, of course it should not be overloaded with formated stuff. I was thinking about emphazising (in italics) parameters, bold face matrices and vectors and perhaps very small explaining formulae as the one you used in your example. I'll try your approach these days, but it sounds quite nice. –  Ronny Dec 18 '12 at 13:19

My advice is to keep things like this as simple as possible. I have found that copying the official documentation is adequate and generates few (... no) error messages.

With v8, there appears to be a newer WRI style. Before this, usages were written

function::usage = "function[arguments, options] does ...";


An example of this form can be found in

ToFileName[{$AddOnsDirectory, AddOns, ExtraPackages, Utilities}, CleanSlate.m].  From v8 onwards, the style seems to have changed to If[!ValueQ[function], function::usage = "function[arguments, options] does ..." ];  An example of this form can be found in ToFileName[{$AddOnsDirectory, AddOns, Packages, ANOVA}, ANOVA.m].


I have found no practical difference between the two versions, but your kilometerage may vary.

-
What's the difference when using the second? Is there an advantage? –  Ronny Dec 17 '12 at 11:08
@Ronny, I defer this question to experts. However, I have not noticed any difference between the two versions, either in performance or functionality. –  dwa Dec 17 '12 at 13:27
Have you seen this? –  Ｊ. Ｍ. Apr 15 '13 at 17:32
@J.M. Interdasting! –  dwa Apr 16 '13 at 8:11