Skip to content

Commit

Permalink
a minimal bookdown example in Chinese
Browse files Browse the repository at this point in the history
  • Loading branch information
yihui committed Dec 13, 2016
1 parent 358fef9 commit ff948e6
Show file tree
Hide file tree
Showing 18 changed files with 576 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
.Rproj.user
.Rhistory
.RData
.Ruserdata
3 changes: 3 additions & 0 deletions 00-author.Rmd
@@ -0,0 +1,3 @@
# 作者简介 {#author .unnumbered}

上不了厅堂,下得了厨房。敲得了代码,逮得住蟑螂。
19 changes: 19 additions & 0 deletions 01-introduction.Rmd
@@ -0,0 +1,19 @@
\mainmatter

# 牛刀小试 {#intro}

现在我们可以试试 **bookdown** 的一些初级功能了,例如图表。图 \@ref(fig:hello) 是一幅无趣的散点图,表 \@ref(tab:iris) 是一份枯燥的数据。

```{r hello, fig.cap='雷猴啊,散点图!', out.width='90%'}
par(mar = c(4, 4, 1, .1))
plot(cars, pch = 19)
```

```{r iris}
knitr::kable(
head(iris), caption = '雷猴啊,iris 数据!',
booktabs = TRUE
)
```

就这样,你可以一直编下去,直到编不下去。
19 changes: 19 additions & 0 deletions 02-foo.Rmd
@@ -0,0 +1,19 @@
# 白苹风末 {#wind}

瞎扯几句。

## 张老爷子

话说张老爷子写了一首诗:

> 姑苏开遍碧桃时,邂逅河阳女画师。
红豆江南留梦影,白苹风末唱秋词。

## 彭大将领

貌似大家都喜欢用白萍风这个意境。又如彭玉麟的对联:

> 凭栏看云影波光,最好是红蓼花疏、白苹秋老;
把酒对琼楼玉宇,莫孤负天心月到、水面风来。

嘿,玛尼玛尼哄。
7 changes: 7 additions & 0 deletions 07-hmm.Rmd
@@ -0,0 +1,7 @@
\cleardoublepage

# (APPENDIX) 附录 {-}

# 余音绕梁 {#sound}

呐,到这里朕的书差不多写完了,但还有几句话要交待,所以开个附录,再啰嗦几句,各位客官稍安勿躁、扶稳坐好。
9 changes: 9 additions & 0 deletions 08-references.Rmd
@@ -0,0 +1,9 @@
`r if (knitr:::is_html_output()) '# 参考文献 {#references .unnumbered}'`

```{r include=FALSE}
# 自动生成 R 包的参考文献
knitr::write_bib(c(
.packages(), 'bookdown', 'knitr', 'rmarkdown'
), 'packages.bib')
```

3 changes: 3 additions & 0 deletions README.md
@@ -0,0 +1,3 @@
# bookdown 中文范例

这是一个简单的用 **bookdown** 写中文书的例子。在使用它之前,建议您至少泛读一遍 [**bookdown** 官方文档](https://bookdown.org/yihui/bookdown)
9 changes: 9 additions & 0 deletions _bookdown.yml
@@ -0,0 +1,9 @@
book_filename: bookdown
clean: [packages.bib, bookdown.bbl]
language:
label:
fig: ""
tab: ""
ui:
edit: "编辑"
chapter_name: ["第 ", " 章"]
31 changes: 31 additions & 0 deletions _output.yml
@@ -0,0 +1,31 @@
bookdown::gitbook:
css: css/style.css
config:
toc:
collapse: none
before: |
<li><a href="./">中文书示例</a></li>
after: |
<li><a href="https://bookdown.org" target="blank">本书由 bookdown 强力驱动</a></li>
download: [pdf, epub]
edit: https://github.com/yihui/bookdown-chinese/edit/master/%s
sharing:
github: yes
facebook: no
bookdown::pdf_book:
includes:
in_header: latex/preamble.tex
before_body: latex/before_body.tex
after_body: latex/after_body.tex
keep_tex: yes
dev: "cairo_pdf"
latex_engine: xelatex
citation_package: natbib
template: template.tex
pandoc_args: --chapters
toc_depth: 3
toc_unnumbered: no
toc_appendix: yes
quote_footer: ["\\begin{flushright}", "\\end{flushright}"]
bookdown::epub_book:
stylesheet: css/style.css
10 changes: 10 additions & 0 deletions book.bib
@@ -0,0 +1,10 @@
@Book{xie2015,
title = {Dynamic Documents with {R} and knitr},
author = {Yihui Xie},
publisher = {Chapman and Hall/CRC},
address = {Boca Raton, Florida},
year = {2015},
edition = {2nd},
note = {ISBN 978-1498716963},
url = {http://yihui.name/knitr/},
}
18 changes: 18 additions & 0 deletions bookdown-chinese.Rproj
@@ -0,0 +1,18 @@
Version: 1.0

RestoreWorkspace: Default
SaveWorkspace: Default
AlwaysSaveHistory: Default

EnableCodeIndexing: Yes
UseSpacesForTab: Yes
NumSpacesForTab: 2
Encoding: UTF-8

RnwWeave: knitr
LaTeX: pdfLaTeX

AutoAppendNewline: Yes
StripTrailingWhitespace: Yes

BuildType: Website
23 changes: 23 additions & 0 deletions css/style.css
@@ -0,0 +1,23 @@
p.caption {
color: #777;
margin-top: 10px;
}
p code {
white-space: inherit;
}
pre {
word-break: normal;
word-wrap: normal;
}
pre code {
white-space: inherit;
}
p.flushright {
text-align: right;
}
blockquote > p:last-child {
text-align: right;
}
blockquote > p:first-child {
text-align: inherit;
}
48 changes: 48 additions & 0 deletions index.Rmd
@@ -0,0 +1,48 @@
---
title: "麻麻说我们可以用 R Markdown 写书了"
author: "张三"
date: "`r Sys.Date()`"
documentclass: ctexbook
bibliography: [book.bib, packages.bib]
biblio-style: apalike
link-citations: yes
colorlinks: yes
lot: yes
lof: yes
geometry: [b5paper, tmargin=2.5cm, bmargin=2.5cm, lmargin=3.5cm, rmargin=2.5cm]
site: bookdown::bookdown_site
description: "一个简单的中文书示例。"
github-repo: yihui/bookdown-chinese
#cover-image: images/cover.jpg
---

```{r setup, include=FALSE}
options(
htmltools.dir.version = FALSE, formatR.indent = 2, width = 55, digits = 4
)
# 填上你需要用到的包,如 c('ggplot2', 'dplyr')
lapply(c(), function(pkg) {
if (system.file(package = pkg) == '') install.packages(pkg)
})
```

# 前言 {-}

你好,世界。我写了一本书。这本书是这样的,第 \@ref(intro) 章介绍了啥啥,第 \@ref(wind) 章说了啥啥,然后是啥啥……

我用了两个 R 包编译这本书,分别是 **knitr**\index{knitr} [@xie2015]**bookdown**\index{bookdown} [@R-bookdown]。以下是我的 R 进程信息:

```{r}
sessionInfo()
```

## 致谢 {-}

非常感谢谁谁以及谁谁对我的帮助。艾玛,要不是他们神一样的队友,我两年前就写完这本书了。

```{block2, type='flushright', html.tag='p'}
张三
于 A 村某角落
```

2 changes: 2 additions & 0 deletions latex/after_body.tex
@@ -0,0 +1,2 @@
\backmatter
\printindex
11 changes: 11 additions & 0 deletions latex/before_body.tex
@@ -0,0 +1,11 @@

\thispagestyle{empty}

\begin{center}
献给……

呃,爱谁谁吧
\end{center}

\setlength{\abovedisplayskip}{-5pt}
\setlength{\abovedisplayshortskip}{-5pt}
64 changes: 64 additions & 0 deletions latex/preamble.tex
@@ -0,0 +1,64 @@
\usepackage{booktabs}
\usepackage{longtable}

\usepackage{framed,color}
\definecolor{shadecolor}{RGB}{248,248,248}

\renewcommand{\textfraction}{0.05}
\renewcommand{\topfraction}{0.8}
\renewcommand{\bottomfraction}{0.8}
\renewcommand{\floatpagefraction}{0.75}

\let\oldhref\href
\renewcommand{\href}[2]{#2\footnote{\url{#1}}}

\ifxetex
\usepackage{letltxmacro}
\setlength{\XeTeXLinkMargin}{1pt}
\LetLtxMacro\SavedIncludeGraphics\includegraphics
\def\includegraphics#1#{% #1 catches optional stuff (star/opt. arg.)
\IncludeGraphicsAux{#1}%
}%
\newcommand*{\IncludeGraphicsAux}[2]{%
\XeTeXLinkBox{%
\SavedIncludeGraphics#1{#2}%
}%
}%
\fi

\makeatletter
\newenvironment{kframe}{%
\medskip{}
\setlength{\fboxsep}{.8em}
\def\at@end@of@kframe{}%
\ifinner\ifhmode%
\def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
\fi\fi%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
% There is no \\@totalrightmargin, so:
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed%
\at@end@of@kframe}
\makeatother

\renewenvironment{Shaded}{\begin{kframe}}{\end{kframe}}

\usepackage{makeidx}
\makeindex

\urlstyle{tt}

\usepackage{amsthm}
\makeatletter
\def\thm@space@setup{%
\thm@preskip=8pt plus 2pt minus 4pt
\thm@postskip=\thm@preskip
}
\makeatother

\frontmatter
29 changes: 29 additions & 0 deletions packages.bib
@@ -0,0 +1,29 @@
@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/},
}
@Manual{R-bookdown,
title = {bookdown: Authoring Books and Technical Documents with R Markdown},
author = {Yihui Xie},
year = {2016},
note = {R package version 0.3.2},
url = {https://github.com/rstudio/bookdown},
}
@Manual{R-knitr,
title = {knitr: A General-Purpose Package for Dynamic Report Generation in R},
author = {Yihui Xie},
year = {2016},
note = {R package version 1.15.5},
url = {http://yihui.name/knitr/},
}
@Manual{R-rmarkdown,
title = {rmarkdown: Dynamic Documents for R},
author = {JJ Allaire and Joe Cheng and Yihui Xie and Jonathan McPherson and Winston Chang and Jeff Allen and Hadley Wickham and Aron Atkins and Rob Hyndman},
year = {2016},
note = {R package version 1.2.9000},
url = {http://rmarkdown.rstudio.com},
}

0 comments on commit ff948e6

Please sign in to comment.