blob: 2a320f12d20c05a37638ff31987d26193154de03 [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 Zhub8098052018-10-08 16:57:43 -0400128 \date{2018-10-04}
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
165\url{https://github.com/haozhu233/kableExtra/tree/master/docs}.
166
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
215\hypertarget{prepare-your-tables-for-both-formats}{%
216\section{Prepare Your Tables for Both
217Formats}\label{prepare-your-tables-for-both-formats}}
218
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
227Here is an example for a table that will work in both HTML and LaTeX.
228
229\begin{Shaded}
230\begin{Highlighting}[]
231\KeywordTok{library}\NormalTok{(kableExtra)}
232\NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{] }\OperatorTok{%>%}
233\StringTok{ }\KeywordTok{kable}\NormalTok{(}\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}\StringTok{ }
234\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}
235 \DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{),}
236 \DataTypeTok{full_width =}\NormalTok{ F}
237\NormalTok{ ) }\OperatorTok{%>%}
238\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%}
239\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{))}
240\end{Highlighting}
241\end{Shaded}
242
243\begin{table}[H]
244\centering\rowcolors{2}{gray!6}{white}
245
246\begin{tabular}{>{\bfseries}lrrrrr}
247\hiderowcolors
248\toprule
249\multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group A} & \multicolumn{3}{c}{Group B} \\
250\cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-6}
251 & mpg & cyl & disp & hp & drat\\
252\midrule
253\showrowcolors
254Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90\\
255Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90\\
256Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85\\
257Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08\\
258Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15\\
259\bottomrule
260\end{tabular}
261\rowcolors{2}{white}{white}
262\end{table}
263
264\hypertarget{use-bootstrap-tables-in-gitbooks-epub}{%
265\chapter{Use Bootstrap Tables in gitbooks \&
266epub}\label{use-bootstrap-tables-in-gitbooks-epub}}
267
268\hypertarget{gitbook}{%
269\section{Gitbook}\label{gitbook}}
270
271Most of \texttt{kableExtra} tricks will work in \texttt{bookdown} except
272those requires \href{http://getbootstrap.com/}{\texttt{bootstrap}}. By
273default, \texttt{rmarkdown} won't load \texttt{bootstrap} for you on
274gitbook as it's not necesary. In \texttt{kableExtra}, I used the
275\href{https://getbootstrap.com/docs/3.3/customize/}{bootstrap 3.3.7
276customization tool} and made a customized css copy. You can load it by
277setting \texttt{options(kableExtra.html.bsTable\ =\ T)}.
278
279\begin{Shaded}
280\begin{Highlighting}[]
281\KeywordTok{library}\NormalTok{(kableExtra)}
Hao Zhuc1450882018-10-03 17:56:26 -0400282
283\NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{] }\OperatorTok{%>%}
284\StringTok{ }\KeywordTok{kable}\NormalTok{(}\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}\StringTok{ }
285\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}
Hao Zhuc859dba2018-10-03 18:05:55 -0400286 \DataTypeTok{bootstrap_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{,}\StringTok{"hover"}\NormalTok{, }\StringTok{"bordered"}\NormalTok{, }\StringTok{"condensed"}\NormalTok{),}
Hao Zhuc1450882018-10-03 17:56:26 -0400287 \DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{),}
288 \DataTypeTok{full_width =}\NormalTok{ F}
289\NormalTok{ ) }\OperatorTok{%>%}
290\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{) }\OperatorTok{%>%}
291\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{))}
292\end{Highlighting}
293\end{Shaded}
294
295\begin{table}[H]
296\centering\rowcolors{2}{gray!6}{white}
297
298\begin{tabular}{>{\leavevmode\color{red}}lrrrrr}
299\hiderowcolors
300\toprule
301\multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group A} & \multicolumn{3}{c}{Group B} \\
302\cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-6}
303 & mpg & cyl & disp & hp & drat\\
304\midrule
305\showrowcolors
306Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90\\
307Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90\\
308Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85\\
309Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08\\
310Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15\\
311\bottomrule
312\end{tabular}
313\rowcolors{2}{white}{white}
314\end{table}
315
316However, very unfortunately, as we can see, the \texttt{hover} part is
317not working and the table still looks different from a regular bootstrap
318table. The reason is that gitbook defines its own way to style tables
319and some of its rules are conflicting with the one defined in bootstrap.
320For css, it's like once you load something, it's impossible to really
321unload it. As a result, unless there are some changes in
322\texttt{bookdown} (for example, we can replace the gitbook table part
323with bootstrap tables), there is nothing we can do here.
324
325\hypertarget{epub}{%
326\section{Epub}\label{epub}}
327
328Right now, it's impossible to load addition CSS through HTML dependency
329(due to a setting in rmarkdown). I will file an issue in
330\texttt{rmarkdown} and see if this is something that can be changed. In
331the mean time, to use bootstrap tables in Epub, you will have to
332manually load
333\href{https://github.com/haozhu233/kableExtra/blob/master/inst/bootstrapTable-3.3.7/bootstrapTable.min.css}{this
334stylesheet} by putting it to a CSS file (such as ``style.css'') and load
335it in \texttt{\_output.yml}. For example,
336
337\begin{verbatim}
338bookdown::epub_book:
339 stylesheet: style.css
340\end{verbatim}
341
342\bibliography{book.bib,packages.bib}
343
344
345\end{document}