美文网首页
[BOOKDOWN first] basic

[BOOKDOWN first] basic

作者: JamesMori | 来源:发表于2023-09-15 21:35 被阅读0次

    1. Inline formating

    text
    text
    H2SO4
    Fe2+
    code
    text
    [图片上传失败...(image-2ce3d5-1694737622178)]
    ^[This is a footnote.]

    2. block formating

    First-level header

    Second-level header

    Third-level header

    If you do not want a certain heading to be numbered, you can add {-} after the heading

    Preface {-}

    Unordered list items start with *, -, or +, and you can nest one list within another list by indenting the sub-list by four spaces

    • one item
    • one item
    • one item
      • one item
      • one item

    Ordered list items start with numbers

    1. the first item
    2. the second item
    3. the third item
    This text is displayed verbatim / preformatted     code form
    

    Or indent by four spaces:

    This text is displayed verbatim / preformatted
    

    f(k) = {n \choose k} p^{k} (1-p)^{n-k}
    f(k) = {n \choose k} p^{k} (1-p)^{n-k}

    3. special environments

    number and reference

    equation

    To number and refer to equations, put them in the equation environments and assign labels to them using the syntax (#eq:label)

    \begin{equation} 
      f\left(k\right) = \binom{n}{k} p^k\left(1-p\right)^{n-k}
      (\#eq:binom)
    \end{equation} 
    

    You may refer to it using @ref(eq:binom)

    All labels in bookdown must only contain alphanumeric characters, :, -, and/or /.

    For HTML output, bookdown can only number the equations with labels. Please make sure equations without labels are not numbered by either using the equation* environment or adding \nonumber or \notag to your equations.

    theorem and proof

    a “theorem” is just a numbered/labeled environment, and it does not have to be a mathematical theorem (e.g., it can be an example irrelevant to mathematics). Similarly, a “proof” is an unnumbered environment. In this section, we always use the general meanings of a “theorem” and “proof” unless explicitly stated.

    ::: {.theorem}
    This is a `theorem` environment that can contain **any**
    _Markdown_ syntax.
    :::
    

    If you want to refer to a theorem, you should label it. The label can be provided as an ID to the block of the form #label. After you label a theorem, you can refer to it using the syntax @ref(prefix:label)

    ::: {.theorem #pyth name="Pythagorean theorem"}
    For a right triangle, if $c$ denotes the length of the hypotenuse
    and $a$ and $b$ denote the lengths of the other two sides, we have
    
    $$a^2 + b^2 = c^2$$
    :::
    

    Theorem 2.1 (Pythagorean theorem) For a right triangle, if c denotes the length of the hypotenuse and a and b denote the lengths of the other two sides, we have

    a2+b2=c2

    theorem与proof
    theorem, lemma, corollary, proposition, conjecture, definition, example, exercise, hypothesis
    proof, remark, solution

    Theorems are numbered by chapters by default. If there are no chapters in your document, they are numbered by sections instead. If the whole document is unnumbered (the output format option number_sections = FALSE), all theorems are numbered sequentially from 1, 2, …, N.

    LaTeX supports numbering one theorem environment after another, e.g., let theorems and lemmas share the same counter. This is not supported for HTML/EPUB output in bookdown.

    You can change the numbering scheme in the LaTeX preamble by defining your own theorem environments.
    When bookdown detects \newtheorem{theorem} in your LaTeX preamble, it will not write out its default theorem definitions, which means you have to define all theorem environments by yourself. For the sake of simplicity and consistency, we do not recommend that you do this.

    \newtheorem{theorem}{Theorem}
    \newtheorem{lemma}[theorem]{Lemma}
    

    4. special first-level headers

    (PART)

    You can use (PART\*) (the backslash before * is required) instead of (PART) if a part title should not be numbered. Or {-}

    # (PART) Part I {-} 
    # Chapter One
    # Chapter Two
    # (PART\*) Part II {-} 
    # Chapter Three
    

    The second type is an unnumbered header that starts with (APPENDIX), indicating that all chapters after this header are appendices

    # Chapter One 
    # Chapter Two
    # (APPENDIX) Appendix {-} 
    # Appendix A
    # Appendix B
    

    5. text reference

    You can assign some text to a label and reference the text using the label elsewhere in your document.
    It is useful when these captions contain special HTML or LaTeX characters, e.g., if the figure caption contains an underscore, it works in the HTML output but may not work in LaTeX output because the underscore must be escaped in LaTeX.
    The syntax for a text reference is (ref:label) text, where label is a unique label throughout the document for text.
    It must be in a separate paragraph with empty lines above and below it. The paragraph must not be wrapped into multiple lines, and should not end with a white space.

    (ref:foo) A scatterplot of the data `cars` using **base** R graphics. 
    
    {r foo, fig.cap='(ref:foo)'}
    plot(cars)  # a scatterplot
    

    6. R code

    inline
    r R_CODE

    chunk

    {r chunk-label, echo = FALSE, fig.cap = 'A figure caption.'}

    1 + 1
    rnorm(10)  # 10 random numbers
    plot(dist ~ speed, cars)  # a scatterplot
    

    7. Figures

    By default, figures have no captions in the output generated by knitr, which means they will be placed wherever they were generated in the R code.
    There are “floating environments” in LaTeX: elements that cannot be split over multiple pages (like figures) are put in floating environments, so they can float to a page that has enough space to hold them.

    If we assign a figure caption to a code chunk via the chunk option fig.cap, R plots will be put into figure environments, which will be automatically labeled and numbered, and can also be cross-referenced.
    The label of a figure environment is generated from the label of the code chunk, e.g., if the chunk label is foo, the figure label will be fig:foo (the prefix fig: is added before foo). To reference a figure, use the syntax \@ref(label), where label is the figure label, e.g., fig:foo.

    To take advantage of Markdown formatting within the figure caption, you will need to use text references.

    For example, a figure caption that contains italic text will not work when the output format is LaTeX/PDF, since the underscore is a special character in LaTeX, but if you use text references, italic text will be translated to LaTeX code when the output is LaTeX.
    简单点说就是,caption是引用的text reference,就很方便,可以写任何话
    If you want to cross-reference figures or tables generated from a code chunk, please make sure the chunk label only contains alphanumeric characters (a-z, A-Z, 0-9), slashes (/), or dashes (-).
    如果cross reference图表,就要保证图表的caption只有这些内容

    other chunk options for figures
    fig.asp = 0.7, fig.width = 6, and fig.align = 'center', out.width, out.height

    use the chunk option fig.show = 'hold' to hold multiple plots from a code chunk and include them in one environment. You can also place plots side by side if the sum of the width of all plots is smaller than or equal to the current line width. For example, if two plots have the same width 50%, they will be placed side by side.

    Sometimes you may have certain images that are not generated from R code, and you can include them in R Markdown via the function knitr::include_graphics()

    knitr::include_graphics(rep("images/knit-logo.png", 3))
    markdown:这是代码部分,还有文本部分和chunk的参数部分
    

    8. Tables

    knitr::kable() 做表的函数,比较适用markdown
    Like figures, tables with captions will also be numbered and can be referenced. The kable() function will automatically generate a label for a table environment, which is the prefix tab: plus the chunk label.

    knitr::kable(
      head(mtcars[, 1:8], 10), booktabs = TRUE,
      caption = 'A table of the first 10 rows of the mtcars data.'
    )
    

    If you want to put multiple tables in a single table environment, wrap the data objects (usually data frames in R) into a list.

    knitr::kable(
      list(
        head(iris[, 1:2], 3),
        head(mtcars[, 1:3], 5)
      ),
      caption = 'A Tale of Two Tables.', booktabs = TRUE
    )
    

    When you do not want a table to float in PDF, you may use the LaTeX package longtable, which can break a table across multiple pages. To use longtable, pass longtable = TRUE to kable(), and make sure to include \usepackage{longtable} in the LaTeX preamble (see Section 4.1 for how to customize the LaTeX preamble). Of course, this is irrelevant to HTML output, since tables in HTML do not need to float.

    Pandoc supports several types of Markdown tables, such as simple tables, multiline tables, grid tables, and pipe tables.

    To be able to cross-reference a Markdown table, it must have a labeled caption of the form Table: (\#label) Caption here, where label must have the prefix tab:, e.g., tab:simple-table.

    If you decide to use other R packages to generate tables, you have to make sure the label for the table environment appears in the beginning of the table caption in the form (\#label) (again, label must have the prefix tab:).

    You have to be very careful about the portability of the table generating function: it should work for both HTML and LaTeX output automatically, so it must consider the output format internally (check knitr::opts_knit$get('rmarkdown.pandoc.to')). When writing out an HTML table, the caption must be written in the <caption></caption> tag. For simple tables, kable() should suffice. If you have to create complicated tables (e.g., with certain cells spanning across multiple columns/rows), you will have to take the aforementioned issues into consideration.
    要想熟悉运用这个工具,还需要深入了解LATEX HTML的差异

    9. cross-reference

    By default, Pandoc will generate an ID for all section headers, e.g., a section # Hello World will have an ID hello-world. We recommend you to manually assign an ID to a section header to make sure you do not forget to update the reference label after you change the section header.

    To assign an ID to a section header, simply add {#id} to the end of the section header.
    You can also create text-based links using explicit or automatic section IDs or even the actual section header text.

    • If you are happy with the section header as the link text, use it inside a single set of square brackets:
      • [Section header text]
    • There are two ways to specify custom link text:
      • [link text][Section header text]
      • [link text](#ID)
        eg : “Table stuff” via [Table stuff](#tables)

    10. Citations

    Pandoc offers two methods for managing citations and bibliographic references in a document.

    1. The default method is to use a Pandoc helper program called pandoc-citeproc, which follows the specifications of the Citation Style Language (CSL) and obtains specific formatting instructions from one of the huge number of available CSL style files.

    2. Users may also choose to use either natbib (based on bibtex) or biblatex as a “citation package”. In this case, the bibliographic data files need to be in the bibtex or biblatex format, and the document output format is limited to PDF. Again, various bibliographic styles are available (please consult the documentation of these packages).

      To use natbib or biblatex to process references, you can set the citation_package option of the R Markdown output format, e.g.,

      output:
        pdf_document:
          citation_package: natbib
        bookdown::pdf_book:
          citation_package: biblatex
      

    Even if you choose natbib or biblatex for PDF output, all other output formats will be using pandoc-citeproc.

    For any non-PDF output format, pandoc-citeproc is the only available option. If consistency across PDF and non-PDF output formats is important, use pandoc-citeproc throughout.

    The bibliographic data can be in several formats. We have only shown examples of BibTeX databases in this section, and please see the “Citations” section of the Pandoc manual for other possible formats.

    A BibTeX database is a plain-text file (with the conventional filename extension .bib) that consists of bibliography entries like this:

    @Manual{R-base,
      title = {R: A Language and Environment for Statistical
        Computing},
      author = {{R Core Team}},
      organization = {R Foundation for Statistical Computing},
      address = {Vienna, Austria},
      year = {2016},
      url = {https://www.R-project.org/},
    }
    

    Once you have one or multiple .bib files, you may use the field bibliography in the YAML metadata of your first R Markdown document (which is typically index.Rmd), and you can also specify the bibliography style via biblio-style (this only applies to PDF output)
    The field link-citations can be used to add internal links from the citation text of the author-year style to the bibliography entry in the HTML output.

    ---
    bibliography: ["one.bib", "another.bib", "yet-another.bib"]
    biblio-style: "apalike"
    link-citations: true
    ---
    

    When the output format is LaTeX, the list of references will be automatically put in a chapter or section at the end of the document. For non-LaTeX output, you can add an empty chapter as the last chapter of your book. For example, if your last chapter is the Rmd file 06-references.Rmd, its content can be an inline R expression:

    `r if (knitr::is_html_output()) '# References {-}'`
    

    For more detailed instructions and further examples on how to use citations, please see the “Citations” section of the Pandoc manual.

    11. Index

    12. HTML Widgets

    13. web pages and shiny apps

    相关文章

      网友评论

          本文标题:[BOOKDOWN first] basic

          本文链接:https://www.haomeiwen.com/subject/gzjiudtx.html