blob: cc2ec9b33aaaa1b02cbc3f0aa8482fbe369b3de0 [file] [log] [blame]
\documentclass[]{book}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % if pdftex
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\else % if luatex or xelatex
\ifxetex
\usepackage{mathspec}
\else
\usepackage{fontspec}
\fi
\defaultfontfeatures{Ligatures=TeX,Scale=MatchLowercase}
\fi
% use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
% use microtype if available
\IfFileExists{microtype.sty}{%
\usepackage{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\usepackage[margin=1in]{geometry}
\usepackage{hyperref}
\hypersetup{unicode=true,
pdftitle={Using kableExtra in Bookdown},
pdfauthor={Hao Zhu},
pdfborder={0 0 0},
breaklinks=true}
\urlstyle{same} % don't use monospace font for urls
\usepackage{natbib}
\bibliographystyle{apalike}
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{248,248,248}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\BuiltInTok}[1]{#1}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
\newcommand{\ExtensionTok}[1]{#1}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\ImportTok}[1]{#1}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
\newcommand{\NormalTok}[1]{#1}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
\newcommand{\RegionMarkerTok}[1]{#1}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
\usepackage{longtable,booktabs}
\usepackage{graphicx,grffile}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
\setcounter{secnumdepth}{5}
% Redefines (sub)paragraphs to behave more like sections
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
%%% Use protect on footnotes to avoid problems with footnotes in titles
\let\rmarkdownfootnote\footnote%
\def\footnote{\protect\rmarkdownfootnote}
%%% Change title format to be more compact
\usepackage{titling}
% Create subtitle command for use in maketitle
\newcommand{\subtitle}[1]{
\posttitle{
\begin{center}\large#1\end{center}
}
}
\setlength{\droptitle}{-2em}
\title{Using kableExtra in Bookdown}
\pretitle{\vspace{\droptitle}\centering\huge}
\posttitle{\par}
\author{Hao Zhu}
\preauthor{\centering\large\emph}
\postauthor{\par}
\predate{\centering\large\emph}
\postdate{\par}
\date{2018-10-03}
\usepackage{booktabs}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
\begin{document}
\maketitle
{
\setcounter{tocdepth}{1}
\tableofcontents
}
\hypertarget{introduction}{%
\chapter{Introduction}\label{introduction}}
If you have tried to use \texttt{kableExtra} in a \texttt{bookdown}
project with mutltiple formats, you may have experienced some problems.
For example, you might not be able to render HTML and PDF tables at the
same time using the same piece of code. At the same time, you might
start to wonder if it's possible to create \texttt{bootstrap} style
tables in \texttt{gitbook}. I will try to address these issues using
this example.
This book is hosted at
\url{https://github.com/haozhu233/kableExtra/tree/master/docs}.
You can download PDF \& EPUB to see the results of kableExtra in those
formats using the same code.
You can find the source code for this project at
\url{https://github.com/haozhu233/kableExtra/tree/master/docs}.
\hypertarget{cross-format-tables-in-bookdown}{%
\chapter{Cross-format Tables in
Bookdown}\label{cross-format-tables-in-bookdown}}
\begin{quote}
Note that if kableExtra 0.9.0 doesn't support the EPUB format. If you
need to output tables in \texttt{.epub}, please upgrade to the dev
version or version 1.0 on CRAN once it's released.
\end{quote}
\hypertarget{use-the-k-m-approach-instead-of-m-k}{%
\section{Use the ``K-M'' approach instead of
``M-K''}\label{use-the-k-m-approach-instead-of-m-k}}
Please read this chapter about the ``K-M''/``M-K'' approaches in
\texttt{bookdown}:
\url{https://bookdown.org/yihui/bookdown/new-session.html}
To generate cross-format tables with \texttt{kableExtra} in a
multi-format bookdown project, you will have to use the ``M-K'' approach
by setting \texttt{new\_session:\ true} in \texttt{\_bookdown.yml}.
Somehow the ``M-K'' approach, which merges chapters to a big Rmd and
then renders, shares the global environment across formats. For now, I'm
not sure if this is a bug or intended behavior. It might be fixable in
the future but please don't count on that. By setting
\texttt{new\_session:\ true}, we force R to use a new session for every
chapter for different formats. In this way, tables are generated
differently in different formats.
Note that the ``M-K'' approach is slower than the ``K-M'' approach. At
the same time, packages and data are not shared accross chapter.
\begin{verbatim}
# Example _bookdown.yml
book_filename: "bookdown_example"
delete_merged_file: true
new_session: true
language:
ui:
chapter_name: "Chapter "
\end{verbatim}
\hypertarget{prepare-your-tables-for-both-formats}{%
\section{Prepare Your Tables for Both
Formats}\label{prepare-your-tables-for-both-formats}}
In most cases, functions in \texttt{kable} and \texttt{kableExtra} use
the same API to accomplish the same styling task in HTML and LaTeX.
However, you also need some format specific settings so your tables will
look good in both formats. Some common items here include the
\texttt{booktabs} and \texttt{longtable} settings in \texttt{kable} and
the \texttt{bootstrap\_options} and \texttt{latex\_options} in
\texttt{kable\_styling}.
Here is an example for a table that will work in both HTML and LaTeX.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(kableExtra)}
\NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{] }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable}\NormalTok{(}\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}
\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{),}
\DataTypeTok{full_width =}\NormalTok{ F}
\NormalTok{ ) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%}
\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{))}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering\rowcolors{2}{gray!6}{white}
\begin{tabular}{>{\bfseries}lrrrrr}
\hiderowcolors
\toprule
\multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group A} & \multicolumn{3}{c}{Group B} \\
\cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-6}
& mpg & cyl & disp & hp & drat\\
\midrule
\showrowcolors
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15\\
\bottomrule
\end{tabular}
\rowcolors{2}{white}{white}
\end{table}
\hypertarget{use-bootstrap-tables-in-gitbooks-epub}{%
\chapter{Use Bootstrap Tables in gitbooks \&
epub}\label{use-bootstrap-tables-in-gitbooks-epub}}
\hypertarget{gitbook}{%
\section{Gitbook}\label{gitbook}}
Most of \texttt{kableExtra} tricks will work in \texttt{bookdown} except
those requires \href{http://getbootstrap.com/}{\texttt{bootstrap}}. By
default, \texttt{rmarkdown} won't load \texttt{bootstrap} for you on
gitbook as it's not necesary. In \texttt{kableExtra}, I used the
\href{https://getbootstrap.com/docs/3.3/customize/}{bootstrap 3.3.7
customization tool} and made a customized css copy. You can load it by
setting \texttt{options(kableExtra.html.bsTable\ =\ T)}.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(kableExtra)}
\KeywordTok{options}\NormalTok{(}\DataTypeTok{kableExtra.html.bsTable =}\NormalTok{ T)}
\NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{] }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{kable}\NormalTok{(}\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}\StringTok{ }
\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}
\DataTypeTok{bootstrap_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{,}\StringTok{"hover"}\NormalTok{, }\StringTok{"bordered"}\NormalTok{, }\StringTok{"condensed"}\NormalTok{),}
\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{),}
\DataTypeTok{full_width =}\NormalTok{ F}
\NormalTok{ ) }\OperatorTok{%>%}
\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{) }\OperatorTok{%>%}
\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{))}
\end{Highlighting}
\end{Shaded}
\begin{table}[H]
\centering\rowcolors{2}{gray!6}{white}
\begin{tabular}{>{\leavevmode\color{red}}lrrrrr}
\hiderowcolors
\toprule
\multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group A} & \multicolumn{3}{c}{Group B} \\
\cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-6}
& mpg & cyl & disp & hp & drat\\
\midrule
\showrowcolors
Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90\\
Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90\\
Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85\\
Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08\\
Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15\\
\bottomrule
\end{tabular}
\rowcolors{2}{white}{white}
\end{table}
However, very unfortunately, as we can see, the \texttt{hover} part is
not working and the table still looks different from a regular bootstrap
table. The reason is that gitbook defines its own way to style tables
and some of its rules are conflicting with the one defined in bootstrap.
For css, it's like once you load something, it's impossible to really
unload it. As a result, unless there are some changes in
\texttt{bookdown} (for example, we can replace the gitbook table part
with bootstrap tables), there is nothing we can do here.
\hypertarget{epub}{%
\section{Epub}\label{epub}}
Right now, it's impossible to load addition CSS through HTML dependency
(due to a setting in rmarkdown). I will file an issue in
\texttt{rmarkdown} and see if this is something that can be changed. In
the mean time, to use bootstrap tables in Epub, you will have to
manually load
\href{https://github.com/haozhu233/kableExtra/blob/master/inst/bootstrapTable-3.3.7/bootstrapTable.min.css}{this
stylesheet} by putting it to a CSS file (such as ``style.css'') and load
it in \texttt{\_output.yml}. For example,
\begin{verbatim}
bookdown::epub_book:
stylesheet: style.css
\end{verbatim}
\bibliography{book.bib,packages.bib}
\end{document}