Mercurial > octave-nkf
annotate doc/interpreter/tips.txi @ 9758:09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Use double spaces after a period at start of new sentence
Do not use @iftex blocks where @tex block is sufficient
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Fri, 23 Oct 2009 09:28:14 -0700 |
parents | d85a43495faa |
children | c3df189b1b15 |
rev | line source |
---|---|
8920 | 1 @c Copyright (C) 1996, 1997, 1999, 2002, 2004, 2005, 2007, 2008, |
2 @c 2009 John W. Eaton | |
7018 | 3 @c |
4 @c This file is part of Octave. | |
5 @c | |
6 @c Octave is free software; you can redistribute it and/or modify it | |
7 @c under the terms of the GNU General Public License as published by the | |
8 @c Free Software Foundation; either version 3 of the License, or (at | |
9 @c your option) any later version. | |
10 @c | |
11 @c Octave is distributed in the hope that it will be useful, but WITHOUT | |
12 @c ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 @c FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 @c for more details. | |
15 @c | |
16 @c You should have received a copy of the GNU General Public License | |
17 @c along with Octave; see the file COPYING. If not, see | |
18 @c <http://www.gnu.org/licenses/>. | |
3294 | 19 |
9032
349616d9c38e
Cleanup top-level documentation menu in octave.texi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
20 @node Tips and Standards |
3294 | 21 @appendix Tips and Standards |
22 @cindex tips | |
23 @cindex standards of coding style | |
24 @cindex coding standards | |
25 | |
26 This chapter describes no additional features of Octave. Instead it | |
27 gives advice on making effective use of the features described in the | |
28 previous chapters. | |
29 | |
30 @menu | |
31 * Style Tips:: Writing clean and robust programs. | |
32 * Coding Tips:: Making code run faster. | |
33 * Comment Tips:: Conventions for writing comments. | |
34 * Function Headers:: Standard headers for functions. | |
6574 | 35 * Documentation Tips:: Writing readable documentation strings. |
3294 | 36 @end menu |
37 | |
4167 | 38 @node Style Tips |
3294 | 39 @section Writing Clean Octave Programs |
40 | |
41 Here are some tips for avoiding common errors in writing Octave code | |
42 intended for widespread use: | |
43 | |
44 @itemize @bullet | |
45 @item | |
46 Since all global variables share the same name space, and all functions | |
47 share another name space, you should choose a short word to distinguish | |
48 your program from other Octave programs. Then take care to begin the | |
49 names of all global variables, constants, and functions with the chosen | |
50 prefix. This helps avoid name conflicts. | |
51 | |
52 If you write a function that you think ought to be added to Octave under | |
53 a certain name, such as @code{fiddle_matrix}, don't call it by that name | |
54 in your program. Call it @code{mylib_fiddle_matrix} in your program, | |
5041 | 55 and send mail to @email{maintainers@@octave.org} suggesting that it |
3294 | 56 be added to Octave. If and when it is, the name can be changed easily |
57 enough. | |
58 | |
59 If one prefix is insufficient, your package may use two or three | |
60 alternative common prefixes, so long as they make sense. | |
61 | |
62 Separate the prefix from the rest of the symbol name with an underscore | |
63 @samp{_}. This will be consistent with Octave itself and with most | |
64 Octave programs. | |
65 | |
66 @item | |
67 When you encounter an error condition, call the function @code{error} | |
68 (or @code{usage}). The @code{error} and @code{usage} functions do not | |
69 return. | |
70 @xref{Errors}. | |
71 | |
72 @item | |
73 Please put a copyright notice on the file if you give copies to anyone. | |
74 Use the same lines that appear at the top of the function files | |
75 distributed with Octave. If you have not signed papers to assign the | |
76 copyright to anyone else, then place your name in the copyright notice. | |
77 @end itemize | |
78 | |
4167 | 79 @node Coding Tips |
3294 | 80 @section Tips for Making Code Run Faster. |
81 @cindex execution speed | |
82 @cindex speedups | |
83 | |
84 Here are some ways of improving the execution speed of Octave programs. | |
85 | |
86 @itemize @bullet | |
87 @item | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
88 Vectorize loops. For instance, rather than |
9356 | 89 @example |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
90 @group |
9356 | 91 for i = 1:n-1 |
92 a(i) = b(i+1) - b(i); | |
93 endfor | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
94 @end group |
9356 | 95 @end example |
96 | |
97 write | |
98 | |
99 @example | |
100 a = b(2:n) - b(1:n-1); | |
101 @end example | |
102 | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
103 This is especially important for loops with "cheap" bodies. Often it suffices to vectorize |
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
104 just the innermost loop to get acceptable performance. A general rule of thumb is that the |
9356 | 105 "order" of the vectorized body should be greater or equal to the "order" of the enclosing loop. |
106 | |
107 @item | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
108 Use built-in and library functions if possible. Built-in and compiled functions are very fast. |
9356 | 109 Even with a m-file library function, chances are good that it is already optimized, or will be |
110 optimized more in a future release. | |
111 | |
112 @item | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
113 Avoid computing costly intermediate results multiple times. Octave currently |
9356 | 114 does not eliminate common subexpressions. |
9486 | 115 Also, certain internal computation results are cached for variables. |
116 For instance, if a matrix variable is used multiple times as an index, | |
117 checking the indices (and internal conversion to integers) is only done once. | |
3294 | 118 |
119 @item | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
120 Be aware of lazy copies (copy-on-write). When a copy of an object |
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
121 is created, the data is not immediately copied, but rather shared. The actual |
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
122 copying is postponed until the copied data needs to be modified. For example: |
9356 | 123 |
124 @example | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
125 @group |
9356 | 126 a = zeros (1000); # create a 1000x1000 matrix |
127 b = a; # no copying done here | |
128 b(1) = 1; # copying done here | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
129 @end group |
9356 | 130 @end example |
131 | |
132 Lazy copying applies to whole Octave objects such as matrices, cells, struct, | |
133 and also individual cell or struct elements (not array elements). | |
134 | |
135 Additionally, index expressions also use lazy copying when Octave can determine | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
136 that the indexed portion is contiguous in memory. For example: |
9356 | 137 |
138 @example | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
139 @group |
9356 | 140 a = zeros (1000); # create a 1000x1000 matrix |
141 b = a(:,10:100); # no copying done here | |
142 b = a(10:100,:); # copying done here | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
143 @end group |
9356 | 144 @end example |
145 | |
146 This applies to arrays (matrices), cell arrays, and structs indexed using (). | |
147 Index expressions generating cs-lists can also benefit of shallow copying | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
148 in some cases. In particular, when @var{a} is a struct array, expressions like |
9356 | 149 @code{@{a.x@}, @{a(:,2).x@}} will use lazy copying, so that data can be shared |
150 between a struct array and a cell array. | |
151 | |
152 Most indexing expressions do not live longer than their `parent' objects. | |
153 In rare cases, however, a lazily copied slice outlasts its parent, in which | |
154 case it becomes orphaned, still occupying unnecessarily more memory than needed. | |
155 To provide a remedy working in most real cases, | |
156 Octave checks for orphaned lazy slices at certain situations, when a value | |
157 is stored into a "permanent" location, such as a named variable or cell or | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
158 struct element, and possibly economizes them. For example |
9356 | 159 |
160 @example | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
161 @group |
9356 | 162 a = zeros (1000); # create a 1000x1000 matrix |
163 b = a(:,10:100); # lazy slice | |
164 a = []; # the original a array is still allocated | |
165 c@{1@} = b; # b is reallocated at this point | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
166 @end group |
9356 | 167 @end example |
3294 | 168 |
169 @item | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
170 Avoid deep recursion. Function calls to m-file functions carry a relatively significant overhead, |
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
171 so rewriting a recursion as a loop often helps. Also, note that the maximum level of recursion is |
9356 | 172 limited. |
173 | |
174 @item | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
175 Avoid resizing matrices unnecessarily. When building a single result |
3294 | 176 matrix from a series of calculations, set the size of the result matrix |
177 first, then insert values into it. Write | |
178 | |
179 @example | |
180 @group | |
181 result = zeros (big_n, big_m) | |
182 for i = over:and_over | |
183 r1 = @dots{} | |
184 r2 = @dots{} | |
185 result (r1, r2) = new_value (); | |
186 endfor | |
187 @end group | |
188 @end example | |
189 | |
190 @noindent | |
191 instead of | |
192 | |
193 @example | |
194 @group | |
195 result = []; | |
196 for i = ever:and_ever | |
197 result = [ result, new_value() ]; | |
198 endfor | |
199 @end group | |
200 @end example | |
201 | |
9356 | 202 Sometimes the number of items can't be computed in advance, and stack-like operations |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
203 are needed. When elements are being repeatedly inserted at/removed from the end of an |
9356 | 204 array, Octave detects it as stack usage and attempts to use a smarter memory management |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
205 strategy preallocating the array in bigger chunks. Likewise works for cell and |
9356 | 206 struct arrays. |
207 | |
208 @example | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
209 @group |
9356 | 210 a = []; |
211 while (condition) | |
212 @dots{} | |
213 a(end+1) = value; # "push" operation | |
214 @dots{} | |
215 a(end) = []; # "pop" operation | |
216 @dots{} | |
217 endwhile | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
218 @end group |
9356 | 219 @end example |
220 | |
3294 | 221 @item |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
222 Use @code{cellfun} intelligently. The @code{cellfun} function is a useful tool |
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
223 for avoiding loops. @xref{Processing Data in Cell Arrays}. |
9356 | 224 @code{cellfun} is often use with anonymous function handles; however, calling |
225 an anonymous function involves an overhead quite comparable to the overhead | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
226 of an m-file function. Passing a handle to a built-in function is faster, |
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
227 because the interpreter is not involved in the internal loop. For example: |
9356 | 228 |
229 @example | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
230 @group |
9356 | 231 a = @{@dots{}@} |
232 v = cellfun (@@(x) det(x), a); # compute determinants | |
233 v = cellfun (@@det, a); # faster | |
9758
09da0bd91412
Periodic grammar check of Octave documentation files to ensure common format
Rik <rdrider0-list@yahoo.com>
parents:
9486
diff
changeset
|
234 @end group |
9356 | 235 @end example |
236 | |
237 @item | |
238 Avoid calling @code{eval} or @code{feval} excessively, because | |
3294 | 239 they require Octave to parse input or look up the name of a function in |
240 the symbol table. | |
241 | |
242 If you are using @code{eval} as an exception handling mechanism and not | |
243 because you need to execute some arbitrary text, use the @code{try} | |
9038
fca0dc2fb042
Cleanup documentation files stmt.texi and func.texi
Rik <rdrider0-list@yahoo.com>
parents:
9032
diff
changeset
|
244 statement instead. @xref{The @code{try} Statement}. |
3294 | 245 |
246 @item | |
247 If you are calling lots of functions but none of them will need to | |
248 change during your run, set the variable | |
249 @code{ignore_function_time_stamp} to @code{"all"} so that Octave doesn't | |
250 waste a lot of time checking to see if you have updated your function | |
251 files. | |
252 @end itemize | |
253 | |
4167 | 254 @node Comment Tips |
3294 | 255 @section Tips on Writing Comments |
256 | |
257 Here are the conventions to follow when writing comments. | |
258 | |
259 @table @samp | |
260 @item # | |
261 Comments that start with a single sharp-sign, @samp{#}, should all be | |
262 aligned to the same column on the right of the source code. Such | |
263 comments usually explain how the code on the same line does its job. In | |
264 the Emacs mode for Octave, the @kbd{M-;} (@code{indent-for-comment}) | |
265 command automatically inserts such a @samp{#} in the right place, or | |
266 aligns such a comment if it is already present. | |
267 | |
268 @item ## | |
7345 | 269 Comments that start with a double sharp-sign, @samp{##}, should be aligned to |
3294 | 270 the same level of indentation as the code. Such comments usually |
271 describe the purpose of the following lines or the state of the program | |
272 at that point. | |
273 @end table | |
274 | |
275 @noindent | |
276 The indentation commands of the Octave mode in Emacs, such as @kbd{M-;} | |
277 (@code{indent-for-comment}) and @kbd{TAB} (@code{octave-indent-line}) | |
278 automatically indent comments according to these conventions, | |
279 depending on the number of semicolons. @xref{Comments,, | |
280 Manipulating Comments, emacs, The GNU Emacs Manual}. | |
281 | |
4167 | 282 @node Function Headers |
3294 | 283 @section Conventional Headers for Octave Functions |
284 @cindex header comments | |
285 | |
286 Octave has conventions for using special comments in function files | |
287 to give information such as who wrote them. This section explains these | |
288 conventions. | |
289 | |
290 The top of the file should contain a copyright notice, followed by a | |
291 block of comments that can be used as the help text for the function. | |
292 Here is an example: | |
293 | |
294 @example | |
6778 | 295 ## Copyright (C) 1996, 1997, 2007 John W. Eaton |
3294 | 296 ## |
297 ## This file is part of Octave. | |
298 ## | |
299 ## Octave is free software; you can redistribute it and/or | |
300 ## modify it under the terms of the GNU General Public | |
301 ## License as published by the Free Software Foundation; | |
7081 | 302 ## either version 3 of the License, or (at your option) any |
303 ## later version. | |
3294 | 304 ## |
305 ## Octave is distributed in the hope that it will be useful, | |
306 ## but WITHOUT ANY WARRANTY; without even the implied | |
307 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
308 ## PURPOSE. See the GNU General Public License for more | |
309 ## details. | |
310 ## | |
311 ## You should have received a copy of the GNU General Public | |
312 ## License along with Octave; see the file COPYING. If not, | |
7016 | 313 ## see <http://www.gnu.org/licenses/>. |
3294 | 314 |
315 ## usage: [IN, OUT, PID] = popen2 (COMMAND, ARGS) | |
316 ## | |
317 ## Start a subprocess with two-way communication. COMMAND | |
318 ## specifies the name of the command to start. ARGS is an | |
319 ## array of strings containing options for COMMAND. IN and | |
320 ## OUT are the file ids of the input and streams for the | |
321 ## subprocess, and PID is the process id of the subprocess, | |
322 ## or -1 if COMMAND could not be executed. | |
323 ## | |
324 ## Example: | |
325 ## | |
326 ## [in, out, pid] = popen2 ("sort", "-nr"); | |
327 ## fputs (in, "these\nare\nsome\nstrings\n"); | |
328 ## fclose (in); | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7345
diff
changeset
|
329 ## while (ischar (s = fgets (out))) |
3294 | 330 ## fputs (stdout, s); |
331 ## endwhile | |
332 ## fclose (out); | |
333 @end example | |
334 | |
335 Octave uses the first block of comments in a function file that do not | |
336 appear to be a copyright notice as the help text for the file. For | |
337 Octave to recognize the first comment block as a copyright notice, it | |
6530 | 338 must start with the word `Copyright' after stripping the leading |
339 comment characters. | |
3294 | 340 |
341 After the copyright notice and help text come several @dfn{header | |
342 comment} lines, each beginning with @samp{## @var{header-name}:}. For | |
343 example, | |
344 | |
345 @example | |
346 @group | |
347 ## Author: jwe | |
348 ## Keywords: subprocesses input-output | |
349 ## Maintainer: jwe | |
350 @end group | |
351 @end example | |
352 | |
353 Here is a table of the conventional possibilities for @var{header-name}: | |
354 | |
355 @table @samp | |
356 @item Author | |
357 This line states the name and net address of at least the principal | |
358 author of the library. | |
359 | |
6670 | 360 @example |
9322 | 361 ## Author: John W. Eaton <jwe@@octave.org> |
6670 | 362 @end example |
3294 | 363 |
364 @item Maintainer | |
365 This line should contain a single name/address as in the Author line, or | |
366 an address only, or the string @samp{jwe}. If there is no maintainer | |
367 line, the person(s) in the Author field are presumed to be the | |
368 maintainers. The example above is mildly bogus because the maintainer | |
369 line is redundant. | |
370 | |
371 The idea behind the @samp{Author} and @samp{Maintainer} lines is to make | |
372 possible a function to ``send mail to the maintainer'' without | |
373 having to mine the name out by hand. | |
374 | |
375 Be sure to surround the network address with @samp{<@dots{}>} if | |
376 you include the person's full name as well as the network address. | |
377 | |
378 @item Created | |
379 This optional line gives the original creation date of the | |
380 file. For historical interest only. | |
381 | |
382 @item Version | |
383 If you wish to record version numbers for the individual Octave program, | |
384 put them in this line. | |
385 | |
386 @item Adapted-By | |
387 In this header line, place the name of the person who adapted the | |
388 library for installation (to make it fit the style conventions, for | |
389 example). | |
390 | |
391 @item Keywords | |
392 This line lists keywords. Eventually, it will be used by an apropos | |
393 command to allow people will find your package when they're looking for | |
394 things by topic area. To separate the keywords, you can use spaces, | |
395 commas, or both. | |
396 @end table | |
397 | |
398 Just about every Octave function ought to have the @samp{Author} and | |
399 @samp{Keywords} header comment lines. Use the others if they are | |
400 appropriate. You can also put in header lines with other header | |
401 names---they have no standard meanings, so they can't do any harm. | |
6574 | 402 |
403 @node Documentation Tips | |
404 @section Tips for Documentation Strings | |
405 | |
406 As noted above, documentation is typically in a commented header block | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
407 on an Octave function following the copyright statement. The help string |
7001 | 408 shown above is an unformatted string and will be displayed as is by |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
409 Octave. Here are some tips for the writing of documentation strings. |
6574 | 410 |
411 @itemize @bullet | |
412 @item | |
413 Every command, function, or variable intended for users to know about | |
414 should have a documentation string. | |
415 | |
416 @item | |
417 An internal variable or subroutine of an Octave program might as well have | |
418 a documentation string. | |
419 | |
420 @item | |
421 The first line of the documentation string should consist of one or two | |
422 complete sentences that stand on their own as a summary. | |
423 | |
424 The documentation string can have additional lines that expand on the | |
425 details of how to use the function or variable. The additional lines | |
426 should also be made up of complete sentences. | |
427 | |
428 @item | |
429 For consistency, phrase the verb in the first sentence of a | |
430 documentation string as an infinitive with ``to'' omitted. For | |
431 instance, use ``Return the frob of A and B.'' in preference to ``Returns | |
432 the frob of A and B@.'' Usually it looks good to do likewise for the | |
433 rest of the first paragraph. Subsequent paragraphs usually look better | |
434 if they have proper subjects. | |
435 | |
436 @item | |
437 Write documentation strings in the active voice, not the passive, and in | |
438 the present tense, not the future. For instance, use ``Return a list | |
439 containing A and B.'' instead of ``A list containing A and B will be | |
440 returned.'' | |
441 | |
442 @item | |
443 Avoid using the word ``cause'' (or its equivalents) unnecessarily. | |
444 Instead of, ``Cause Octave to display text in boldface,'' write just | |
445 ``Display text in boldface.'' | |
446 | |
447 @item | |
448 Do not start or end a documentation string with whitespace. | |
449 | |
450 @item | |
451 Format the documentation string so that it fits in an Emacs window on an | |
452 80-column screen. It is a good idea for most lines to be no wider than | |
453 60 characters. | |
454 | |
455 However, rather than simply filling the entire documentation string, you | |
456 can make it much more readable by choosing line breaks with care. | |
457 Use blank lines between topics if the documentation string is long. | |
458 | |
459 @item | |
460 @strong{Do not} indent subsequent lines of a documentation string so | |
461 that the text is lined up in the source code with the text of the first | |
462 line. This looks nice in the source code, but looks bizarre when users | |
463 view the documentation. Remember that the indentation before the | |
464 starting double-quote is not part of the string! | |
465 | |
466 @item | |
467 The documentation string for a variable that is a yes-or-no flag should | |
468 start with words such as ``Nonzero means@dots{}'', to make it clear that | |
469 all nonzero values are equivalent and indicate explicitly what zero and | |
470 nonzero mean. | |
471 | |
472 @item | |
473 When a function's documentation string mentions the value of an argument | |
474 of the function, use the argument name in capital letters as if it were | |
475 a name for that value. Thus, the documentation string of the operator | |
476 @code{/} refers to its second argument as @samp{DIVISOR}, because the | |
477 actual argument name is @code{divisor}. | |
478 | |
479 Also use all caps for meta-syntactic variables, such as when you show | |
480 the decomposition of a list or vector into subunits, some of which may | |
481 vary. | |
482 @end itemize | |
483 | |
484 Octave also allows extensive formatting of the help string of functions | |
485 using Texinfo. The effect on the online documentation is relatively | |
486 small, but makes the help string of functions conform to the help of | |
487 Octave's own functions. However, the effect on the appearance of printed | |
488 or online documentation will be greatly improved. | |
489 | |
490 The fundamental building block of Texinfo documentation strings is the | |
491 Texinfo-macro @code{@@deftypefn}, which takes three arguments: The class | |
492 the function is in, its output arguments, and the function's | |
493 signature. Typical classes for functions include @code{Function File} | |
494 for standard Octave functions, and @code{Loadable Function} for | |
495 dynamically linked functions. A skeletal Texinfo documentation string | |
496 therefore looks like this | |
497 | |
498 @example | |
499 @group | |
500 -*- texinfo -*- | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
501 @@deftypefn@{Function File@} @{@@var@{ret@} =@} fn (@dots{}) |
6574 | 502 @@cindex index term |
7081 | 503 Help text in Texinfo format. Code samples should be marked |
504 like @@code@{sample of code@} and variables should be marked | |
505 as @@var@{variable@}. | |
506 @@seealso@{fn2@} | |
6574 | 507 @@end deftypefn |
508 @end group | |
509 @end example | |
510 | |
511 This help string must be commented in user functions, or in the help | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
512 string of the @w{@code{DEFUN_DLD}} macro for dynamically loadable |
6574 | 513 functions. The important aspects of the documentation string are |
514 | |
515 @table @asis | |
516 @item -*- texinfo -*- | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
7768
diff
changeset
|
517 This string signals Octave that the following text is in Texinfo format, |
6574 | 518 and should be the first part of any help string in Texinfo format. |
519 @item @@deftypefn@{class@} @dots{} @@end deftypefn | |
520 The entire help string should be enclosed within the block defined by | |
521 deftypefn. | |
522 @item @@cindex index term | |
523 This generates an index entry, and can be useful when the function is | |
524 included as part of a larger piece of documentation. It is ignored | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
525 within Octave's help viewer. Only one index term may appear per line |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
526 but multiple @@cindex lines are valid if the function should be |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
527 filed under different terms. |
6574 | 528 @item @@var@{variable@} |
529 All variables should be marked with this macro. The markup of variables | |
530 is then changed appropriately for display. | |
531 @item @@code@{sample of code@} | |
532 All samples of code should be marked with this macro for the same | |
533 reasons as the @@var macro. | |
534 @item @@seealso@{function2@} | |
535 This is a comma separated list of function names that allows cross | |
536 referencing from one function documentation string to another. | |
537 @end table | |
538 | |
539 Texinfo format has been designed to generate output for online viewing | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
540 with text terminals as well as generating high-quality printed output. |
6574 | 541 To these ends, Texinfo has commands which control the diversion of parts |
542 of the document into a particular output processor. Three formats are | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
543 of importance: info, html and @TeX{}. These are selected with |
6574 | 544 |
545 @example | |
546 @group | |
547 @@ifinfo | |
548 Text area for info only | |
549 @@end ifinfo | |
550 @end group | |
551 @end example | |
552 | |
553 @example | |
554 @group | |
555 @@ifhtml | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
556 Text area for HTML only |
6574 | 557 @@end ifhtml |
558 @end group | |
559 @end example | |
560 | |
561 @example | |
562 @group | |
563 @@tex | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
564 Text area for TeX only |
6574 | 565 @@end tex |
566 @end group | |
567 @end example | |
568 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
569 Note that often @TeX{} output can be used in html documents and so often |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
570 the @code{@@ifhtml} blocks are unnecessary. If no specific output |
6574 | 571 processor is chosen, by default, the text goes into all output |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
572 processors. It is usual to have the above blocks in pairs to allow the |
6574 | 573 same information to be conveyed in all output formats, but with a |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
574 different markup. Currently, most Octave documentation only makes a |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
575 distinction between @TeX{} and all other formats. Therefore, the |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
576 following construct is seen repeatedly. |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
577 |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
578 @example |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
579 @group |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
580 @@tex |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
581 text for TeX only |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
582 @@end tex |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
583 @@ifnottex |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
584 text for info, HTML, plaintext |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
585 @@end ifnottex |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
586 @end group |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
587 @end example |
6574 | 588 |
589 Another important feature of Texinfo that is often used in Octave help | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
590 strings is the @code{@@example} environment. An example of its use is |
6574 | 591 |
592 @example | |
593 @group | |
594 @@example | |
595 @@group | |
596 @@code@{2 * 2@} | |
597 @@result@{@} 4 | |
598 @@end group | |
599 @@end example | |
600 @end group | |
601 @end example | |
602 | |
603 which produces | |
604 | |
605 @example | |
606 @group | |
607 @code{2 * 2} | |
608 @result{} 4 | |
609 @end group | |
610 @end example | |
611 | |
612 The @code{@@group} block prevents the example from being split across a | |
613 page boundary, while the @code{@@result@{@}} macro produces a right | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
614 arrow signifying the result of a command. If your example is larger than |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
615 20 lines it is better NOT to use grouping so that a reasonable page boundary |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
616 can be calculated. |
6574 | 617 |
8828 | 618 In many cases a function has multiple ways in which it can be called, |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
619 and the @code{@@deftypefnx} macro can be used to give alternatives. For |
6574 | 620 example |
621 | |
622 @example | |
623 @group | |
624 -*- texinfo -*- | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
625 @@deftypefn@{Function File@} @{@@var@{a@} =@} fn (@@var@{x@}, @dots{}) |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
626 @@deftypefnx@{Function File@} @{@@var@{a@} =@} fn (@@var@{y@}, @dots{}) |
6574 | 627 Help text in Texinfo format. |
628 @@end deftypefn | |
629 @end group | |
630 @end example | |
631 | |
632 Many complete examples of Texinfo documentation can be taken from the | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
633 help strings for the Octave functions themselves. A relatively complete |
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
634 example of which is the @code{nchoosek} function. The Texinfo |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
635 documentation string for @code{nchoosek} is |
6574 | 636 |
637 @example | |
638 -*- texinfo -*- | |
7081 | 639 @@deftypefn @{Function File@} @{@} nchoosek (@@var@{n@}, @@var@{k@}) |
6574 | 640 |
7081 | 641 Compute the binomial coefficient or all combinations of |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
642 @@var@{n@}. If @@var@{n@} is a scalar then, calculate the |
7081 | 643 binomial coefficient of @@var@{n@} and @@var@{k@}, defined as |
6574 | 644 |
645 @@tex | |
646 $$ | |
647 @{n \choose k@} = @{n (n-1) (n-2) \cdots (n-k+1) \over k!@} | |
648 $$ | |
649 @@end tex | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
650 @@ifnottex |
6574 | 651 |
652 @@example | |
653 @@group | |
654 / \ | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
655 | n | n (n-1) (n-2) @dots{} (n-k+1) |
6574 | 656 | | = ------------------------- |
657 | k | k! | |
658 \ / | |
659 @@end group | |
660 @@end example | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
661 @@end ifnottex |
6574 | 662 |
7081 | 663 If @@var@{n@} is a vector, this generates all combinations |
664 of the elements of @@var@{n@}, taken @@var@{k@} at a time, | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
665 one row per combination. The resulting @@var@{c@} has size |
7081 | 666 @@code@{[nchoosek (length (@@var@{n@}),@@var@{k@}), @@var@{k@}]@}. |
6574 | 667 |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
668 @@code@{nchoosek@} works only for non-negative integer arguments; use |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
669 @@code@{bincoeff@} for non-integer scalar arguments and for using vector |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
670 arguments to compute many coefficients at once. |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
671 |
6574 | 672 @@seealso@{bincoeff@} |
673 @@end deftypefn | |
674 @end example | |
675 | |
676 which demonstrates most of the concepts discussed above. | |
677 @iftex | |
678 This documentation string renders as | |
679 | |
680 @c Note actually use the output of info below rather than try and | |
681 @c reproduce it here to prevent it looking different than how it would | |
682 @c appear with info. | |
683 @example | |
684 @group | |
685 -- Function File: C = nchoosek (N, K) | |
7081 | 686 Compute the binomial coefficient or all combinations |
687 of N. If N is a scalar then, calculate the binomial | |
688 coefficient of N and K, defined as | |
6574 | 689 |
690 / \ | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
691 | n | n (n-1) (n-2) @dots{} (n-k+1) n! |
7081 | 692 | | = ------------------------- = --------- |
693 | k | k! k! (n-k)! | |
6574 | 694 \ / |
695 | |
7081 | 696 If N is a vector generate all combinations of the |
697 elements of N, taken K at a time, one row per | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
698 combination. The resulting C has size `[nchoosek |
7081 | 699 (length (N), K), K]'. |
6574 | 700 |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
701 `nchoosek' works only for non-negative integer |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
702 arguments; use `bincoeff' for non-integer scalar |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
703 arguments and for using vector arguments to compute |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
704 many coefficients at once. |
6574 | 705 |
706 See also: bincoeff. | |
707 @end group | |
708 @end example | |
709 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
710 using info, whereas in a printed documentation using @TeX{} it will appear |
6574 | 711 as |
712 | |
713 @deftypefn {Function File} {@var{c} =} nchoosek (@var{n}, @var{k}) | |
714 | |
6576 | 715 Compute the binomial coefficient or all combinations of @var{n}. |
6574 | 716 If @var{n} is a scalar then, calculate the binomial coefficient |
717 of @var{n} and @var{k}, defined as | |
718 | |
719 @tex | |
720 $$ | |
721 {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} | |
722 $$ | |
723 @end tex | |
724 | |
725 If @var{n} is a vector generate all combinations of the elements | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
726 of @var{n}, taken @var{k} at a time, one row per combination. The |
6574 | 727 resulting @var{c} has size @code{[nchoosek (length (@var{n}), |
728 @var{k}), @var{k}]}. | |
729 | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
730 @code{nchoosek} works only for non-negative integer arguments; use |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
731 @code{bincoeff} for non-integer scalar arguments and for using vector |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
732 arguments to compute many coefficients at once. |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
733 |
6574 | 734 @seealso{bincoeff} |
735 @end deftypefn | |
736 | |
737 @end iftex |