Mercurial > hg > octave-lyh
annotate doc/interpreter/tips.txi @ 9210:a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
rather than @iftex construction.
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sun, 17 May 2009 14:17:32 -0700 |
parents | 923c7cb7f13f |
children | 66fdc831c580 |
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 | |
88 Avoid looping wherever possible. | |
89 | |
90 @item | |
91 Use iteration rather than recursion whenever possible. | |
92 Function calls are slow in Octave. | |
93 | |
94 @item | |
95 Avoid resizing matrices unnecessarily. When building a single result | |
96 matrix from a series of calculations, set the size of the result matrix | |
97 first, then insert values into it. Write | |
98 | |
99 @example | |
100 @group | |
101 result = zeros (big_n, big_m) | |
102 for i = over:and_over | |
103 r1 = @dots{} | |
104 r2 = @dots{} | |
105 result (r1, r2) = new_value (); | |
106 endfor | |
107 @end group | |
108 @end example | |
109 | |
110 @noindent | |
111 instead of | |
112 | |
113 @example | |
114 @group | |
115 result = []; | |
116 for i = ever:and_ever | |
117 result = [ result, new_value() ]; | |
118 endfor | |
119 @end group | |
120 @end example | |
121 | |
122 @item | |
123 Avoid calling @code{eval} or @code{feval} whenever possible, because | |
124 they require Octave to parse input or look up the name of a function in | |
125 the symbol table. | |
126 | |
127 If you are using @code{eval} as an exception handling mechanism and not | |
128 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
|
129 statement instead. @xref{The @code{try} Statement}. |
3294 | 130 |
131 @item | |
132 If you are calling lots of functions but none of them will need to | |
133 change during your run, set the variable | |
134 @code{ignore_function_time_stamp} to @code{"all"} so that Octave doesn't | |
135 waste a lot of time checking to see if you have updated your function | |
136 files. | |
137 @end itemize | |
138 | |
4167 | 139 @node Comment Tips |
3294 | 140 @section Tips on Writing Comments |
141 | |
142 Here are the conventions to follow when writing comments. | |
143 | |
144 @table @samp | |
145 @item # | |
146 Comments that start with a single sharp-sign, @samp{#}, should all be | |
147 aligned to the same column on the right of the source code. Such | |
148 comments usually explain how the code on the same line does its job. In | |
149 the Emacs mode for Octave, the @kbd{M-;} (@code{indent-for-comment}) | |
150 command automatically inserts such a @samp{#} in the right place, or | |
151 aligns such a comment if it is already present. | |
152 | |
153 @item ## | |
7345 | 154 Comments that start with a double sharp-sign, @samp{##}, should be aligned to |
3294 | 155 the same level of indentation as the code. Such comments usually |
156 describe the purpose of the following lines or the state of the program | |
157 at that point. | |
158 @end table | |
159 | |
160 @noindent | |
161 The indentation commands of the Octave mode in Emacs, such as @kbd{M-;} | |
162 (@code{indent-for-comment}) and @kbd{TAB} (@code{octave-indent-line}) | |
163 automatically indent comments according to these conventions, | |
164 depending on the number of semicolons. @xref{Comments,, | |
165 Manipulating Comments, emacs, The GNU Emacs Manual}. | |
166 | |
4167 | 167 @node Function Headers |
3294 | 168 @section Conventional Headers for Octave Functions |
169 @cindex header comments | |
170 | |
171 Octave has conventions for using special comments in function files | |
172 to give information such as who wrote them. This section explains these | |
173 conventions. | |
174 | |
175 The top of the file should contain a copyright notice, followed by a | |
176 block of comments that can be used as the help text for the function. | |
177 Here is an example: | |
178 | |
179 @example | |
6778 | 180 ## Copyright (C) 1996, 1997, 2007 John W. Eaton |
3294 | 181 ## |
182 ## This file is part of Octave. | |
183 ## | |
184 ## Octave is free software; you can redistribute it and/or | |
185 ## modify it under the terms of the GNU General Public | |
186 ## License as published by the Free Software Foundation; | |
7081 | 187 ## either version 3 of the License, or (at your option) any |
188 ## later version. | |
3294 | 189 ## |
190 ## Octave is distributed in the hope that it will be useful, | |
191 ## but WITHOUT ANY WARRANTY; without even the implied | |
192 ## warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR | |
193 ## PURPOSE. See the GNU General Public License for more | |
194 ## details. | |
195 ## | |
196 ## You should have received a copy of the GNU General Public | |
197 ## License along with Octave; see the file COPYING. If not, | |
7016 | 198 ## see <http://www.gnu.org/licenses/>. |
3294 | 199 |
200 ## usage: [IN, OUT, PID] = popen2 (COMMAND, ARGS) | |
201 ## | |
202 ## Start a subprocess with two-way communication. COMMAND | |
203 ## specifies the name of the command to start. ARGS is an | |
204 ## array of strings containing options for COMMAND. IN and | |
205 ## OUT are the file ids of the input and streams for the | |
206 ## subprocess, and PID is the process id of the subprocess, | |
207 ## or -1 if COMMAND could not be executed. | |
208 ## | |
209 ## Example: | |
210 ## | |
211 ## [in, out, pid] = popen2 ("sort", "-nr"); | |
212 ## fputs (in, "these\nare\nsome\nstrings\n"); | |
213 ## fclose (in); | |
7768
a2d9f325b65a
Use isschar instead of deprecated isstr
Rafael Laboissiere <rafael@debian.org>
parents:
7345
diff
changeset
|
214 ## while (ischar (s = fgets (out))) |
3294 | 215 ## fputs (stdout, s); |
216 ## endwhile | |
217 ## fclose (out); | |
218 @end example | |
219 | |
220 Octave uses the first block of comments in a function file that do not | |
221 appear to be a copyright notice as the help text for the file. For | |
222 Octave to recognize the first comment block as a copyright notice, it | |
6530 | 223 must start with the word `Copyright' after stripping the leading |
224 comment characters. | |
3294 | 225 |
226 After the copyright notice and help text come several @dfn{header | |
227 comment} lines, each beginning with @samp{## @var{header-name}:}. For | |
228 example, | |
229 | |
230 @example | |
231 @group | |
232 ## Author: jwe | |
233 ## Keywords: subprocesses input-output | |
234 ## Maintainer: jwe | |
235 @end group | |
236 @end example | |
237 | |
238 Here is a table of the conventional possibilities for @var{header-name}: | |
239 | |
240 @table @samp | |
241 @item Author | |
242 This line states the name and net address of at least the principal | |
243 author of the library. | |
244 | |
6670 | 245 @example |
3294 | 246 ## Author: John W. Eaton <jwe@@bevo.che.wisc.edu> |
6670 | 247 @end example |
3294 | 248 |
249 @item Maintainer | |
250 This line should contain a single name/address as in the Author line, or | |
251 an address only, or the string @samp{jwe}. If there is no maintainer | |
252 line, the person(s) in the Author field are presumed to be the | |
253 maintainers. The example above is mildly bogus because the maintainer | |
254 line is redundant. | |
255 | |
256 The idea behind the @samp{Author} and @samp{Maintainer} lines is to make | |
257 possible a function to ``send mail to the maintainer'' without | |
258 having to mine the name out by hand. | |
259 | |
260 Be sure to surround the network address with @samp{<@dots{}>} if | |
261 you include the person's full name as well as the network address. | |
262 | |
263 @item Created | |
264 This optional line gives the original creation date of the | |
265 file. For historical interest only. | |
266 | |
267 @item Version | |
268 If you wish to record version numbers for the individual Octave program, | |
269 put them in this line. | |
270 | |
271 @item Adapted-By | |
272 In this header line, place the name of the person who adapted the | |
273 library for installation (to make it fit the style conventions, for | |
274 example). | |
275 | |
276 @item Keywords | |
277 This line lists keywords. Eventually, it will be used by an apropos | |
278 command to allow people will find your package when they're looking for | |
279 things by topic area. To separate the keywords, you can use spaces, | |
280 commas, or both. | |
281 @end table | |
282 | |
283 Just about every Octave function ought to have the @samp{Author} and | |
284 @samp{Keywords} header comment lines. Use the others if they are | |
285 appropriate. You can also put in header lines with other header | |
286 names---they have no standard meanings, so they can't do any harm. | |
6574 | 287 |
288 @node Documentation Tips | |
289 @section Tips for Documentation Strings | |
290 | |
291 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
|
292 on an Octave function following the copyright statement. The help string |
7001 | 293 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
|
294 Octave. Here are some tips for the writing of documentation strings. |
6574 | 295 |
296 @itemize @bullet | |
297 @item | |
298 Every command, function, or variable intended for users to know about | |
299 should have a documentation string. | |
300 | |
301 @item | |
302 An internal variable or subroutine of an Octave program might as well have | |
303 a documentation string. | |
304 | |
305 @item | |
306 The first line of the documentation string should consist of one or two | |
307 complete sentences that stand on their own as a summary. | |
308 | |
309 The documentation string can have additional lines that expand on the | |
310 details of how to use the function or variable. The additional lines | |
311 should also be made up of complete sentences. | |
312 | |
313 @item | |
314 For consistency, phrase the verb in the first sentence of a | |
315 documentation string as an infinitive with ``to'' omitted. For | |
316 instance, use ``Return the frob of A and B.'' in preference to ``Returns | |
317 the frob of A and B@.'' Usually it looks good to do likewise for the | |
318 rest of the first paragraph. Subsequent paragraphs usually look better | |
319 if they have proper subjects. | |
320 | |
321 @item | |
322 Write documentation strings in the active voice, not the passive, and in | |
323 the present tense, not the future. For instance, use ``Return a list | |
324 containing A and B.'' instead of ``A list containing A and B will be | |
325 returned.'' | |
326 | |
327 @item | |
328 Avoid using the word ``cause'' (or its equivalents) unnecessarily. | |
329 Instead of, ``Cause Octave to display text in boldface,'' write just | |
330 ``Display text in boldface.'' | |
331 | |
332 @item | |
333 Do not start or end a documentation string with whitespace. | |
334 | |
335 @item | |
336 Format the documentation string so that it fits in an Emacs window on an | |
337 80-column screen. It is a good idea for most lines to be no wider than | |
338 60 characters. | |
339 | |
340 However, rather than simply filling the entire documentation string, you | |
341 can make it much more readable by choosing line breaks with care. | |
342 Use blank lines between topics if the documentation string is long. | |
343 | |
344 @item | |
345 @strong{Do not} indent subsequent lines of a documentation string so | |
346 that the text is lined up in the source code with the text of the first | |
347 line. This looks nice in the source code, but looks bizarre when users | |
348 view the documentation. Remember that the indentation before the | |
349 starting double-quote is not part of the string! | |
350 | |
351 @item | |
352 The documentation string for a variable that is a yes-or-no flag should | |
353 start with words such as ``Nonzero means@dots{}'', to make it clear that | |
354 all nonzero values are equivalent and indicate explicitly what zero and | |
355 nonzero mean. | |
356 | |
357 @item | |
358 When a function's documentation string mentions the value of an argument | |
359 of the function, use the argument name in capital letters as if it were | |
360 a name for that value. Thus, the documentation string of the operator | |
361 @code{/} refers to its second argument as @samp{DIVISOR}, because the | |
362 actual argument name is @code{divisor}. | |
363 | |
364 Also use all caps for meta-syntactic variables, such as when you show | |
365 the decomposition of a list or vector into subunits, some of which may | |
366 vary. | |
367 @end itemize | |
368 | |
369 Octave also allows extensive formatting of the help string of functions | |
370 using Texinfo. The effect on the online documentation is relatively | |
371 small, but makes the help string of functions conform to the help of | |
372 Octave's own functions. However, the effect on the appearance of printed | |
373 or online documentation will be greatly improved. | |
374 | |
375 The fundamental building block of Texinfo documentation strings is the | |
376 Texinfo-macro @code{@@deftypefn}, which takes three arguments: The class | |
377 the function is in, its output arguments, and the function's | |
378 signature. Typical classes for functions include @code{Function File} | |
379 for standard Octave functions, and @code{Loadable Function} for | |
380 dynamically linked functions. A skeletal Texinfo documentation string | |
381 therefore looks like this | |
382 | |
383 @example | |
384 @group | |
385 -*- texinfo -*- | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
386 @@deftypefn@{Function File@} @{@@var@{ret@} =@} fn (@dots{}) |
6574 | 387 @@cindex index term |
7081 | 388 Help text in Texinfo format. Code samples should be marked |
389 like @@code@{sample of code@} and variables should be marked | |
390 as @@var@{variable@}. | |
391 @@seealso@{fn2@} | |
6574 | 392 @@end deftypefn |
393 @end group | |
394 @end example | |
395 | |
396 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
|
397 string of the @w{@code{DEFUN_DLD}} macro for dynamically loadable |
6574 | 398 functions. The important aspects of the documentation string are |
399 | |
400 @table @asis | |
401 @item -*- texinfo -*- | |
8347
fa78cb8d8a5c
corrections for typos
Brian Gough<bjg@network-theory.co.uk>
parents:
7768
diff
changeset
|
402 This string signals Octave that the following text is in Texinfo format, |
6574 | 403 and should be the first part of any help string in Texinfo format. |
404 @item @@deftypefn@{class@} @dots{} @@end deftypefn | |
405 The entire help string should be enclosed within the block defined by | |
406 deftypefn. | |
407 @item @@cindex index term | |
408 This generates an index entry, and can be useful when the function is | |
409 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
|
410 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
|
411 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
|
412 filed under different terms. |
6574 | 413 @item @@var@{variable@} |
414 All variables should be marked with this macro. The markup of variables | |
415 is then changed appropriately for display. | |
416 @item @@code@{sample of code@} | |
417 All samples of code should be marked with this macro for the same | |
418 reasons as the @@var macro. | |
419 @item @@seealso@{function2@} | |
420 This is a comma separated list of function names that allows cross | |
421 referencing from one function documentation string to another. | |
422 @end table | |
423 | |
424 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
|
425 with text terminals as well as generating high-quality printed output. |
6574 | 426 To these ends, Texinfo has commands which control the diversion of parts |
427 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
|
428 of importance: info, html and @TeX{}. These are selected with |
6574 | 429 |
430 @example | |
431 @group | |
432 @@ifinfo | |
433 Text area for info only | |
434 @@end ifinfo | |
435 @end group | |
436 @end example | |
437 | |
438 @example | |
439 @group | |
440 @@ifhtml | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
441 Text area for HTML only |
6574 | 442 @@end ifhtml |
443 @end group | |
444 @end example | |
445 | |
446 @example | |
447 @group | |
448 @@tex | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
449 Text area for TeX only |
6574 | 450 @@end tex |
451 @end group | |
452 @end example | |
453 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
454 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
|
455 the @code{@@ifhtml} blocks are unnecessary. If no specific output |
6574 | 456 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
|
457 processors. It is usual to have the above blocks in pairs to allow the |
6574 | 458 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
|
459 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
|
460 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
|
461 following construct is seen repeatedly. |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
462 |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
463 @example |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
464 @group |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
465 @@tex |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
466 text for TeX only |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
467 @@end tex |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
468 @@ifnottex |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
469 text for info, HTML, plaintext |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
470 @@end ifnottex |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
471 @end group |
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
472 @end example |
6574 | 473 |
474 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
|
475 strings is the @code{@@example} environment. An example of its use is |
6574 | 476 |
477 @example | |
478 @group | |
479 @@example | |
480 @@group | |
481 @@code@{2 * 2@} | |
482 @@result@{@} 4 | |
483 @@end group | |
484 @@end example | |
485 @end group | |
486 @end example | |
487 | |
488 which produces | |
489 | |
490 @example | |
491 @group | |
492 @code{2 * 2} | |
493 @result{} 4 | |
494 @end group | |
495 @end example | |
496 | |
497 The @code{@@group} block prevents the example from being split across a | |
498 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
|
499 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
|
500 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
|
501 can be calculated. |
6574 | 502 |
8828 | 503 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
|
504 and the @code{@@deftypefnx} macro can be used to give alternatives. For |
6574 | 505 example |
506 | |
507 @example | |
508 @group | |
509 -*- texinfo -*- | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
510 @@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
|
511 @@deftypefnx@{Function File@} @{@@var@{a@} =@} fn (@@var@{y@}, @dots{}) |
6574 | 512 Help text in Texinfo format. |
513 @@end deftypefn | |
514 @end group | |
515 @end example | |
516 | |
517 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
|
518 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
|
519 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
|
520 documentation string for @code{nchoosek} is |
6574 | 521 |
522 @example | |
523 -*- texinfo -*- | |
7081 | 524 @@deftypefn @{Function File@} @{@} nchoosek (@@var@{n@}, @@var@{k@}) |
6574 | 525 |
7081 | 526 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
|
527 @@var@{n@}. If @@var@{n@} is a scalar then, calculate the |
7081 | 528 binomial coefficient of @@var@{n@} and @@var@{k@}, defined as |
6574 | 529 |
530 @@tex | |
531 $$ | |
532 @{n \choose k@} = @{n (n-1) (n-2) \cdots (n-k+1) \over k!@} | |
533 $$ | |
534 @@end tex | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
535 @@ifnottex |
6574 | 536 |
537 @@example | |
538 @@group | |
539 / \ | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
540 | n | n (n-1) (n-2) @dots{} (n-k+1) |
6574 | 541 | | = ------------------------- |
542 | k | k! | |
543 \ / | |
544 @@end group | |
545 @@end example | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
546 @@end ifnottex |
6574 | 547 |
7081 | 548 If @@var@{n@} is a vector, this generates all combinations |
549 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
|
550 one row per combination. The resulting @@var@{c@} has size |
7081 | 551 @@code@{[nchoosek (length (@@var@{n@}),@@var@{k@}), @@var@{k@}]@}. |
6574 | 552 |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
553 @@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
|
554 @@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
|
555 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
|
556 |
6574 | 557 @@seealso@{bincoeff@} |
558 @@end deftypefn | |
559 @end example | |
560 | |
561 which demonstrates most of the concepts discussed above. | |
562 @iftex | |
563 This documentation string renders as | |
564 | |
565 @c Note actually use the output of info below rather than try and | |
566 @c reproduce it here to prevent it looking different than how it would | |
567 @c appear with info. | |
568 @example | |
569 @group | |
570 -- Function File: C = nchoosek (N, K) | |
7081 | 571 Compute the binomial coefficient or all combinations |
572 of N. If N is a scalar then, calculate the binomial | |
573 coefficient of N and K, defined as | |
6574 | 574 |
575 / \ | |
9080
ec41eabf4499
Cleanup documentation files dynamic.texi, testfun.texi, tips.texi
Rik <rdrider0-list@yahoo.com>
parents:
9038
diff
changeset
|
576 | n | n (n-1) (n-2) @dots{} (n-k+1) n! |
7081 | 577 | | = ------------------------- = --------- |
578 | k | k! k! (n-k)! | |
6574 | 579 \ / |
580 | |
7081 | 581 If N is a vector generate all combinations of the |
582 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
|
583 combination. The resulting C has size `[nchoosek |
7081 | 584 (length (N), K), K]'. |
6574 | 585 |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
586 `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
|
587 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
|
588 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
|
589 many coefficients at once. |
6574 | 590 |
591 See also: bincoeff. | |
592 @end group | |
593 @end example | |
594 | |
9209
923c7cb7f13f
Simplify TeXinfo files by eliminating redundant @iftex followed by @tex construction.
Rik <rdrider0-list@yahoo.com>
parents:
9080
diff
changeset
|
595 using info, whereas in a printed documentation using @TeX{} it will appear |
6574 | 596 as |
597 | |
598 @deftypefn {Function File} {@var{c} =} nchoosek (@var{n}, @var{k}) | |
599 | |
6576 | 600 Compute the binomial coefficient or all combinations of @var{n}. |
6574 | 601 If @var{n} is a scalar then, calculate the binomial coefficient |
602 of @var{n} and @var{k}, defined as | |
603 | |
604 @tex | |
605 $$ | |
606 {n \choose k} = {n (n-1) (n-2) \cdots (n-k+1) \over k!} | |
607 $$ | |
608 @end tex | |
609 | |
610 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
|
611 of @var{n}, taken @var{k} at a time, one row per combination. The |
6574 | 612 resulting @var{c} has size @code{[nchoosek (length (@var{n}), |
613 @var{k}), @var{k}]}. | |
614 | |
9210
a7a9eecc07b5
Change recommendation for writing documentation to favor @tex
Rik <rdrider0-list@yahoo.com>
parents:
9209
diff
changeset
|
615 @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
|
616 @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
|
617 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
|
618 |
6574 | 619 @seealso{bincoeff} |
620 @end deftypefn | |
621 | |
622 @end iftex |