blob: 61f792265c29d413bb97622d2925b3f72fbb09b1 [file] [log] [blame]
Hao Zhuc1450882018-10-03 17:56:26 -04001\documentclass[]{book}
2\usepackage{lmodern}
3\usepackage{amssymb,amsmath}
4\usepackage{ifxetex,ifluatex}
5\usepackage{fixltx2e} % provides \textsubscript
6\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
7 \usepackage[T1]{fontenc}
8 \usepackage[utf8]{inputenc}
9\else % if luatex or xelatex
10 \ifxetex
11 \usepackage{mathspec}
12 \else
13 \usepackage{fontspec}
14 \fi
15 \defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
16\fi
17% use upquote if available, for straight quotes in verbatim environments
18\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
19% use microtype if available
20\IfFileExists{microtype.sty}{%
21\usepackage{microtype}
22\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
23}{}
24\usepackage[margin=1in]{geometry}
25\usepackage{hyperref}
26\hypersetup{unicode=true,
Hao Zhuc859dba2018-10-03 18:05:55 -040027 pdftitle={Using kableExtra in Bookdown},
Hao Zhuc1450882018-10-03 17:56:26 -040028 pdfauthor={Hao Zhu},
29 pdfborder={0 0 0},
30 breaklinks=true}
31\urlstyle{same} % don't use monospace font for urls
32\usepackage{natbib}
33\bibliographystyle{apalike}
34\usepackage{color}
35\usepackage{fancyvrb}
36\newcommand{\VerbBar}{|}
37\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
38\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
39% Add ',fontsize=\small' for more characters per line
40\usepackage{framed}
41\definecolor{shadecolor}{RGB}{248,248,248}
42\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
43\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
44\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
45\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
46\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
47\newcommand{\BuiltInTok}[1]{#1}
48\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
49\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
50\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
51\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
52\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
53\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
54\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
55\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
56\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
57\newcommand{\ExtensionTok}[1]{#1}
58\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
59\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
60\newcommand{\ImportTok}[1]{#1}
61\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
62\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
63\newcommand{\NormalTok}[1]{#1}
64\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
65\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
66\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
67\newcommand{\RegionMarkerTok}[1]{#1}
68\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
69\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
70\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
71\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
72\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
73\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
74\usepackage{longtable,booktabs}
75\usepackage{graphicx,grffile}
76\makeatletter
77\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
78\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
79\makeatother
80% Scale images if necessary, so that they will not overflow the page
81% margins by default, and it is still possible to overwrite the defaults
82% using explicit options in \includegraphics[width, height, ...]{}
83\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
84\IfFileExists{parskip.sty}{%
85\usepackage{parskip}
86}{% else
87\setlength{\parindent}{0pt}
88\setlength{\parskip}{6pt plus 2pt minus 1pt}
89}
90\setlength{\emergencystretch}{3em} % prevent overfull lines
91\providecommand{\tightlist}{%
92 \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
93\setcounter{secnumdepth}{5}
94% Redefines (sub)paragraphs to behave more like sections
95\ifx\paragraph\undefined\else
96\let\oldparagraph\paragraph
97\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
98\fi
99\ifx\subparagraph\undefined\else
100\let\oldsubparagraph\subparagraph
101\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
102\fi
103
104%%% Use protect on footnotes to avoid problems with footnotes in titles
105\let\rmarkdownfootnote\footnote%
106\def\footnote{\protect\rmarkdownfootnote}
107
108%%% Change title format to be more compact
109\usepackage{titling}
110
111% Create subtitle command for use in maketitle
112\newcommand{\subtitle}[1]{
113 \posttitle{
114 \begin{center}\large#1\end{center}
115 }
116}
117
118\setlength{\droptitle}{-2em}
119
Hao Zhuc859dba2018-10-03 18:05:55 -0400120 \title{Using kableExtra in Bookdown}
Hao Zhuc1450882018-10-03 17:56:26 -0400121 \pretitle{\vspace{\droptitle}\centering\huge}
122 \posttitle{\par}
123 \author{Hao Zhu}
124 \preauthor{\centering\large\emph}
125 \postauthor{\par}
126 \predate{\centering\large\emph}
127 \postdate{\par}
Hao Zhu5250ab52018-10-23 15:25:14 -0400128 \date{2018-10-23}
Hao Zhuc1450882018-10-03 17:56:26 -0400129
130\usepackage{booktabs}
131\usepackage{booktabs}
132\usepackage{longtable}
133\usepackage{array}
134\usepackage{multirow}
135\usepackage[table]{xcolor}
136\usepackage{wrapfig}
137\usepackage{float}
138\usepackage{colortbl}
139\usepackage{pdflscape}
140\usepackage{tabu}
141\usepackage{threeparttable}
142\usepackage{threeparttablex}
143\usepackage[normalem]{ulem}
144\usepackage{makecell}
145
146\begin{document}
147\maketitle
148
149{
150\setcounter{tocdepth}{1}
151\tableofcontents
152}
153\hypertarget{introduction}{%
154\chapter{Introduction}\label{introduction}}
155
156If you have tried to use \texttt{kableExtra} in a \texttt{bookdown}
157project with mutltiple formats, you may have experienced some problems.
158For example, you might not be able to render HTML and PDF tables at the
159same time using the same piece of code. At the same time, you might
160start to wonder if it's possible to create \texttt{bootstrap} style
161tables in \texttt{gitbook}. I will try to address these issues using
162this example.
163
164This book is hosted at
Hao Zhu5250ab52018-10-23 15:25:14 -0400165\url{https://haozhu233.github.io/kableExtra/bookdown/index.html}
Hao Zhuc1450882018-10-03 17:56:26 -0400166
Hao Zhuc859dba2018-10-03 18:05:55 -0400167You can download PDF \& EPUB to see the results of kableExtra in those
168formats using the same code.
169
Hao Zhuc1450882018-10-03 17:56:26 -0400170You can find the source code for this project at
171\url{https://github.com/haozhu233/kableExtra/tree/master/docs}.
172
173\hypertarget{cross-format-tables-in-bookdown}{%
174\chapter{Cross-format Tables in
175Bookdown}\label{cross-format-tables-in-bookdown}}
176
177\begin{quote}
178Note that if kableExtra 0.9.0 doesn't support the EPUB format. If you
179need to output tables in \texttt{.epub}, please upgrade to the dev
180version or version 1.0 on CRAN once it's released.
181\end{quote}
182
183\hypertarget{use-the-k-m-approach-instead-of-m-k}{%
184\section{Use the ``K-M'' approach instead of
185``M-K''}\label{use-the-k-m-approach-instead-of-m-k}}
186
187Please read this chapter about the ``K-M''/``M-K'' approaches in
188\texttt{bookdown}:
189\url{https://bookdown.org/yihui/bookdown/new-session.html}
190
191To generate cross-format tables with \texttt{kableExtra} in a
192multi-format bookdown project, you will have to use the ``M-K'' approach
193by setting \texttt{new\_session:\ true} in \texttt{\_bookdown.yml}.
194Somehow the ``M-K'' approach, which merges chapters to a big Rmd and
195then renders, shares the global environment across formats. For now, I'm
196not sure if this is a bug or intended behavior. It might be fixable in
197the future but please don't count on that. By setting
198\texttt{new\_session:\ true}, we force R to use a new session for every
199chapter for different formats. In this way, tables are generated
200differently in different formats.
201
202Note that the ``M-K'' approach is slower than the ``K-M'' approach. At
203the same time, packages and data are not shared accross chapter.
204
205\begin{verbatim}
206# Example _bookdown.yml
207book_filename: "bookdown_example"
208delete_merged_file: true
209new_session: true
210language:
211 ui:
212 chapter_name: "Chapter "
213\end{verbatim}
214
Hao Zhu5250ab52018-10-23 15:25:14 -0400215\hypertarget{prepare-your-tables-for-all-formats}{%
216\section{Prepare Your Tables for All
217Formats}\label{prepare-your-tables-for-all-formats}}
Hao Zhuc1450882018-10-03 17:56:26 -0400218
219In most cases, functions in \texttt{kable} and \texttt{kableExtra} use
220the same API to accomplish the same styling task in HTML and LaTeX.
221However, you also need some format specific settings so your tables will
222look good in both formats. Some common items here include the
223\texttt{booktabs} and \texttt{longtable} settings in \texttt{kable} and
224the \texttt{bootstrap\_options} and \texttt{latex\_options} in
225\texttt{kable\_styling}.
226
Hao Zhu5250ab52018-10-23 15:25:14 -0400227Here is an example for a table that will work in HTML, LaTeX \& EPUB.
Hao Zhuc1450882018-10-03 17:56:26 -0400228
229\begin{Shaded}
230\begin{Highlighting}[]
231\KeywordTok{library}\NormalTok{(kableExtra)}
Hao Zhu5250ab52018-10-23 15:25:14 -0400232\KeywordTok{library}\NormalTok{(dplyr)}
233\end{Highlighting}
234\end{Shaded}
235
236\begin{verbatim}
237## Warning: package 'dplyr' was built under R version 3.5.1
238\end{verbatim}
239
240\begin{verbatim}
241##
242## Attaching package: 'dplyr'
243\end{verbatim}
244
245\begin{verbatim}
246## The following objects are masked from 'package:stats':
247##
248## filter, lag
249\end{verbatim}
250
251\begin{verbatim}
252## The following objects are masked from 'package:base':
253##
254## intersect, setdiff, setequal, union
255\end{verbatim}
256
257\begin{Shaded}
258\begin{Highlighting}[]
259\KeywordTok{options}\NormalTok{(}\DataTypeTok{kableExtra.html.bsTable =}\NormalTok{ T)}
260\NormalTok{iris[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, ] }\OperatorTok{%>%}
261\StringTok{ }\KeywordTok{mutate_if}\NormalTok{(is.numeric, }\ControlFlowTok{function}\NormalTok{(x) \{}
262 \KeywordTok{cell_spec}\NormalTok{(x, }\DataTypeTok{bold =}\NormalTok{ T, }
263 \DataTypeTok{color =} \KeywordTok{spec_color}\NormalTok{(x, }\DataTypeTok{end =} \FloatTok{0.9}\NormalTok{),}
264 \DataTypeTok{font_size =} \KeywordTok{spec_font_size}\NormalTok{(x))}
265\NormalTok{ \}) }\OperatorTok{%>%}
266\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{Species =} \KeywordTok{cell_spec}\NormalTok{(}
267\NormalTok{ Species, }\DataTypeTok{color =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T,}
268 \DataTypeTok{background =} \KeywordTok{spec_color}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DataTypeTok{end =} \FloatTok{0.9}\NormalTok{, }
269 \DataTypeTok{option =} \StringTok{"A"}\NormalTok{, }\DataTypeTok{direction =} \DecValTok{-1}\NormalTok{)}
270\NormalTok{ )) }\OperatorTok{%>%}
271\StringTok{ }\KeywordTok{kable}\NormalTok{(}\DataTypeTok{escape =}\NormalTok{ F, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
272\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{, }\StringTok{"condensed"}\NormalTok{), }
273 \DataTypeTok{latex_options =} \StringTok{"striped"}\NormalTok{, }
274 \DataTypeTok{full_width =}\NormalTok{ F)}
Hao Zhuc1450882018-10-03 17:56:26 -0400275\end{Highlighting}
276\end{Shaded}
277
278\begin{table}[H]
279\centering\rowcolors{2}{gray!6}{white}
280
Hao Zhu5250ab52018-10-23 15:25:14 -0400281\begin{tabular}{ccccc}
Hao Zhuc1450882018-10-03 17:56:26 -0400282\hiderowcolors
283\toprule
Hao Zhu5250ab52018-10-23 15:25:14 -0400284Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
Hao Zhuc1450882018-10-03 17:56:26 -0400285\midrule
286\showrowcolors
Hao Zhu5250ab52018-10-23 15:25:14 -0400287\bgroup\fontsize{14}{16}\selectfont \textcolor[HTML]{28AE80}{\textbf{5.1}}\egroup{} & \bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{1F9A8A}{\textbf{3.5}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{FECE91}{\textcolor{white}{\textbf{setosa}}}\\
288\bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{4.9}}\egroup{} & \bgroup\fontsize{9}{11}\selectfont \textcolor[HTML]{482274}{\textbf{3}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{FEA06D}{\textcolor{white}{\textbf{setosa}}}\\
289\bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{39578C}{\textbf{4.7}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{38588C}{\textbf{3.2}}\egroup{} & \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{440154}{\textbf{1.3}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{F66E5C}{\textcolor{white}{\textbf{setosa}}}\\
290\bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{433E85}{\textbf{4.6}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{433E85}{\textbf{3.1}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{1.5}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{DE4968}{\textcolor{white}{\textbf{setosa}}}\\
291\bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{1F9A8A}{\textbf{5}}\egroup{} & \bgroup\fontsize{14}{16}\selectfont \textcolor[HTML]{29AF7F}{\textbf{3.6}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{B73779}{\textcolor{white}{\textbf{setosa}}}\\
292\addlinespace
293\bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{BBDF27}{\textbf{5.4}}\egroup{} & \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{BBDF27}{\textbf{3.9}}\egroup{} & \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{BBDF27}{\textbf{1.7}}\egroup{} & \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{BBDF27}{\textbf{0.4}}\egroup{} & \cellcolor[HTML]{8C2981}{\textcolor{white}{\textbf{setosa}}}\\
294\bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{433E85}{\textbf{4.6}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{3.4}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{22A884}{\textbf{0.3}}\egroup{} & \cellcolor[HTML]{641A80}{\textcolor{white}{\textbf{setosa}}}\\
295\bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{1F9A8A}{\textbf{5}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{3.4}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{1.5}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{3C0F70}{\textcolor{white}{\textbf{setosa}}}\\
296\bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{440154}{\textbf{4.4}}\egroup{} & \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{440154}{\textbf{2.9}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{3E4B8A}{\textbf{1.4}}\egroup{} & \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{35608D}{\textbf{0.2}}\egroup{} & \cellcolor[HTML]{140E36}{\textcolor{white}{\textbf{setosa}}}\\
297\bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{4.9}}\egroup{} & \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{433E85}{\textbf{3.1}}\egroup{} & \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{25838E}{\textbf{1.5}}\egroup{} & \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{440154}{\textbf{0.1}}\egroup{} & \cellcolor[HTML]{000004}{\textcolor{white}{\textbf{setosa}}}\\
Hao Zhuc1450882018-10-03 17:56:26 -0400298\bottomrule
299\end{tabular}
300\rowcolors{2}{white}{white}
301\end{table}
302
303\hypertarget{use-bootstrap-tables-in-gitbooks-epub}{%
304\chapter{Use Bootstrap Tables in gitbooks \&
305epub}\label{use-bootstrap-tables-in-gitbooks-epub}}
306
307\hypertarget{gitbook}{%
308\section{Gitbook}\label{gitbook}}
309
310Most of \texttt{kableExtra} tricks will work in \texttt{bookdown} except
Hao Zhu5250ab52018-10-23 15:25:14 -0400311those requiring \href{http://getbootstrap.com/}{\texttt{bootstrap}}. By
Hao Zhuc1450882018-10-03 17:56:26 -0400312default, \texttt{rmarkdown} won't load \texttt{bootstrap} for you on
313gitbook as it's not necesary. In \texttt{kableExtra}, I used the
314\href{https://getbootstrap.com/docs/3.3/customize/}{bootstrap 3.3.7
315customization tool} and made a customized css copy. You can load it by
316setting \texttt{options(kableExtra.html.bsTable\ =\ T)}.
317
318\begin{Shaded}
319\begin{Highlighting}[]
320\KeywordTok{library}\NormalTok{(kableExtra)}
Hao Zhu5250ab52018-10-23 15:25:14 -0400321\KeywordTok{options}\NormalTok{(}\DataTypeTok{kableExtra.html.bsTable =}\NormalTok{ T)}
Hao Zhuc1450882018-10-03 17:56:26 -0400322
323\NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{] }\OperatorTok{%>%}
324\StringTok{ }\KeywordTok{kable}\NormalTok{(}\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}\StringTok{ }
325\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}
Hao Zhuc859dba2018-10-03 18:05:55 -0400326 \DataTypeTok{bootstrap_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{,}\StringTok{"hover"}\NormalTok{, }\StringTok{"bordered"}\NormalTok{, }\StringTok{"condensed"}\NormalTok{),}
Hao Zhu5250ab52018-10-23 15:25:14 -0400327 \DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{)}
Hao Zhuc1450882018-10-03 17:56:26 -0400328\NormalTok{ ) }\OperatorTok{%>%}
329\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{) }\OperatorTok{%>%}
330\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group A"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{, }\StringTok{"Group B"}\NormalTok{ =}\StringTok{ }\DecValTok{3}\NormalTok{))}
331\end{Highlighting}
332\end{Shaded}
333
334\begin{table}[H]
335\centering\rowcolors{2}{gray!6}{white}
336
337\begin{tabular}{>{\leavevmode\color{red}}lrrrrr}
338\hiderowcolors
339\toprule
340\multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group A} & \multicolumn{3}{c}{Group B} \\
341\cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-6}
342 & mpg & cyl & disp & hp & drat\\
343\midrule
344\showrowcolors
345Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90\\
346Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90\\
347Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85\\
348Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08\\
349Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15\\
350\bottomrule
351\end{tabular}
352\rowcolors{2}{white}{white}
353\end{table}
354
Hao Zhu5250ab52018-10-23 15:25:14 -0400355\textbf{One very important note} here is that by default bookdown loads
356the gitbook table css on start up. It has some conflicts with
357\texttt{bootstrap} tables. As a result, some features like
358\texttt{hover} won't be able to work by default. To solve this problem,
359you need to use the latest version of \texttt{bookdown} (version
360\textgreater{}= 0.7.21) and turn off the \texttt{table\_css} option
361under \texttt{bookdown::gitbook} in \texttt{\_output.yml}
362
363\begin{verbatim}
364bookdown::gitbook:
365 table_css: false
366\end{verbatim}
Hao Zhuc1450882018-10-03 17:56:26 -0400367
368\hypertarget{epub}{%
369\section{Epub}\label{epub}}
370
371Right now, it's impossible to load addition CSS through HTML dependency
Hao Zhu5250ab52018-10-23 15:25:14 -0400372and this mechanism exists for a reason (
373\href{https://github.com/rstudio/rmarkdown/issues/1457}{See this issue I
374filed} ). You will have to manually load
Hao Zhuc1450882018-10-03 17:56:26 -0400375\href{https://github.com/haozhu233/kableExtra/blob/master/inst/bootstrapTable-3.3.7/bootstrapTable.min.css}{this
376stylesheet} by putting it to a CSS file (such as ``style.css'') and load
377it in \texttt{\_output.yml}. For example,
378
379\begin{verbatim}
380bookdown::epub_book:
381 stylesheet: style.css
382\end{verbatim}
383
384\bibliography{book.bib,packages.bib}
385
386
387\end{document}