Hao Zhu | c145088 | 2018-10-03 17:56:26 -0400 | [diff] [blame] | 1 | \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, |
| 27 | pdftitle={Using kableExtra in Bookdown Projects}, |
| 28 | 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 | |
| 120 | \title{Using kableExtra in Bookdown Projects} |
| 121 | \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} |
| 128 | \date{2018-10-03} |
| 129 | |
| 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 | |
| 156 | If you have tried to use \texttt{kableExtra} in a \texttt{bookdown} |
| 157 | project with mutltiple formats, you may have experienced some problems. |
| 158 | For example, you might not be able to render HTML and PDF tables at the |
| 159 | same time using the same piece of code. At the same time, you might |
| 160 | start to wonder if it's possible to create \texttt{bootstrap} style |
| 161 | tables in \texttt{gitbook}. I will try to address these issues using |
| 162 | this example. |
| 163 | |
| 164 | This book is hosted at |
| 165 | \url{https://github.com/haozhu233/kableExtra/tree/master/docs}. |
| 166 | |
| 167 | You can find the source code for this project at |
| 168 | \url{https://github.com/haozhu233/kableExtra/tree/master/docs}. |
| 169 | |
| 170 | \hypertarget{cross-format-tables-in-bookdown}{% |
| 171 | \chapter{Cross-format Tables in |
| 172 | Bookdown}\label{cross-format-tables-in-bookdown}} |
| 173 | |
| 174 | \begin{quote} |
| 175 | Note that if kableExtra 0.9.0 doesn't support the EPUB format. If you |
| 176 | need to output tables in \texttt{.epub}, please upgrade to the dev |
| 177 | version or version 1.0 on CRAN once it's released. |
| 178 | \end{quote} |
| 179 | |
| 180 | \hypertarget{use-the-k-m-approach-instead-of-m-k}{% |
| 181 | \section{Use the ``K-M'' approach instead of |
| 182 | ``M-K''}\label{use-the-k-m-approach-instead-of-m-k}} |
| 183 | |
| 184 | Please read this chapter about the ``K-M''/``M-K'' approaches in |
| 185 | \texttt{bookdown}: |
| 186 | \url{https://bookdown.org/yihui/bookdown/new-session.html} |
| 187 | |
| 188 | To generate cross-format tables with \texttt{kableExtra} in a |
| 189 | multi-format bookdown project, you will have to use the ``M-K'' approach |
| 190 | by setting \texttt{new\_session:\ true} in \texttt{\_bookdown.yml}. |
| 191 | Somehow the ``M-K'' approach, which merges chapters to a big Rmd and |
| 192 | then renders, shares the global environment across formats. For now, I'm |
| 193 | not sure if this is a bug or intended behavior. It might be fixable in |
| 194 | the future but please don't count on that. By setting |
| 195 | \texttt{new\_session:\ true}, we force R to use a new session for every |
| 196 | chapter for different formats. In this way, tables are generated |
| 197 | differently in different formats. |
| 198 | |
| 199 | Note that the ``M-K'' approach is slower than the ``K-M'' approach. At |
| 200 | the same time, packages and data are not shared accross chapter. |
| 201 | |
| 202 | \begin{verbatim} |
| 203 | # Example _bookdown.yml |
| 204 | book_filename: "bookdown_example" |
| 205 | delete_merged_file: true |
| 206 | new_session: true |
| 207 | language: |
| 208 | ui: |
| 209 | chapter_name: "Chapter " |
| 210 | \end{verbatim} |
| 211 | |
| 212 | \hypertarget{prepare-your-tables-for-both-formats}{% |
| 213 | \section{Prepare Your Tables for Both |
| 214 | Formats}\label{prepare-your-tables-for-both-formats}} |
| 215 | |
| 216 | In most cases, functions in \texttt{kable} and \texttt{kableExtra} use |
| 217 | the same API to accomplish the same styling task in HTML and LaTeX. |
| 218 | However, you also need some format specific settings so your tables will |
| 219 | look good in both formats. Some common items here include the |
| 220 | \texttt{booktabs} and \texttt{longtable} settings in \texttt{kable} and |
| 221 | the \texttt{bootstrap\_options} and \texttt{latex\_options} in |
| 222 | \texttt{kable\_styling}. |
| 223 | |
| 224 | Here is an example for a table that will work in both HTML and LaTeX. |
| 225 | |
| 226 | \begin{Shaded} |
| 227 | \begin{Highlighting}[] |
| 228 | \KeywordTok{library}\NormalTok{(kableExtra)} |
| 229 | \NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{] }\OperatorTok{%>%} |
| 230 | \StringTok{ }\KeywordTok{kable}\NormalTok{(}\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}\StringTok{ } |
| 231 | \StringTok{ }\KeywordTok{kable_styling}\NormalTok{(} |
| 232 | \DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{),} |
| 233 | \DataTypeTok{full_width =}\NormalTok{ F} |
| 234 | \NormalTok{ ) }\OperatorTok{%>%} |
| 235 | \StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%} |
| 236 | \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{))} |
| 237 | \end{Highlighting} |
| 238 | \end{Shaded} |
| 239 | |
| 240 | \begin{table}[H] |
| 241 | \centering\rowcolors{2}{gray!6}{white} |
| 242 | |
| 243 | \begin{tabular}{>{\bfseries}lrrrrr} |
| 244 | \hiderowcolors |
| 245 | \toprule |
| 246 | \multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group A} & \multicolumn{3}{c}{Group B} \\ |
| 247 | \cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-6} |
| 248 | & mpg & cyl & disp & hp & drat\\ |
| 249 | \midrule |
| 250 | \showrowcolors |
| 251 | Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90\\ |
| 252 | Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90\\ |
| 253 | Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85\\ |
| 254 | Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08\\ |
| 255 | Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15\\ |
| 256 | \bottomrule |
| 257 | \end{tabular} |
| 258 | \rowcolors{2}{white}{white} |
| 259 | \end{table} |
| 260 | |
| 261 | \hypertarget{use-bootstrap-tables-in-gitbooks-epub}{% |
| 262 | \chapter{Use Bootstrap Tables in gitbooks \& |
| 263 | epub}\label{use-bootstrap-tables-in-gitbooks-epub}} |
| 264 | |
| 265 | \hypertarget{gitbook}{% |
| 266 | \section{Gitbook}\label{gitbook}} |
| 267 | |
| 268 | Most of \texttt{kableExtra} tricks will work in \texttt{bookdown} except |
| 269 | those requires \href{http://getbootstrap.com/}{\texttt{bootstrap}}. By |
| 270 | default, \texttt{rmarkdown} won't load \texttt{bootstrap} for you on |
| 271 | gitbook as it's not necesary. In \texttt{kableExtra}, I used the |
| 272 | \href{https://getbootstrap.com/docs/3.3/customize/}{bootstrap 3.3.7 |
| 273 | customization tool} and made a customized css copy. You can load it by |
| 274 | setting \texttt{options(kableExtra.html.bsTable\ =\ T)}. |
| 275 | |
| 276 | \begin{Shaded} |
| 277 | \begin{Highlighting}[] |
| 278 | \KeywordTok{library}\NormalTok{(kableExtra)} |
| 279 | \KeywordTok{options}\NormalTok{(}\DataTypeTok{kableExtra.html.bsTable =}\NormalTok{ T)} |
| 280 | |
| 281 | \NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{] }\OperatorTok{%>%} |
| 282 | \StringTok{ }\KeywordTok{kable}\NormalTok{(}\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}\StringTok{ } |
| 283 | \StringTok{ }\KeywordTok{kable_styling}\NormalTok{(} |
| 284 | \DataTypeTok{bootstrap_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{,}\StringTok{"hover"}\NormalTok{, }\StringTok{"bordered"}\NormalTok{),} |
| 285 | \DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{),} |
| 286 | \DataTypeTok{full_width =}\NormalTok{ F} |
| 287 | \NormalTok{ ) }\OperatorTok{%>%} |
| 288 | \StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{) }\OperatorTok{%>%} |
| 289 | \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{))} |
| 290 | \end{Highlighting} |
| 291 | \end{Shaded} |
| 292 | |
| 293 | \begin{table}[H] |
| 294 | \centering\rowcolors{2}{gray!6}{white} |
| 295 | |
| 296 | \begin{tabular}{>{\leavevmode\color{red}}lrrrrr} |
| 297 | \hiderowcolors |
| 298 | \toprule |
| 299 | \multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group A} & \multicolumn{3}{c}{Group B} \\ |
| 300 | \cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-6} |
| 301 | & mpg & cyl & disp & hp & drat\\ |
| 302 | \midrule |
| 303 | \showrowcolors |
| 304 | Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90\\ |
| 305 | Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90\\ |
| 306 | Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85\\ |
| 307 | Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08\\ |
| 308 | Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15\\ |
| 309 | \bottomrule |
| 310 | \end{tabular} |
| 311 | \rowcolors{2}{white}{white} |
| 312 | \end{table} |
| 313 | |
| 314 | However, very unfortunately, as we can see, the \texttt{hover} part is |
| 315 | not working and the table still looks different from a regular bootstrap |
| 316 | table. The reason is that gitbook defines its own way to style tables |
| 317 | and some of its rules are conflicting with the one defined in bootstrap. |
| 318 | For css, it's like once you load something, it's impossible to really |
| 319 | unload it. As a result, unless there are some changes in |
| 320 | \texttt{bookdown} (for example, we can replace the gitbook table part |
| 321 | with bootstrap tables), there is nothing we can do here. |
| 322 | |
| 323 | \hypertarget{epub}{% |
| 324 | \section{Epub}\label{epub}} |
| 325 | |
| 326 | Right now, it's impossible to load addition CSS through HTML dependency |
| 327 | (due to a setting in rmarkdown). I will file an issue in |
| 328 | \texttt{rmarkdown} and see if this is something that can be changed. In |
| 329 | the mean time, to use bootstrap tables in Epub, you will have to |
| 330 | manually load |
| 331 | \href{https://github.com/haozhu233/kableExtra/blob/master/inst/bootstrapTable-3.3.7/bootstrapTable.min.css}{this |
| 332 | stylesheet} by putting it to a CSS file (such as ``style.css'') and load |
| 333 | it in \texttt{\_output.yml}. For example, |
| 334 | |
| 335 | \begin{verbatim} |
| 336 | bookdown::epub_book: |
| 337 | stylesheet: style.css |
| 338 | \end{verbatim} |
| 339 | |
| 340 | \bibliography{book.bib,packages.bib} |
| 341 | |
| 342 | |
| 343 | \end{document} |