how-to-paper.scrbl (24048B)
1 #lang scribble/doc 2 @(require scribble/manual scribble/bnf "utils.rkt" 3 pict 4 (for-label scriblib/figure scribble/base (only-in scribble/acmart abstract) 5 (except-in pict table))) 6 7 @(define-syntax-rule (samplemod . text) (codeblock . text)) 8 @(define-syntax-rule (sample a . text) 9 (codeblock #:context #'a #:keep-lang-line? #f 10 "#lang scribble/base" "\n" a . text)) 11 @(define (result . text) (apply nested #:style 'inset text)) 12 13 @(define sep @hspace[1]) 14 15 @(define sub*section subsection) 16 17 @title[#:tag "getting-started"]{Getting Started} 18 19 No matter what you want to do with Scribble, it's best to start by 20 generating a few simple HTML and/or PDF documents. This chapter steps 21 you through the basics, and it ends in @secref["roadmap"] with 22 goal-specific advice on how to continue. 23 24 @section[#:tag "first-example"]{A First Example} 25 26 Create a file @filepath{mouse.scrbl} with this content: 27 28 @samplemod|{ 29 #lang scribble/base 30 31 @title{On the Cookie-Eating Habits of Mice} 32 33 If you give a mouse a cookie, he's going to ask for a 34 glass of milk. 35 }| 36 37 The first line's @racket[#, @hash-lang[] #, 38 @racketmodname[scribble/base]] indicates that the file implements a 39 Scribble document. The document starts in ``text mode,'' and the 40 @litchar["@"] character escapes to operators like @racket[title], 41 where the curly braces return to text mode for the arguments to the 42 operator. The rest is document content. 43 44 Now run the @exec{scribble} command-line program, specifying a mode 45 for the kind of document that you want as output: 46 47 @itemize[ 48 49 @item{Run 50 @commandline{scribble mouse.scrbl} 51 to generate HTML as @filepath{mouse.html}. You may 52 notice that the apostrophe in ``he's'' turned into a 53 curly apostrophe.} 54 55 @item{Run 56 @commandline{scribble --htmls mouse.scrbl} 57 to generate HTML as @filepath{mouse/index.html}. 58 Sub-sections (which we add next) will appear as separate 59 HTML files in the @filepath{mouse} directory.} 60 61 @item{Run 62 @commandline{scribble --pdf mouse.scrbl} 63 to generate PDF as @filepath{mouse.pdf}. This will 64 work only if you have @exec{pdflatex} installed. 65 If you'd like to see the intermediate Latex, try 66 @commandline{scribble --latex mouse.scrbl} 67 to generate @filepath{mouse.tex}.} 68 69 ] 70 71 See @secref["running"] for more information on the @exec{scribble} 72 command-line tool. 73 74 @section{Multiple Sections} 75 76 Add more text to @filepath{mouse.scrbl} so that it looks like this: 77 78 @samplemod|{ 79 #lang scribble/base 80 81 @title{On the Cookie-Eating Habits of Mice} 82 83 If you give a mouse a cookie, he's going to ask for a 84 glass of milk. 85 86 @section{The Consequences of Milk} 87 88 That ``squeak'' was the mouse asking for milk. Let's 89 suppose that you give him some in a big glass. 90 91 He's a small mouse. The glass is too big---way too 92 big. So, he'll probably ask you for a straw. You might as 93 well give it to him. 94 95 @section{Not the Last Straw} 96 97 For now, to handle the milk moustache, it's enough to give 98 him a napkin. But it doesn't end there... oh, no. 99 }| 100 101 Now, after the first paragraph of the paper, we have two 102 sub-sections, each created by calling @racket[section] to 103 generate a sub-section declaration. The first sub-section has 104 two paragraphs. The second section, as initiated by the result 105 of the second @racket[section] call, has a single paragraph. 106 107 Run the @exec{scribble} command(s) from @secref["first-example"] 108 again. You may notice the curly double-quotes in the output, and 109 the @litchar{---} turned into an em dash. 110 111 @;---------------------------------------- 112 @section{Splitting the Document Source} 113 114 As a document grows larger, it's better to split sections into 115 separate source files. The @racket[include-section] operation 116 incorporates a document defined by a @filepath{.scrbl} file into a 117 larger document. 118 119 To split the example document into multiple files, change 120 @filepath{mouse.scrbl} to just 121 122 @samplemod|{ 123 #lang scribble/base 124 125 @title{On the Cookie-Eating Habits of Mice} 126 127 If you give a mouse a cookie, he's going to ask for a 128 glass of milk. 129 130 @include-section["milk.scrbl"] 131 @include-section["straw.scrbl"] 132 }| 133 134 Create @filepath{milk.scrbl} and @filepath{straw.scrbl} in the same 135 directory as @filepath{mouse.scrbl}. In @filepath{milk.scrbl}, put 136 137 @samplemod|{ 138 #lang scribble/base 139 140 @title{The Consequences of Milk} 141 142 That ``squeak'' was the mouse asking for milk... 143 }| 144 145 and in @filepath{straw.scrbl}, put 146 147 @samplemod|{ 148 #lang scribble/base 149 150 @title{Not the Last Straw} 151 152 For now, to handle the milk moustache, ... 153 }| 154 155 Notice that the new files both start with @hash-lang[], like the 156 original document, and the @racket[section]s from the original 157 document become @racket[title]s in the new documents. Both 158 @filepath{milk.scrbl} and @filepath{straw.scrbl} are documents in 159 their own right with their own titles, and they can be individually 160 rendered using @exec{scribble}. Running @exec{scribble} on 161 @filepath{mouse.scrbl}, meanwhile, incorporates the smaller documents 162 into one document that is the same as before. 163 164 @; ---------------------------------------- 165 @section{Document Styles} 166 167 Scribble currently supports only one form of HTML output. You can 168 replace the @filepath{scribble.css} file for the generated pages, and 169 that's about it. (We expect to add more styles in the future.) 170 171 For Latex-based PDF output, Scribble includes support for 172 multiple page-layout configurations. The @filepath{mouse.scrbl} 173 example so far uses the default Latex style. If you plan on submitting 174 the paper to a workshop on programming languages, then---well, you 175 probably need a different topic. But you can start making the current 176 content look right by changing the first line to 177 178 @samplemod|{ 179 #lang scribble/acmart 180 }| 181 182 If you're instead working toward Racket library documentation, 183 try changing the first line to 184 185 @samplemod|{ 186 #lang scribble/manual 187 }| 188 189 which produces output with a separate title page, initial content on 190 that page (intended as a brief orientation to the document), and 191 top-level sections turned into chapters that each start on a new page. 192 If you have split the document into multiple files, the first line of 193 the main document file determines the output format. 194 195 Using @racketmodname[scribble/acmart] or 196 @racketmodname[scribble/manual] does not change the rendered HTML for 197 a document---aside from @racketmodname[scribble/manual] adding a 198 version number---but it changes the set of bindings available in the 199 document body. For example, with @racketmodname[scribble/acmart], the 200 introductory text can be marked as an abstract: 201 202 @samplemod|{ 203 #lang scribble/acmart 204 205 @title{On the Cookie-Eating Habits of Mice} 206 207 @abstract{If you give a mouse a cookie, he's going to 208 ask for a glass of milk.} 209 210 @section{The Consequences of Milk} 211 212 ....}| 213 214 When rendered as HTML, the abstract shows up as an inset paragraph. If 215 you try to use @racket[abstract] with the 216 @racketmodname[scribble/base] or @racketmodname[scribble/manual] 217 language, then you get an error, because @racket[abstract] is not 218 defined. 219 220 When a document is implemented across multiple files, changing the 221 language of the main document can set the style for all of the parts, 222 but it does not introduce bindings into the other part files. For 223 example, if you change the language of @filepath{mouse.scrbl} to 224 @racketmodname[scribble/acmart], then @racket[abstract] becomes 225 available in @filepath{mouse.scrbl} but not in @filepath{milk.scrbl} 226 or @filepath{straw.scrbl}. In other words, operator names are 227 lexically scoped. 228 229 @; ---------------------------------------- 230 @section{More Functions} 231 232 The @racketmodname[scribble/base] language provides a collection of 233 basic operations (both @racketmodname[scribble/acmart] and 234 @racketmodname[scribble/manual] are supersets of 235 @racketmodname[scribble/base]). Many of the operations are style 236 variations that you can apply to text: 237 238 @sample|{ 239 He's a @smaller{small mouse}. The glass is too 240 @larger{big}---@bold{way @larger{too @larger{big}}}. So, he'll 241 @italic{probably} ask you for a straw. 242 }| 243 244 which renders as 245 246 @result{ 247 He's a @smaller{small mouse}. The glass is too 248 @larger{big}---@bold{way @larger{too @larger{big}}}. So, he'll 249 @italic{probably} ask you for a straw. 250 } 251 252 As you would expect, calls to functions like @racket[smaller], 253 @racket[larger], and @racket[bold] can be nested in other calls. They 254 can also be nested within calls to @racket[title] or @racket[section]: 255 256 @sample|{ 257 @section{@italic{Not} the Last Straw} 258 }| 259 260 @sub*section{Centering} 261 262 The @racket[centered] operation centers a flow of text: 263 264 @sample|{ 265 If a mouse eats all your cookies, put up a sign that says 266 @centered{ 267 @bold{Cookies Wanted} 268 269 @italic{Chocolate chip preferred!} 270 } 271 and see if anyone brings you more. 272 }| 273 274 which renders as 275 276 @result{ 277 If a mouse eats all your cookies, put up a sign that says 278 @centered{ 279 @bold{Cookies Wanted} 280 281 @italic{Chocolate chip preferred!} 282 } 283 and see if anyone brings you more. 284 } 285 286 @sub*section{Margin Notes} 287 288 The @racket[margin-note] operation is used in a similar way, but the 289 rendered text is moved to the margins. 290 @margin-note*{If you use @racket[margin-note], then the content shows 291 up over here.} 292 293 @sub*section{Itemizations} 294 295 The @racket[itemlist] operation creates a sequence of bulleted text, 296 where the @racket[item] operation groups text to appear in a single 297 bullet. The @racket[itemlist] operation is different from the others 298 that we have seen before, because it only accepts values produced by 299 @racket[item] instead of arbitrary text. This difference is reflected 300 in the use of @litchar{[}...@litchar{]} for the arguments to 301 @racket[itemlist] instead of @litchar["{"]...@litchar["}"]: 302 303 @sample|{ 304 @centered{@bold{Notice to Mice}} 305 306 @itemlist[@item{We have cookies for you.} 307 @item{If you want to eat a cookie, 308 you must bring your own straw.}] 309 }| 310 311 which renders as 312 313 @result{ 314 @centered{@bold{Notice to Mice}} 315 316 @itemlist[@item{We have cookies for you.} 317 @item{If you want to eat a cookie, 318 you must bring your own straw.}] 319 } 320 321 @sub*section{Tables} 322 323 The @racket[tabular] function takes a list of lists to organize into a 324 two-dimensional table. By default, no spacing is added between columns, 325 so supply a @racket[#:sep] argument to act as a column separator. 326 For example, 327 328 @sample|{ 329 @tabular[#:sep @hspace[1] 330 (list (list @bold{Animal} @bold{Food}) 331 (list "mouse" "cookie") 332 (list "moose" "muffin"))] 333 }| 334 335 renders as 336 337 @result{ 338 @tabular[#:sep @hspace[1] 339 (list (list @bold{Animal} @bold{Food}) 340 (list "mouse" "cookie") 341 (list "moose" "muffin"))] 342 } 343 344 @; ---------------------------------------- 345 @section{Text Mode vs. Racket Mode for Arguments} 346 347 When @litchar{[}...@litchar{]} surrounds the arguments of an 348 operation, the argument expressions are in Racket mode rather than 349 text mode. Even in Racket mode, @litchar["@"] can be used to apply 350 operations; once the @"@" syntax is enabled through a 351 language like @racketmodname[scribble/base] (as opposed to 352 @racketmodname[racket]), it behaves the same in both Racket mode and 353 text mode. 354 355 One advantage of using Racket mode for the arguments to 356 @racket[itemlist] is that we can pass a keyword-tagged optional 357 argument to @racket[itemlist]. In particular, if you want a list with 358 numbers instead of bullets, supply the @racket['ordered] style to 359 @racket[itemlist] using the @racket[#:style] keyword: 360 361 @sample|{ 362 @itemlist[#:style 'ordered 363 @item{Eat cookie.} 364 @item{Drink milk.} 365 @item{Wipe mouth.} 366 @item{...}] 367 }| 368 369 An operation doesn't care whether it's used with 370 @litchar{[}...@litchar{]} or @litchar["{"]...@litchar["}"]. Roughly, 371 @litchar["{"]...@litchar["}"] forms an argument that is a 372 string. (Only roughly, though. Newlines or uses of @litchar["@"] 373 within @litchar["{"]...@litchar["}"] complicate the picture, and we'll 374 get back to that soon.) So, 375 376 @sample|{ 377 @italic{Yummy!} 378 }| 379 380 is equivalent to 381 382 @sample|{ 383 @italic["Yummy!"] 384 }| 385 386 which is equivalent to the Racket expression 387 388 @racketblock[ 389 (italic "Yummy!") 390 ] 391 392 These equivalences explain why Scribble functions are documented in 393 Racket notation. If you're reading this in HTML format, you can click 394 @racket[italic] above to access its documentation. The documentation 395 won't completely make sense, yet, but it will by the end of this 396 chapter. 397 398 What if you want to provide arguments in text mode, but you also want 399 to supply other optional arguments? You can use both 400 @litchar{[}...@litchar{]} and @litchar["{"]...@litchar["}"] for an 401 operation, as long as the @litchar{[}...@litchar{]} is first, and as 402 long as no characters separate the closing @litchar{]} from the 403 opening @litchar["{"]. For example, calling @racket[italic] is the 404 same as using @racket[elem] with the @racket['italic] style: 405 406 @sample|{ 407 @elem[#:style 'italic]{Yummy!} 408 }| 409 410 You can also @emph{omit} both @litchar{[}...@litchar{]} and 411 @litchar["{"]...@litchar["}"]. In that case, the Racket expression 412 after @litchar["@"] is used directly instead of applied as an 413 operation. For example, 414 415 @sample|{ 416 1 plus 2 is @(number->string (+ 1 2)). 417 }| 418 419 renders as 420 421 @result{ 422 1 plus 2 is @(number->string (+ 1 2)). 423 } 424 425 The call to @racket[number->string] is needed because a naked number 426 is not valid as document content. 427 428 @; ---------------------------------------- 429 @section[#:tag "how-to:reader"]{@"@" Syntax Basics} 430 431 The @"@" notation provided by Scribble is just another way of 432 writing Racket expressions. Scribble documents could be constructed 433 using normal Racket notation, without using @"@" at all, but 434 that would be inconvenient for most purposes. The @"@" 435 notation makes dealing with textual content much easier. 436 437 Whether in text mode or Racket mode, @litchar["@"] in a document 438 provides an escape to Racket mode. The basic syntax of @litchar["@"] is 439 440 @racketblock[ 441 @#,BNF-seq[@litchar["@"] 442 @nonterm{cmd} 443 @litchar{[} @kleenestar{@nonterm{datum}} @litchar{]} 444 @litchar["{"] @nonterm{text-body} @litchar["}"]] 445 ] 446 447 where all three parts after @litchar["@"] are optional, but at least 448 one must be present. No spaces are allowed between 449 450 @itemize[ 451 452 @item{@litchar["@"] and @nonterm{cmd}, @litchar{[}, or @litchar["{"]} 453 454 @item{@nonterm{cmd} and @litchar{[} or @litchar["{"]; or} 455 456 @item{@litchar{]} and @litchar["{"].} 457 458 ] 459 460 A @nonterm{cmd} or @nonterm{datum} is normal Racket notation, while a 461 @nonterm{text-body} is itself in text mode. A @nonterm{cmd} obviously 462 must not start with @litchar{[} or @litchar["{"], even though Racket 463 forms could otherwise start with those characters. 464 465 The expansion of just @litchar["@"]@nonterm{cmd} into Racket code is 466 467 @racketblock[ 468 @#,nonterm{cmd} 469 ] 470 471 When either @litchar{[} @litchar{]} or @litchar["{"] @litchar["}"] 472 are used, the expansion is 473 474 @racketblock[ 475 (@#,nonterm{cmd} @#,kleenestar{@nonterm{datum}} @#,kleenestar{@nonterm{parsed-body}}) 476 ] 477 478 where @kleenestar{@nonterm{parsed-body}} is the parse result of the 479 @nonterm{text-body}. The @kleenestar{@nonterm{parsed-body}} part often 480 turns out to be a sequence of Racket strings. 481 482 In practice, the @nonterm{cmd} is normally a Racket identifier that is 483 bound to a procedure or syntactic form. If the procedure or form 484 expects further text to typeset, then @litchar["{"]...@litchar["}"] 485 supplies the text. If the form expects other data, typically 486 @litchar{[}...@litchar{]} is used to surround Racket arguments, 487 instead. Even if an operation's argument is a string, if the string is 488 not used as content text (but instead used as, say, a hyperlink 489 label), then the string is typically provided through 490 @litchar{[}...@litchar{]} instead of @litchar["{"]...@litchar["}"]. 491 Sometimes, both @litchar{[}...@litchar{]} and 492 @litchar["{"]...@litchar["}"] are used, where the former surround 493 Racket arguments that precede text to typeset. Finally, if a form is a 494 purely Racket-level form with no typeset result, such as a 495 @racket[require] to import more operations, then typically just 496 @litchar["@"] is used. 497 498 For example the text-mode stream 499 500 @sample|{ 501 @(require scriblib/figure) 502 503 @section[#:tag "poetry"]{Of Mice and Cookies} 504 See @secref["milk"]. 505 506 @section[#:tag "milk"]{@italic{Important} Milk Supplies} 507 @figure["straw" @elem{A straw}]{@image["straw.png"]} 508 }| 509 510 is equivalent to the Racket-mode sequence 511 512 @racketblock[ 513 (require scriblib/figure) "\n" 514 "\n" 515 (section #:tag "poetry" "Of Mice and Cookies") "\n" 516 "See " (secref "milk") "." "\n" 517 "\n" 518 (section #:tag "milk" (italic "Important") " Milk Supplies") "\n" 519 (figure "straw" (elem "A straw") (image "straw.png")) "\n" 520 ] 521 522 Besides showing how different argument conventions are used for 523 different operations, the above example illustrates how whitespace is 524 preserved in the Racket form of a text-mode stream---including 525 newlines preserved as their own strings. Notice how the second 526 @racket[section] gets two arguments for its content, since the 527 argument content for @racket[section] in the source stream includes 528 both the use of an operator and additional text. When an operation 529 like @racket[section] or @racket[italic] accepts content to typeset, 530 it normally accepts an arbitrary number of arguments that together 531 form the content. 532 533 In addition to its role for command, a @litchar["@"] can be followed 534 by @litchar{;} to start a @index['("Scribble" 535 "comments")]{comment}. If the character after @litchar{;} is 536 @litchar["{"], then the comment runs until a matching @litchar["}"], 537 otherwise the comment runs until the end-of-line: 538 539 @racketblock[ 540 @#,BNF-seq[@litchar["@;{"] @nonterm{comment} @litchar["}"]] 541 @#,BNF-seq[@litchar["@;"] @nonterm{line-comment}] 542 ] 543 544 For more information on the syntax of @litchar["@"], see 545 @secref["reader"]. The full syntax includes a few more details, such 546 as brackets like @litchar["|{"]...@litchar["}|"] for text-mode 547 arguments while disabling @litchar["@"] between the brackets. 548 549 @; ---------------------------------------- 550 @section{Decoding Sequences} 551 552 In a document that starts @racket[#, @hash-lang[] #, 553 @racketmodname[scribble/base]], the top level is a text-mode stream, 554 just like the @nonterm{text-body} in a @litchar["@"] form. As 555 illustrated in the previous section, such a top-level sequence 556 corresponds to a mixture of Racket-mode strings and operation 557 applications. There's an implicit operation, @racket[decode], that 558 wraps the whole document to consume this mixture of strings and other 559 values and turn them into a document description. 560 561 The @racket[decode] operation implements @defterm{flow decoding}, 562 which takes a document stream and breaks it up into sections and 563 paragraphs. Blank lines delimit paragraphs, and the results of 564 operations like @racket[title] and @racket[section] generate ``here's 565 the title'' or ``a new section starts here'' declarations that are 566 recognized by @racket[decode]. 567 568 A different but related @defterm{content decoding} takes place within 569 a paragraph or section title. Content decoding is responsible for 570 converting @litchar{---} to an em dash or for converting @litchar{"} 571 and @litchar{'} to suitable curly quotes. 572 573 The decoding process for document's stream is ultimately determined by 574 the @hash-lang[] line that starts the document. The 575 @racketmodname[scribble/base], @racketmodname[scribble/manual], and 576 @racketmodname[scribble/acmart] languages all use the same 577 @racket[decode] operation. The @racketmodname[scribble/text] language, 578 however, acts more like a plain-text generator and preprocessor, and it 579 does not perform any such decoding rules. (For more on 580 @racketmodname[scribble/text], see @other-doc['(lib 581 "scribblings/scribble/scribble-pp.scrbl")].) 582 583 @margin-note{More precisely, languages like 584 @racketmodname[scribble/base] apply @racket[decode] only after 585 lifting out all definitions and imports from the document 586 stream.} 587 588 When the flow decoder is used, after it breaks the input stream into 589 paragraphs, it applies content decoding to strings within the 590 paragraph. When content is wrapped with an operation, however, content 591 decoding does not apply automatically. An operation is responsible for 592 calling a content or flow decoder as it sees fit. Most operations call 593 the decoder; for example, @racket[italic], @racket[bold], 594 @racket[smaller], etc., all decode their arguments. Similarly, 595 @racket[title] and @racket[section] decode the given content for the 596 title or section name. The @racket[literal] and @racket[verbatim] 597 operators, however, do not decode the given strings. For example, 598 599 @sample|{ 600 @verbatim{---} 601 }| 602 603 renders as 604 605 @result{ 606 @verbatim{---} 607 } 608 609 Don't confuse decoding with the expansion of @"@" 610 notation. The source form 611 612 @sample|{ 613 @verbatim{@(number->string (+ 1 2))} 614 }| 615 616 renders as 617 618 @result{ 619 @verbatim{@(number->string (+ 1 2))} 620 } 621 622 because the source is equivalent to 623 624 @racketblock[ 625 (verbatim (number->string (+ 1 2))) 626 ] 627 628 where @racket[(number->string (+ 1 2))] is evaluated to produce the 629 argument to @racket[verbatim]. The @litchar["|{"]...@litchar["}|"] 630 style of brackets is often used with @racket[verbatim], because 631 @litchar["|{"]...@litchar["}|"] disables @"@" notation for 632 arguments. For example, 633 634 @sample|{ 635 @verbatim|{@(number->string (+ 1 2))}| 636 }| 637 638 renders as 639 640 @result{ 641 @verbatim|{@(number->string (+ 1 2))}| 642 } 643 644 645 @; ---------------------------------------- 646 @section[#:tag "pictures"]{Pictures} 647 648 Any value that is convertable to an image can be used directly within 649 a Scribble document. Functions from the @racketmodname[pict] 650 and @racketmodname[2htdp/image #:indirect] libraries, for example, generate 651 images. For example, 652 653 @sample|{ 654 @(require pict) 655 656 This cookie has lost its chocolate chips: 657 @(colorize (filled-ellipse 40 40) "beige"). 658 }| 659 660 renders as 661 662 @result{ 663 This cookie has lost its chocolate chips: 664 @(colorize (filled-ellipse 40 40) "beige"). 665 } 666 667 668 @; ---------------------------------------- 669 @section[#:tag "roadmap"]{Next Steps} 670 671 If your immediate goal is to document a Racket library or write 672 literate programs, skip to @secref["how-to-doc"], and then go back to 673 @secref["reader"] and other chapters. 674 675 If you are more interested in producing documents unrelated to 676 Racket, continue with @secref["reader"] and then 677 @secref["generic-prose"]. Move on to @secref["internals"] when you 678 need more power. 679 680 If you are interested in text generation and preprocessing, continue 681 with @secref["reader"], but then switch to 682 @other-doc['(lib "scribblings/scribble/scribble-pp.scrbl")].