blob: 80c318a0c00275449c7d2c679406516eb92360ad [file] [log] [blame]
Hao Zhu7039ecf2019-01-06 17:51:21 -05001\documentclass[table]{article}
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={Create Awesome LaTeX Table with knitr::kable and kableExtra},
28 pdfauthor={Hao Zhu},
29 pdfborder={0 0 0},
30 breaklinks=true}
31\urlstyle{same} % don't use monospace font for urls
32\usepackage{color}
33\usepackage{fancyvrb}
34\newcommand{\VerbBar}{|}
35\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
36\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
37% Add ',fontsize=\small' for more characters per line
38\usepackage{framed}
39\definecolor{shadecolor}{RGB}{248,248,248}
40\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
41\newcommand{\AlertTok}[1]{\textcolor[rgb]{0.94,0.16,0.16}{#1}}
42\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
43\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.77,0.63,0.00}{#1}}
44\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
45\newcommand{\BuiltInTok}[1]{#1}
46\newcommand{\CharTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
47\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
48\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
49\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
50\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
51\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{#1}}
52\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
53\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
54\newcommand{\ErrorTok}[1]{\textcolor[rgb]{0.64,0.00,0.00}{\textbf{#1}}}
55\newcommand{\ExtensionTok}[1]{#1}
56\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.00,0.81}{#1}}
57\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
58\newcommand{\ImportTok}[1]{#1}
59\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
60\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.13,0.29,0.53}{\textbf{#1}}}
61\newcommand{\NormalTok}[1]{#1}
62\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.81,0.36,0.00}{\textbf{#1}}}
63\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{#1}}
64\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textit{#1}}}
65\newcommand{\RegionMarkerTok}[1]{#1}
66\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
67\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
68\newcommand{\StringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
69\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.00,0.00,0.00}{#1}}
70\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.31,0.60,0.02}{#1}}
71\newcommand{\WarningTok}[1]{\textcolor[rgb]{0.56,0.35,0.01}{\textbf{\textit{#1}}}}
72\usepackage{graphicx,grffile}
73\makeatletter
74\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
75\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
76\makeatother
77% Scale images if necessary, so that they will not overflow the page
78% margins by default, and it is still possible to overwrite the defaults
79% using explicit options in \includegraphics[width, height, ...]{}
80\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
81\IfFileExists{parskip.sty}{%
82\usepackage{parskip}
83}{% else
84\setlength{\parindent}{0pt}
85\setlength{\parskip}{6pt plus 2pt minus 1pt}
86}
87\setlength{\emergencystretch}{3em} % prevent overfull lines
88\providecommand{\tightlist}{%
89 \setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}
90\setcounter{secnumdepth}{0}
91% Redefines (sub)paragraphs to behave more like sections
92\ifx\paragraph\undefined\else
93\let\oldparagraph\paragraph
94\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
95\fi
96\ifx\subparagraph\undefined\else
97\let\oldsubparagraph\subparagraph
98\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
99\fi
100
101%%% Use protect on footnotes to avoid problems with footnotes in titles
102\let\rmarkdownfootnote\footnote%
103\def\footnote{\protect\rmarkdownfootnote}
104
105%%% Change title format to be more compact
106\usepackage{titling}
107
108% Create subtitle command for use in maketitle
109\newcommand{\subtitle}[1]{
110 \posttitle{
111 \begin{center}\large#1\end{center}
112 }
113}
114
115\setlength{\droptitle}{-2em}
116
117 \title{Create Awesome LaTeX Table with knitr::kable and kableExtra}
118 \pretitle{\vspace{\droptitle}\centering\huge}
119 \posttitle{\par}
120 \author{Hao Zhu}
121 \preauthor{\centering\large\emph}
122 \postauthor{\par}
123 \predate{\centering\large\emph}
124 \postdate{\par}
125 \date{2019-01-06}
126
127\usepackage{booktabs}
128\usepackage{longtable}
129\usepackage{array}
130\usepackage{multirow}
131\usepackage{wrapfig}
132\usepackage{float}
133\usepackage{colortbl}
134\usepackage{pdflscape}
135\usepackage{tabu}
136\usepackage{threeparttable}
137\usepackage{threeparttablex}
138\usepackage[normalem]{ulem}
139\usepackage{makecell}
140
141\begin{document}
142\maketitle
143
144{
145\setcounter{tocdepth}{2}
146\tableofcontents
147}
148\clearpage
149
150\begin{quote}
151Please see the package
152\href{http://haozhu233.github.io/kableExtra}{documentation site} for how
153to use this package in HTML and more.
154\end{quote}
155
156\hypertarget{overview}{%
157\section{Overview}\label{overview}}
158
159\begin{wrapfigure}{r}{0.2\textwidth}\centering
160 \includegraphics{kableExtra_sm.png}
161\end{wrapfigure}
162
163The goal of \texttt{kableExtra} is to help you build common complex
164tables and manipulate table styles. It imports the pipe
165\texttt{\%\textgreater{}\%} symbol from \texttt{magrittr} and verbalizes
166all the functions, so basically you can add ``layers'' to a kable output
167in a way that is similar with \texttt{ggplot2} and \texttt{plotly}.
168
169To learn how to generate complex tables in HTML, please visit
170\url{http://haozhu233.github.io/kableExtra/awesome_table_in_html.html}.
171
172\hypertarget{installation}{%
173\section{Installation}\label{installation}}
174
175\begin{Shaded}
176\begin{Highlighting}[]
177\KeywordTok{install.packages}\NormalTok{(}\StringTok{"kableExtra"}\NormalTok{)}
178
179\CommentTok{# For dev version}
180\CommentTok{# install.packages("devtools")}
181\NormalTok{devtools}\OperatorTok{::}\KeywordTok{install_github}\NormalTok{(}\StringTok{"haozhu233/kableExtra"}\NormalTok{)}
182\end{Highlighting}
183\end{Shaded}
184
185\hypertarget{getting-started}{%
186\section{Getting Started}\label{getting-started}}
187
188Here we are using the first few columns and rows from dataset
189\texttt{mtcars}
190
191\begin{Shaded}
192\begin{Highlighting}[]
193\KeywordTok{library}\NormalTok{(knitr)}
194\KeywordTok{library}\NormalTok{(kableExtra)}
195\NormalTok{dt <-}\StringTok{ }\NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{6}\NormalTok{]}
196\end{Highlighting}
197\end{Shaded}
198
199When you are using \texttt{kable()}, if you don't specify
200\texttt{format}, by default it will generate a markdown table and let
201pandoc handle the conversion from markdown to HTML/PDF. This is the most
202favorable approach to render most simple tables as it is format
203independent. If you switch from HTML to pdf, you basically don't need to
204change anything in your code. However, markdown doesn't support complex
205table. For example, if you want to have a double-row header table,
206markdown just cannot provide you the functionality you need. As a
207result, when you have such a need, you should \textbf{define
208\texttt{format} in \texttt{kable()}} as either ``html'' or ``latex''.
209\emph{You can also define a global option at the beginning using
210\texttt{options(knitr.table.format\ =\ "latex")} so you don't repeat the
211step every time.} \textbf{In this tutorial, I'll still put
212format=``latex'' in the function in case users just want to quickly
213replicate the results. In practice, you don't need to define those
214formats.}
215
216\textbf{Starting from \texttt{kableExtra} 0.9.0}, when you load this
217package (\texttt{library(kableExtra)}),
218\textcolor{red}{\textbf{it will automatically set up the global option 'knitr.table.format' based on your current environment}}.
219Unless you are rendering a PDF, \texttt{kableExtra} will try to render a
220HTML table for you. \textbf{You no longer need to manually set either
221the global option or the \texttt{format} option in each \texttt{kable()}
222function}. I'm still including the explanation above here in this
223vignette so you can understand what is going on behind the scene. Note
224that this is only an global option. You can manually set any format in
225\texttt{kable()} whenever you want. I just hope you can enjoy a peace of
226mind in most of your time.
227
228You can disable this behavior by setting
229\texttt{options(kableExtra.auto\_format\ =\ FALSE)} before you load
230\texttt{kableExtra}.
231
232\begin{Shaded}
233\begin{Highlighting}[]
234\CommentTok{# If you are using kableExtra < 0.9.0, you are recommended to set a global option first.}
235\CommentTok{# options(knitr.table.format = "latex") }
236\CommentTok{## If you don't define format here, you'll need put `format = "latex"` }
237\CommentTok{## in every kable function.}
238\end{Highlighting}
239\end{Shaded}
240
241\hypertarget{latex-packages-used-in-this-package}{%
242\subsection{LaTeX packages used in this
243package}\label{latex-packages-used-in-this-package}}
244
245If you are using a recent version of rmarkdown, you are recommended to
246load this package entirely via \texttt{library(kableExtra)} or
247\texttt{require(kableExtra)} because this package will load all
248necessary LaTeX packages, such as \texttt{booktabs} or
249\texttt{multirow}, for you automatically. Note that, if you are calling
250functions from \texttt{kableExtra} via
251\texttt{kableExtra::kable\_styling()} or if you put
252\texttt{library(kableExtra)} in a separate R file that is
253\textbf{sourced} by the rmarkdown document, these packages won't be
254loaded. Furthermore, you can suppress this auto-loading behavior by
255setting a global option \texttt{kableExtra.latex.load\_packages} to be
256\texttt{FALSE} before you load \texttt{kableExtra}.
257
258\begin{Shaded}
259\begin{Highlighting}[]
260\CommentTok{# Not evaluated. Ilustration purpose}
261\KeywordTok{options}\NormalTok{(}\DataTypeTok{kableExtra.latex.load_packages =} \OtherTok{FALSE}\NormalTok{)}
262\KeywordTok{library}\NormalTok{(kableExtra)}
263\end{Highlighting}
264\end{Shaded}
265
266If you are using R Sweave, beamer, R package vignette template, tufte or
267some customized rmarkdown templates, you can put the following meta data
268into the \texttt{yaml} section. If you are familar with LaTeX and you
269know what you are doing, feel free to remove unnecessary packages from
270the list.
271
272\begin{verbatim}
273header-includes:
274 - \usepackage{booktabs}
275 - \usepackage{longtable}
276 - \usepackage{array}
277 - \usepackage{multirow}
278 - \usepackage{wrapfig}
279 - \usepackage{float}
280 - \usepackage{colortbl}
281 - \usepackage{pdflscape}
282 - \usepackage{tabu}
283 - \usepackage{threeparttable}
284 - \usepackage{threeparttablex}
285 - \usepackage[normalem]{ulem}
286 - \usepackage{makecell}
287\end{verbatim}
288
289Note: \texttt{kableExtra} was using \texttt{xcolor} for alternative row
290color before 1.0. However, the recent updates in \texttt{fancyvbr}
291causes a clash in \texttt{xcolor} option. Therefore, we removed the
292\texttt{xcolor} dependency in version 1.0 and started to rely on
293\texttt{colortbl} completely. If you experience any issues, please
294report on github.
295
296\hypertarget{plain-latex}{%
297\subsection{Plain LaTeX}\label{plain-latex}}
298
299Plain LaTeX table looks relatively ugly in 2017.
300
301\begin{Shaded}
302\begin{Highlighting}[]
303\CommentTok{# Again, with kableExtra >= 0.9.0, `format = "latex"` is automatically defined}
304\CommentTok{# when this package gets loaded. Otherwise, you still need to define formats}
305\KeywordTok{kable}\NormalTok{(dt)}
306\end{Highlighting}
307\end{Shaded}
308
309\begin{tabular}{l|r|r|r|r|r|r}
310\hline
311 & mpg & cyl & disp & hp & drat & wt\\
312\hline
313Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
314\hline
315Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
316\hline
317Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
318\hline
319Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
320\hline
321Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
322\hline
323\end{tabular}
324
325\begin{Shaded}
326\begin{Highlighting}[]
327\CommentTok{# Same: kable(dt, "latex")}
328\end{Highlighting}
329\end{Shaded}
330
331\hypertarget{latex-table-with-booktabs}{%
332\subsection{LaTeX table with booktabs}\label{latex-table-with-booktabs}}
333
334Similar to Bootstrap in HTML, in LaTeX, you can also use a trick to make
335your table look prettier as well. The different part is that, this time
336you don't need to pipe kable outputs to another function. Instead, you
337should call \texttt{booktabs\ =\ T} directly in \texttt{kable()}.
338
339\begin{Shaded}
340\begin{Highlighting}[]
341\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T)}
342\end{Highlighting}
343\end{Shaded}
344
345\begin{tabular}{lrrrrrr}
346\toprule
347 & mpg & cyl & disp & hp & drat & wt\\
348\midrule
349Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
350Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
351Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
352Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
353Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
354\bottomrule
355\end{tabular}
356
357\hypertarget{table-styles}{%
358\section{Table Styles}\label{table-styles}}
359
360\texttt{kable\_styling} in LaTeX uses the same syntax and structure as
361\texttt{kable\_styling} in HTML. However, instead of
362\texttt{bootstrap\_options}, you should specify \texttt{latex\_options}
363instead.
364
365\hypertarget{latex-options}{%
366\subsection{LaTeX options}\label{latex-options}}
367
368Similar with \texttt{bootstap\_options}, \texttt{latex\_options} is also
369a charter vector with a bunch of options including \texttt{striped},
370\texttt{hold\_position} and \texttt{scale\_down}.
371
372\hypertarget{striped}{%
373\subsubsection{Striped}\label{striped}}
374
375Even though in the LaTeX world, people usually call it
376\texttt{alternative\ row\ colors} but here I'm using its bootstrap name
377for consistency. Note that to make it happen, LaTeX package
378\texttt{xcolor} is required to be loaded. In an environment like
379rmarkdown::pdf\_document (rmarkdown 1.4.0 +), \texttt{kable\_styling}
380will load it automatically if \texttt{striped} is enabled. However, in
381other cases, you probably need to import that package by yourself.
382
383\begin{Shaded}
384\begin{Highlighting}[]
385\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
386\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \StringTok{"striped"}\NormalTok{)}
387\end{Highlighting}
388\end{Shaded}
389
390\begin{table}[H]
391\centering\rowcolors{2}{gray!6}{white}
392
393\begin{tabular}{lrrrrrr}
394\hiderowcolors
395\toprule
396 & mpg & cyl & disp & hp & drat & wt\\
397\midrule
398\showrowcolors
399Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
400Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
401Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
402Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
403Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
404\bottomrule
405\end{tabular}
406\rowcolors{2}{white}{white}
407\end{table}
408
409\hypertarget{hold-position}{%
410\subsubsection{Hold position}\label{hold-position}}
411
412If you provide a table caption in \texttt{kable()}, it will put your
413LaTeX tabular in a \texttt{table} environment, unless you are using
414\texttt{longtable}. A \texttt{table} environment will automatically find
415the best place (it thinks) to put your table. However, in many cases,
416you do want your table to appear in a position you want it to be. In
417this case, you can use this \texttt{hold\_position} options here.
418
419\begin{Shaded}
420\begin{Highlighting}[]
421\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{caption =} \StringTok{"Demo table"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
422\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{, }\StringTok{"hold_position"}\NormalTok{))}
423\end{Highlighting}
424\end{Shaded}
425
426\rowcolors{2}{gray!6}{white}
427\begin{table}[!h]
428
429\caption{\label{tab:unnamed-chunk-8}Demo table}
430\centering
431\begin{tabular}{lrrrrrr}
432\hiderowcolors
433\toprule
434 & mpg & cyl & disp & hp & drat & wt\\
435\midrule
436\showrowcolors
437Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
438Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
439Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
440Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
441Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
442\bottomrule
443\end{tabular}
444\end{table}
445\rowcolors{2}{white}{white}
446
447If you find \texttt{hold\_position} is not powerful enough to literally
448PIN your table in the exact position, you may want to use
449\texttt{HOLD\_position}, which is a more powerful version of this
450feature. For those who are familiar with LaTeX, \texttt{hold\_position}
451uses \texttt{{[}!h{]}} and \texttt{HOLD\_position} uses \texttt{{[}H{]}}
452and the \texttt{float} package.
453
454\hypertarget{scale-down}{%
455\subsubsection{Scale down}\label{scale-down}}
456
457When you have a wide table that will normally go out of the page, and
458you want to scale down the table to fit the page, you can use the
459\texttt{scale\_down} option here. Note that, if your table is too small,
460it will also scale up your table. It was named in this way only because
461scaling up isn't very useful in most cases.
462
463\begin{Shaded}
464\begin{Highlighting}[]
465\KeywordTok{kable}\NormalTok{(}\KeywordTok{cbind}\NormalTok{(dt, dt, dt), }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
466\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{, }\StringTok{"scale_down"}\NormalTok{))}
467\end{Highlighting}
468\end{Shaded}
469
470\begin{table}[H]
471\centering\rowcolors{2}{gray!6}{white}
472
473\resizebox{\linewidth}{!}{
474\begin{tabular}{lrrrrrrrrrrrrrrrrrr}
475\hiderowcolors
476\toprule
477 & mpg & cyl & disp & hp & drat & wt & mpg & cyl & disp & hp & drat & wt & mpg & cyl & disp & hp & drat & wt\\
478\midrule
479\showrowcolors
480Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
481Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 21.0 & 6 & 160 & 110 & 3.90 & 2.875 & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
482Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
483Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 21.4 & 6 & 258 & 110 & 3.08 & 3.215 & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
484Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 18.7 & 8 & 360 & 175 & 3.15 & 3.440 & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
485\bottomrule
486\end{tabular}}
487\rowcolors{2}{white}{white}
488\end{table}
489
490\begin{Shaded}
491\begin{Highlighting}[]
492\KeywordTok{kable}\NormalTok{(}\KeywordTok{cbind}\NormalTok{(dt), }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
493\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"striped"}\NormalTok{, }\StringTok{"scale_down"}\NormalTok{))}
494\end{Highlighting}
495\end{Shaded}
496
497\begin{table}[H]
498\centering\rowcolors{2}{gray!6}{white}
499
500\resizebox{\linewidth}{!}{
501\begin{tabular}{lrrrrrr}
502\hiderowcolors
503\toprule
504 & mpg & cyl & disp & hp & drat & wt\\
505\midrule
506\showrowcolors
507Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
508Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
509Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
510Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
511Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
512\bottomrule
513\end{tabular}}
514\rowcolors{2}{white}{white}
515\end{table}
516
517\hypertarget{repeat-header-in-longtable}{%
518\subsubsection{Repeat header in
519longtable}\label{repeat-header-in-longtable}}
520
521In \texttt{kableExtra} 0.3.0 or above, a new option
522\texttt{repeat\_header} was introduced into \texttt{kable\_styling}. It
523will add header rows to longtables spanning multiple pages. For table
524captions on following pages, it will append \emph{``continued''} to the
525caption to differentiate. If you need texts other than
526\emph{``(continued)''} (for example, other languages), you can specify
527it using \texttt{kable\_styling(...,\ repeat\_header\_text\ =\ "xxx")}.
528If you want to completely replace the table caption instead of
529appending, you can specify it in the option
530\texttt{repeat\_header\_method}.
531
532\begin{Shaded}
533\begin{Highlighting}[]
534\NormalTok{long_dt <-}\StringTok{ }\KeywordTok{rbind}\NormalTok{(mtcars, mtcars) }
535
536\KeywordTok{kable}\NormalTok{(long_dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{longtable =}\NormalTok{ T, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{caption =} \StringTok{"Longtable"}\NormalTok{) }\OperatorTok{%>%}
537\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 1"}\NormalTok{ =}\StringTok{ }\DecValTok{5}\NormalTok{, }\StringTok{"Group 2"}\NormalTok{ =}\StringTok{ }\DecValTok{6}\NormalTok{)) }\OperatorTok{%>%}
538\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"repeat_header"}\NormalTok{))}
539\end{Highlighting}
540\end{Shaded}
541
542\begin{longtable}{lrrrrrrrrrrr}
543\caption{\label{tab:unnamed-chunk-11}Longtable}\\
544\toprule
545\multicolumn{1}{c}{ } & \multicolumn{5}{c}{Group 1} & \multicolumn{6}{c}{Group 2} \\
546\cmidrule(l{3pt}r{3pt}){2-6} \cmidrule(l{3pt}r{3pt}){7-12}
547 & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
548\midrule
549\endfirsthead
550\caption[]{Longtable \textit{(continued)}}\\
551\toprule
552\multicolumn{1}{c}{ } & \multicolumn{5}{c}{Group 1} & \multicolumn{6}{c}{Group 2} \\
553\cmidrule(l{3pt}r{3pt}){2-6} \cmidrule(l{3pt}r{3pt}){7-12}
554 & mpg & cyl & disp & hp & drat & wt & qsec & vs & am & gear & carb\\
555\midrule
556\endhead
557\
558\endfoot
559\bottomrule
560\endlastfoot
561Mazda RX4 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\
562Mazda RX4 Wag & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\
563Datsun 710 & 22.8 & 4 & 108.0 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1\\
564Hornet 4 Drive & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1\\
565Hornet Sportabout & 18.7 & 8 & 360.0 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2\\
566\addlinespace
567Valiant & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1\\
568Duster 360 & 14.3 & 8 & 360.0 & 245 & 3.21 & 3.570 & 15.84 & 0 & 0 & 3 & 4\\
569Merc 240D & 24.4 & 4 & 146.7 & 62 & 3.69 & 3.190 & 20.00 & 1 & 0 & 4 & 2\\
570Merc 230 & 22.8 & 4 & 140.8 & 95 & 3.92 & 3.150 & 22.90 & 1 & 0 & 4 & 2\\
571Merc 280 & 19.2 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.30 & 1 & 0 & 4 & 4\\
572\addlinespace
573Merc 280C & 17.8 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.90 & 1 & 0 & 4 & 4\\
574Merc 450SE & 16.4 & 8 & 275.8 & 180 & 3.07 & 4.070 & 17.40 & 0 & 0 & 3 & 3\\
575Merc 450SL & 17.3 & 8 & 275.8 & 180 & 3.07 & 3.730 & 17.60 & 0 & 0 & 3 & 3\\
576Merc 450SLC & 15.2 & 8 & 275.8 & 180 & 3.07 & 3.780 & 18.00 & 0 & 0 & 3 & 3\\
577Cadillac Fleetwood & 10.4 & 8 & 472.0 & 205 & 2.93 & 5.250 & 17.98 & 0 & 0 & 3 & 4\\
578\addlinespace
579Lincoln Continental & 10.4 & 8 & 460.0 & 215 & 3.00 & 5.424 & 17.82 & 0 & 0 & 3 & 4\\
580Chrysler Imperial & 14.7 & 8 & 440.0 & 230 & 3.23 & 5.345 & 17.42 & 0 & 0 & 3 & 4\\
581Fiat 128 & 32.4 & 4 & 78.7 & 66 & 4.08 & 2.200 & 19.47 & 1 & 1 & 4 & 1\\
582Honda Civic & 30.4 & 4 & 75.7 & 52 & 4.93 & 1.615 & 18.52 & 1 & 1 & 4 & 2\\
583Toyota Corolla & 33.9 & 4 & 71.1 & 65 & 4.22 & 1.835 & 19.90 & 1 & 1 & 4 & 1\\
584\addlinespace
585Toyota Corona & 21.5 & 4 & 120.1 & 97 & 3.70 & 2.465 & 20.01 & 1 & 0 & 3 & 1\\
586Dodge Challenger & 15.5 & 8 & 318.0 & 150 & 2.76 & 3.520 & 16.87 & 0 & 0 & 3 & 2\\
587AMC Javelin & 15.2 & 8 & 304.0 & 150 & 3.15 & 3.435 & 17.30 & 0 & 0 & 3 & 2\\
588Camaro Z28 & 13.3 & 8 & 350.0 & 245 & 3.73 & 3.840 & 15.41 & 0 & 0 & 3 & 4\\
589Pontiac Firebird & 19.2 & 8 & 400.0 & 175 & 3.08 & 3.845 & 17.05 & 0 & 0 & 3 & 2\\
590\addlinespace
591Fiat X1-9 & 27.3 & 4 & 79.0 & 66 & 4.08 & 1.935 & 18.90 & 1 & 1 & 4 & 1\\
592Porsche 914-2 & 26.0 & 4 & 120.3 & 91 & 4.43 & 2.140 & 16.70 & 0 & 1 & 5 & 2\\
593Lotus Europa & 30.4 & 4 & 95.1 & 113 & 3.77 & 1.513 & 16.90 & 1 & 1 & 5 & 2\\
594Ford Pantera L & 15.8 & 8 & 351.0 & 264 & 4.22 & 3.170 & 14.50 & 0 & 1 & 5 & 4\\
595Ferrari Dino & 19.7 & 6 & 145.0 & 175 & 3.62 & 2.770 & 15.50 & 0 & 1 & 5 & 6\\
596\addlinespace
597Maserati Bora & 15.0 & 8 & 301.0 & 335 & 3.54 & 3.570 & 14.60 & 0 & 1 & 5 & 8\\
598Volvo 142E & 21.4 & 4 & 121.0 & 109 & 4.11 & 2.780 & 18.60 & 1 & 1 & 4 & 2\\
599Mazda RX41 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620 & 16.46 & 0 & 1 & 4 & 4\\
600Mazda RX4 Wag1 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875 & 17.02 & 0 & 1 & 4 & 4\\
601Datsun 7101 & 22.8 & 4 & 108.0 & 93 & 3.85 & 2.320 & 18.61 & 1 & 1 & 4 & 1\\
602\addlinespace
603Hornet 4 Drive1 & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215 & 19.44 & 1 & 0 & 3 & 1\\
604Hornet Sportabout1 & 18.7 & 8 & 360.0 & 175 & 3.15 & 3.440 & 17.02 & 0 & 0 & 3 & 2\\
605Valiant1 & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460 & 20.22 & 1 & 0 & 3 & 1\\
606Duster 3601 & 14.3 & 8 & 360.0 & 245 & 3.21 & 3.570 & 15.84 & 0 & 0 & 3 & 4\\
607Merc 240D1 & 24.4 & 4 & 146.7 & 62 & 3.69 & 3.190 & 20.00 & 1 & 0 & 4 & 2\\
608\addlinespace
609Merc 2301 & 22.8 & 4 & 140.8 & 95 & 3.92 & 3.150 & 22.90 & 1 & 0 & 4 & 2\\
610Merc 2801 & 19.2 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.30 & 1 & 0 & 4 & 4\\
611Merc 280C1 & 17.8 & 6 & 167.6 & 123 & 3.92 & 3.440 & 18.90 & 1 & 0 & 4 & 4\\
612Merc 450SE1 & 16.4 & 8 & 275.8 & 180 & 3.07 & 4.070 & 17.40 & 0 & 0 & 3 & 3\\
613Merc 450SL1 & 17.3 & 8 & 275.8 & 180 & 3.07 & 3.730 & 17.60 & 0 & 0 & 3 & 3\\
614\addlinespace
615Merc 450SLC1 & 15.2 & 8 & 275.8 & 180 & 3.07 & 3.780 & 18.00 & 0 & 0 & 3 & 3\\
616Cadillac Fleetwood1 & 10.4 & 8 & 472.0 & 205 & 2.93 & 5.250 & 17.98 & 0 & 0 & 3 & 4\\
617Lincoln Continental1 & 10.4 & 8 & 460.0 & 215 & 3.00 & 5.424 & 17.82 & 0 & 0 & 3 & 4\\
618Chrysler Imperial1 & 14.7 & 8 & 440.0 & 230 & 3.23 & 5.345 & 17.42 & 0 & 0 & 3 & 4\\
619Fiat 1281 & 32.4 & 4 & 78.7 & 66 & 4.08 & 2.200 & 19.47 & 1 & 1 & 4 & 1\\
620\addlinespace
621Honda Civic1 & 30.4 & 4 & 75.7 & 52 & 4.93 & 1.615 & 18.52 & 1 & 1 & 4 & 2\\
622Toyota Corolla1 & 33.9 & 4 & 71.1 & 65 & 4.22 & 1.835 & 19.90 & 1 & 1 & 4 & 1\\
623Toyota Corona1 & 21.5 & 4 & 120.1 & 97 & 3.70 & 2.465 & 20.01 & 1 & 0 & 3 & 1\\
624Dodge Challenger1 & 15.5 & 8 & 318.0 & 150 & 2.76 & 3.520 & 16.87 & 0 & 0 & 3 & 2\\
625AMC Javelin1 & 15.2 & 8 & 304.0 & 150 & 3.15 & 3.435 & 17.30 & 0 & 0 & 3 & 2\\
626\addlinespace
627Camaro Z281 & 13.3 & 8 & 350.0 & 245 & 3.73 & 3.840 & 15.41 & 0 & 0 & 3 & 4\\
628Pontiac Firebird1 & 19.2 & 8 & 400.0 & 175 & 3.08 & 3.845 & 17.05 & 0 & 0 & 3 & 2\\
629Fiat X1-91 & 27.3 & 4 & 79.0 & 66 & 4.08 & 1.935 & 18.90 & 1 & 1 & 4 & 1\\
630Porsche 914-21 & 26.0 & 4 & 120.3 & 91 & 4.43 & 2.140 & 16.70 & 0 & 1 & 5 & 2\\
631Lotus Europa1 & 30.4 & 4 & 95.1 & 113 & 3.77 & 1.513 & 16.90 & 1 & 1 & 5 & 2\\
632\addlinespace
633Ford Pantera L1 & 15.8 & 8 & 351.0 & 264 & 4.22 & 3.170 & 14.50 & 0 & 1 & 5 & 4\\
634Ferrari Dino1 & 19.7 & 6 & 145.0 & 175 & 3.62 & 2.770 & 15.50 & 0 & 1 & 5 & 6\\
635Maserati Bora1 & 15.0 & 8 & 301.0 & 335 & 3.54 & 3.570 & 14.60 & 0 & 1 & 5 & 8\\
636Volvo 142E1 & 21.4 & 4 & 121.0 & 109 & 4.11 & 2.780 & 18.60 & 1 & 1 & 4 & 2\\*
637\end{longtable}
638
639\hypertarget{full-width}{%
640\subsection{Full width?}\label{full-width}}
641
642If you have a small table and you want it to spread wide on the page,
643you can try the \texttt{full\_width} option. Unlike
644\texttt{scale\_down}, it won't change your font size. You can use
645\texttt{column\_spec}, which will be explained later, together with
646\texttt{full\_width} to achieve the best result.
647
648\begin{Shaded}
649\begin{Highlighting}[]
650\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
651\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{full_width =}\NormalTok{ T) }\OperatorTok{%>%}
652\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{width =} \StringTok{"8cm"}\NormalTok{)}
653\end{Highlighting}
654\end{Shaded}
655
656\begin{tabu} to \linewidth {>{\raggedright\arraybackslash}p{8cm}>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X>{\raggedleft}X}
657\toprule
658 & mpg & cyl & disp & hp & drat & wt\\
659\midrule
660Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
661Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
662Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
663Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
664Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
665\bottomrule
666\end{tabu}
667
668\hypertarget{position}{%
669\subsection{Position}\label{position}}
670
671Table Position only matters when the table doesn't have
672\texttt{full\_width}. You can choose to align the table to
673\texttt{center} or \texttt{left} side of the page. The default value of
674position is \texttt{center}.
675
676Note that even though you can select to \texttt{right} align your table
677but the table will actually be centered. Somehow it is very difficult to
678right align a table in LaTeX (since it's not very useful in the real
679world?). If you know how to do it, please send out an issue or PR and
680let me know.
681
682\begin{Shaded}
683\begin{Highlighting}[]
684\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
685\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{position =} \StringTok{"center"}\NormalTok{)}
686\end{Highlighting}
687\end{Shaded}
688
689\begin{table}[H]
690\centering
691\begin{tabular}{lrrrrrr}
692\toprule
693 & mpg & cyl & disp & hp & drat & wt\\
694\midrule
695Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
696Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
697Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
698Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
699Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
700\bottomrule
701\end{tabular}
702\end{table}
703
704Becides these three common options, you can also wrap text around the
705table using the \texttt{float-left} or \texttt{float-right} options.
706Note that, like \texttt{striped}, this feature will load another
707non-default LaTeX package \texttt{wrapfig} which requires rmarkdown
7081.4.0 +. If you rmarkdown version \textless{} 1.4.0, you need to load
709the package through a customed LaTeX template file.
710
711\begin{Shaded}
712\begin{Highlighting}[]
713\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
714\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{position =} \StringTok{"float_right"}\NormalTok{)}
715\end{Highlighting}
716\end{Shaded}
717
718\begin{wraptable}{r}{0pt}
719\begin{tabular}{lrrrrrr}
720\toprule
721 & mpg & cyl & disp & hp & drat & wt\\
722\midrule
723Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
724Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
725Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
726Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
727Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
728\bottomrule
729\end{tabular}\end{wraptable}
730
731Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras sit amet
732mauris in ex ultricies elementum vel rutrum dolor. Phasellus tempor
733convallis dui, in hendrerit mauris placerat scelerisque. Maecenas a
734accumsan enim, a maximus velit. Pellentesque in risus eget est faucibus
735convallis nec at nulla. Phasellus nec lacinia justo. Morbi fermentum,
736orci id varius accumsan, nibh neque porttitor ipsum, consectetur luctus
737risus arcu ac ex. Aenean a luctus augue. Suspendisse et auctor nisl.
738Suspendisse cursus ultrices quam non vulputate. Phasellus et pharetra
739neque, vel feugiat erat. Sed feugiat elit at mauris commodo consequat.
740Sed congue lectus id mattis hendrerit. Mauris turpis nisl, congue eget
741velit sed, imperdiet convallis magna. Nam accumsan urna risus, non
742feugiat odio vehicula eget.
743
744\hypertarget{font-size}{%
745\subsection{Font Size}\label{font-size}}
746
747If one of your tables is huge and you want to use a smaller font size
748for that specific table, you can use the \texttt{font\_size} option.
749
750\begin{Shaded}
751\begin{Highlighting}[]
752\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
753\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{font_size =} \DecValTok{7}\NormalTok{)}
754\end{Highlighting}
755\end{Shaded}
756
757\begin{table}[H]
758\centering\begingroup\fontsize{7}{9}\selectfont
759
760\begin{tabular}{lrrrrrr}
761\toprule
762 & mpg & cyl & disp & hp & drat & wt\\
763\midrule
764Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
765Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
766Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
767Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
768Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
769\bottomrule
770\end{tabular}
771\endgroup{}
772\end{table}
773
774\hypertarget{column-row-specification}{%
775\section{Column / Row Specification}\label{column-row-specification}}
776
777\hypertarget{column-spec}{%
778\subsection{Column spec}\label{column-spec}}
779
780When you have a table with lots of explanatory texts, you may want to
781specify the column width for different column, since the auto adjust in
782HTML may not work in its best way while basic LaTeX table is really bad
783at handling text wrapping. Also, sometimes, you may want to highlight a
784column (e.g., a ``Total'' column) by making it bold. In these scenarios,
785you can use \texttt{column\_spec()}. You can find an example below.
786
787\begin{Shaded}
788\begin{Highlighting}[]
789\NormalTok{text_tbl <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}
790 \DataTypeTok{Items =} \KeywordTok{c}\NormalTok{(}\StringTok{"Item 1"}\NormalTok{, }\StringTok{"Item 2"}\NormalTok{, }\StringTok{"Item 3"}\NormalTok{),}
791 \DataTypeTok{Features =} \KeywordTok{c}\NormalTok{(}
792 \StringTok{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a. "}\NormalTok{,}
793 \StringTok{"In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in. "}\NormalTok{, }
794 \StringTok{"Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis. "}
795\NormalTok{ )}
796\NormalTok{)}
797
798\KeywordTok{kable}\NormalTok{(text_tbl, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
799\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{full_width =}\NormalTok{ F) }\OperatorTok{%>%}
800\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{color =} \StringTok{"red"}\NormalTok{) }\OperatorTok{%>%}
801\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{2}\NormalTok{, }\DataTypeTok{width =} \StringTok{"30em"}\NormalTok{)}
802\end{Highlighting}
803\end{Shaded}
804
805\begin{table}[H]
806\centering
807\begin{tabular}{>{\bfseries\leavevmode\color{red}}l>{\raggedright\arraybackslash}p{30em}}
808\toprule
809Items & Features\\
810\midrule
811Item 1 & Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin vehicula tempor ex. Morbi malesuada sagittis turpis, at venenatis nisl luctus a.\\
812Item 2 & In eu urna at magna luctus rhoncus quis in nisl. Fusce in velit varius, posuere risus et, cursus augue. Duis eleifend aliquam ante, a aliquet ex tincidunt in.\\
813Item 3 & Vivamus venenatis egestas eros ut tempus. Vivamus id est nisi. Aliquam molestie erat et sollicitudin venenatis. In ac lacus at velit scelerisque mattis.\\
814\bottomrule
815\end{tabular}
816\end{table}
817
818\hypertarget{row-spec}{%
819\subsection{Row spec}\label{row-spec}}
820
821Similar with \texttt{column\_spec}, you can define specifications for
822rows. Currently, you can either bold or italicize an entire row. Note
823that, similar to other row-related functions in \texttt{kableExtra}, for
824the position of the target row, you don't need to count in header rows
825or the group labeling rows.
826
827\begin{Shaded}
828\begin{Highlighting}[]
829\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
830\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\StringTok{"striped"}\NormalTok{, }\DataTypeTok{full_width =}\NormalTok{ F) }\OperatorTok{%>%}
831\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{7}\NormalTok{, }\DataTypeTok{border_left =}\NormalTok{ T, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%}
832\StringTok{ }\KeywordTok{row_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{strikeout =}\NormalTok{ T) }\OperatorTok{%>%}
833\StringTok{ }\KeywordTok{row_spec}\NormalTok{(}\DecValTok{3}\OperatorTok{:}\DecValTok{5}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{color =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{background =} \StringTok{"black"}\NormalTok{)}
834\end{Highlighting}
835\end{Shaded}
836
837\begin{table}[H]
838\centering
839\begin{tabular}{lrrrrr|>{\bfseries}r}
840\toprule
841 & mpg & cyl & disp & hp & drat & wt\\
842\midrule
843\sout{Mazda RX4} & \sout{21.0} & \sout{6} & \sout{160} & \sout{110} & \sout{3.90} & \sout{2.620}\\
844Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
845\rowcolor{black} \textcolor{white}{\textbf{Datsun 710}} & \textcolor{white}{\textbf{22.8}} & \textcolor{white}{\textbf{4}} & \textcolor{white}{\textbf{108}} & \textcolor{white}{\textbf{93}} & \textcolor{white}{\textbf{3.85}} & \textcolor{white}{\textbf{2.320}}\\
846\rowcolor{black} \textcolor{white}{\textbf{Hornet 4 Drive}} & \textcolor{white}{\textbf{21.4}} & \textcolor{white}{\textbf{6}} & \textcolor{white}{\textbf{258}} & \textcolor{white}{\textbf{110}} & \textcolor{white}{\textbf{3.08}} & \textcolor{white}{\textbf{3.215}}\\
847\rowcolor{black} \textcolor{white}{\textbf{Hornet Sportabout}} & \textcolor{white}{\textbf{18.7}} & \textcolor{white}{\textbf{8}} & \textcolor{white}{\textbf{360}} & \textcolor{white}{\textbf{175}} & \textcolor{white}{\textbf{3.15}} & \textcolor{white}{\textbf{3.440}}\\
848\bottomrule
849\end{tabular}
850\end{table}
851
852\hypertarget{header-rows}{%
853\subsection{Header Rows}\label{header-rows}}
854
855One special case of \texttt{row\_spec} is that you can specify the
856format of the header row via \texttt{row\_spec(row\ =\ 0,\ ...)}.
857
858\begin{Shaded}
859\begin{Highlighting}[]
860\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{) }\OperatorTok{%>%}
861\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \StringTok{"striped"}\NormalTok{, }\DataTypeTok{full_width =}\NormalTok{ F) }\OperatorTok{%>%}
862\StringTok{ }\KeywordTok{row_spec}\NormalTok{(}\DecValTok{0}\NormalTok{, }\DataTypeTok{angle =} \DecValTok{45}\NormalTok{)}
863\end{Highlighting}
864\end{Shaded}
865
866\begin{table}[H]
867\centering\rowcolors{2}{gray!6}{white}
868
869\begin{tabular}{lcccccc}
870\hiderowcolors
871\toprule
872\rotatebox{45}{ } & \rotatebox{45}{mpg} & \rotatebox{45}{cyl} & \rotatebox{45}{disp} & \rotatebox{45}{hp} & \rotatebox{45}{drat} & \rotatebox{45}{wt}\\
873\midrule
874\showrowcolors
875Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
876Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
877Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
878Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
879Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
880\bottomrule
881\end{tabular}
882\rowcolors{2}{white}{white}
883\end{table}
884
885\hypertarget{celltext-specification}{%
886\section{Cell/Text Specification}\label{celltext-specification}}
887
888Function \texttt{cell\_spec} is introduced in version 0.6.0 of
889\texttt{kableExtra}. Unlike \texttt{column\_spec} and
890\texttt{row\_spec}, \textbf{this function is designed to be used before
891the data.frame gets into the \texttt{kable} function}. Comparing with
892figuring out a list of 2 dimensional indexes for targeted cells, this
893design is way easier to learn and use, and it fits perfectly well with
894\texttt{dplyr}'s \texttt{mutate} and \texttt{summarize} functions. With
895this design, there are two things to be noted: * Since
896\texttt{cell\_spec} generates raw \texttt{HTML} or \texttt{LaTeX} code,
897make sure you remember to put \texttt{escape\ =\ FALSE} in
898\texttt{kable}. At the same time, you have to escape special symbols
899including \texttt{\%} manually by yourself * \texttt{cell\_spec} needs a
900way to know whether you want \texttt{html} or \texttt{latex}. You can
901specify it locally in function or globally via the
902\texttt{options(knitr.table.format\ =\ "latex")} method as suggested at
903the beginning. If you don't provide anything, this function will output
904as HTML by default.
905
906Currently, \texttt{cell\_spec} supports features including bold, italic,
907monospace, text color, background color, align, font size \& rotation
908angle. More features may be added in the future. Please see function
909documentations as reference.
910
911\hypertarget{conditional-logic}{%
912\subsection{Conditional logic}\label{conditional-logic}}
913
914It is very easy to use \texttt{cell\_spec} with conditional logic. Here
915is an example.
916
917\begin{Shaded}
918\begin{Highlighting}[]
919\KeywordTok{library}\NormalTok{(dplyr)}
920\NormalTok{mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{2}\NormalTok{] }\OperatorTok{%>%}
921\StringTok{ }\KeywordTok{mutate}\NormalTok{(}
922 \DataTypeTok{car =} \KeywordTok{row.names}\NormalTok{(.),}
923 \CommentTok{# You don't need format = "latex" if you have ever defined options(knitr.table.format)}
924 \DataTypeTok{mpg =} \KeywordTok{cell_spec}\NormalTok{(mpg, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \KeywordTok{ifelse}\NormalTok{(mpg }\OperatorTok{>}\StringTok{ }\DecValTok{20}\NormalTok{, }\StringTok{"red"}\NormalTok{, }\StringTok{"blue"}\NormalTok{)),}
925 \DataTypeTok{cyl =} \KeywordTok{cell_spec}\NormalTok{(cyl, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{angle =} \DecValTok{45}\NormalTok{, }
926 \DataTypeTok{background =} \KeywordTok{factor}\NormalTok{(cyl, }\KeywordTok{c}\NormalTok{(}\DecValTok{4}\NormalTok{, }\DecValTok{6}\NormalTok{, }\DecValTok{8}\NormalTok{), }
927 \KeywordTok{c}\NormalTok{(}\StringTok{"#666666"}\NormalTok{, }\StringTok{"#999999"}\NormalTok{, }\StringTok{"#BBBBBB"}\NormalTok{)))}
928\NormalTok{ ) }\OperatorTok{%>%}
929\StringTok{ }\KeywordTok{select}\NormalTok{(car, mpg, cyl) }\OperatorTok{%>%}
930\StringTok{ }\KeywordTok{kable}\NormalTok{(}\StringTok{"latex"}\NormalTok{, }\DataTypeTok{escape =}\NormalTok{ F, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{linesep =} \StringTok{""}\NormalTok{)}
931\end{Highlighting}
932\end{Shaded}
933
934\begin{tabular}{lll}
935\toprule
936car & mpg & cyl\\
937\midrule
938Mazda RX4 & \textcolor{red}{21} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
939Mazda RX4 Wag & \textcolor{red}{21} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
940Datsun 710 & \textcolor{red}{22.8} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{666666}{\textcolor{white}{4}}}}\\
941Hornet 4 Drive & \textcolor{red}{21.4} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
942Hornet Sportabout & \textcolor{blue}{18.7} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{BBBBBB}{\textcolor{white}{8}}}}\\
943Valiant & \textcolor{blue}{18.1} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
944Duster 360 & \textcolor{blue}{14.3} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{BBBBBB}{\textcolor{white}{8}}}}\\
945Merc 240D & \textcolor{red}{24.4} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{666666}{\textcolor{white}{4}}}}\\
946Merc 230 & \textcolor{red}{22.8} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{666666}{\textcolor{white}{4}}}}\\
947Merc 280 & \textcolor{blue}{19.2} & \multicolumn{1}{c}{\rotatebox{45}{\cellcolor[HTML]{999999}{\textcolor{white}{6}}}}\\
948\bottomrule
949\end{tabular}
950
951\hypertarget{visualize-data-with-viridis-color}{%
952\subsection{Visualize data with Viridis
953Color}\label{visualize-data-with-viridis-color}}
954
955This package also comes with a few helper functions, including
956\texttt{spec\_color}, \texttt{spec\_font\_size} \& \texttt{spec\_angle}.
957These functions can rescale continuous variables to certain scales. For
958example, function \texttt{spec\_color} would map a continuous variable
959to any \href{https://CRAN.R-project.org/package=viridisLite}{viridis
960color palettes}. It offers a very visually impactful representation in a
961tabular format.
962
963\begin{Shaded}
964\begin{Highlighting}[]
965\NormalTok{iris[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, ] }\OperatorTok{%>%}
966\StringTok{ }\KeywordTok{mutate_if}\NormalTok{(is.numeric, }\ControlFlowTok{function}\NormalTok{(x) \{}
967 \KeywordTok{cell_spec}\NormalTok{(x, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{color =} \KeywordTok{spec_color}\NormalTok{(x, }\DataTypeTok{end =} \FloatTok{0.9}\NormalTok{),}
968 \DataTypeTok{font_size =} \KeywordTok{spec_font_size}\NormalTok{(x))}
969\NormalTok{ \}) }\OperatorTok{%>%}
970\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{Species =} \KeywordTok{cell_spec}\NormalTok{(}
971\NormalTok{ Species, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \StringTok{"white"}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T,}
972 \DataTypeTok{background =} \KeywordTok{spec_color}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DataTypeTok{end =} \FloatTok{0.9}\NormalTok{, }\DataTypeTok{option =} \StringTok{"A"}\NormalTok{, }\DataTypeTok{direction =} \DecValTok{-1}\NormalTok{)}
973\NormalTok{ )) }\OperatorTok{%>%}
974\StringTok{ }\KeywordTok{kable}\NormalTok{(}\StringTok{"latex"}\NormalTok{, }\DataTypeTok{escape =}\NormalTok{ F, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{linesep =} \StringTok{""}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{)}
975\end{Highlighting}
976\end{Shaded}
977
978\begin{tabular}{ccccc}
979\toprule
980Sepal.Length & Sepal.Width & Petal.Length & Petal.Width & Species\\
981\midrule
982\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}}}\\
983\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}}}\\
984\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}}}\\
985\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}}}\\
986\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}}}\\
987\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}}}\\
988\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}}}\\
989\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}}}\\
990\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}}}\\
991\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}}}\\
992\bottomrule
993\end{tabular}
994
995In the example above, I'm using the \texttt{mutate} functions from
996\texttt{dplyr}. You don't have to use it. Base R solutions like
997\texttt{iris\$Species\ \textless{}-\ cell\_spec(iris\$Species,\ color\ =\ "red")}
998also works.
999
1000\hypertarget{text-specification}{%
1001\subsection{Text Specification}\label{text-specification}}
1002
1003If you check the results of \texttt{cell\_spec}, you will find that this
1004function does nothing more than wrapping the text with appropriate
1005HTML/LaTeX formatting syntax. The result of this function is just a
1006vector of character strings. As a result, when you are writing a
1007\texttt{rmarkdown} document or write some text in shiny apps, if you
1008need extra markups other than \textbf{bold} or \emph{italic}, you may
1009use this function to \textcolor{red}{color},
1010\bgroup\fontsize{16}{18}\selectfont change font size \egroup{} or
1011\rotatebox{30}{rotate} your text.
1012
1013An aliased function \texttt{text\_spec} is also provided for a more
1014literal writing experience. The only difference is that in LaTeX, unless
1015you specify \texttt{latex\_background\_in\_cell\ =\ FALSE} (default is
1016\texttt{TRUE}) in \texttt{cell\_spec}, it will define cell background
1017color as \texttt{\textbackslash{}cellcolor\{\}}, which doesn't work
1018outside of a table, while for \texttt{text\_spec}, the default value for
1019\texttt{latex\_background\_in\_cell} is \texttt{FALSE}.
1020
1021\begin{Shaded}
1022\begin{Highlighting}[]
1023\NormalTok{sometext <-}\StringTok{ }\KeywordTok{strsplit}\NormalTok{(}\KeywordTok{paste0}\NormalTok{(}
1024 \StringTok{"You can even try to make some crazy things like this paragraph. "}\NormalTok{, }
1025 \StringTok{"It may seem like a useless feature right now but it's so cool "}\NormalTok{,}
1026 \StringTok{"and nobody can resist. ;)"}
1027\NormalTok{), }\StringTok{" "}\NormalTok{)[[}\DecValTok{1}\NormalTok{]]}
1028\NormalTok{text_formatted <-}\StringTok{ }\KeywordTok{paste}\NormalTok{(}
1029 \KeywordTok{text_spec}\NormalTok{(sometext, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{color =} \KeywordTok{spec_color}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\KeywordTok{length}\NormalTok{(sometext), }\DataTypeTok{end =} \FloatTok{0.9}\NormalTok{),}
1030 \DataTypeTok{font_size =} \KeywordTok{spec_font_size}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\KeywordTok{length}\NormalTok{(sometext), }\DataTypeTok{begin =} \DecValTok{5}\NormalTok{, }\DataTypeTok{end =} \DecValTok{20}\NormalTok{)),}
1031 \DataTypeTok{collapse =} \StringTok{" "}\NormalTok{)}
1032
1033\CommentTok{# To display the text, type `r text_formatted` outside of the chunk}
1034\end{Highlighting}
1035\end{Shaded}
1036
1037\bgroup\fontsize{5}{7}\selectfont \textcolor[HTML]{440154}{You}\egroup{} \bgroup\fontsize{6}{8}\selectfont \textcolor[HTML]{470D60}{can}\egroup{} \bgroup\fontsize{6}{8}\selectfont \textcolor[HTML]{48186A}{even}\egroup{} \bgroup\fontsize{7}{9}\selectfont \textcolor[HTML]{482274}{try}\egroup{} \bgroup\fontsize{7}{9}\selectfont \textcolor[HTML]{472D7A}{to}\egroup{} \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{453681}{make}\egroup{} \bgroup\fontsize{8}{10}\selectfont \textcolor[HTML]{424086}{some}\egroup{} \bgroup\fontsize{9}{11}\selectfont \textcolor[HTML]{3E4989}{crazy}\egroup{} \bgroup\fontsize{9}{11}\selectfont \textcolor[HTML]{3B518B}{things}\egroup{} \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{375A8C}{like}\egroup{} \bgroup\fontsize{10}{12}\selectfont \textcolor[HTML]{33628D}{this}\egroup{} \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{306A8E}{paragraph.}\egroup{} \bgroup\fontsize{11}{13}\selectfont \textcolor[HTML]{2C718E}{It}\egroup{} \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{29798E}{may}\egroup{} \bgroup\fontsize{12}{14}\selectfont \textcolor[HTML]{26818E}{seem}\egroup{} \bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{23888E}{like}\egroup{} \bgroup\fontsize{13}{15}\selectfont \textcolor[HTML]{21908D}{a}\egroup{} \bgroup\fontsize{14}{16}\selectfont \textcolor[HTML]{1F968B}{useless}\egroup{} \bgroup\fontsize{14}{16}\selectfont \textcolor[HTML]{1F9E89}{feature}\egroup{} \bgroup\fontsize{15}{17}\selectfont \textcolor[HTML]{21A585}{right}\egroup{} \bgroup\fontsize{15}{17}\selectfont \textcolor[HTML]{26AD81}{now}\egroup{} \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{30B47C}{but}\egroup{} \bgroup\fontsize{16}{18}\selectfont \textcolor[HTML]{3BBB75}{it's}\egroup{} \bgroup\fontsize{17}{19}\selectfont \textcolor[HTML]{4AC16D}{so}\egroup{} \bgroup\fontsize{17}{19}\selectfont \textcolor[HTML]{5AC864}{cool}\egroup{} \bgroup\fontsize{18}{20}\selectfont \textcolor[HTML]{6CCD5A}{and}\egroup{} \bgroup\fontsize{18}{20}\selectfont \textcolor[HTML]{7FD34E}{nobody}\egroup{} \bgroup\fontsize{19}{21}\selectfont \textcolor[HTML]{91D742}{can}\egroup{} \bgroup\fontsize{19}{21}\selectfont \textcolor[HTML]{A6DB35}{resist.}\egroup{} \bgroup\fontsize{20}{22}\selectfont \textcolor[HTML]{BBDF27}{;)}\egroup{}
1038
1039\hypertarget{grouped-columns-rows}{%
1040\section{Grouped Columns / Rows}\label{grouped-columns-rows}}
1041
1042\hypertarget{add-header-rows-to-group-columns}{%
1043\subsection{Add header rows to group
1044columns}\label{add-header-rows-to-group-columns}}
1045
1046Tables with multi-row headers can be very useful to demonstrate grouped
1047data. To do that, you can pipe your kable object into
1048\texttt{add\_header\_above()}. The header variable is supposed to be a
1049named character with the names as new column names and values as column
1050span. For your convenience, if column span equals to 1, you can ignore
1051the \texttt{=1} part so the function below can be written as
1052`add\_header\_above(c(" ``,''Group 1" = 2, ``Group 2'' = 2, ``Group 3''
1053= 2)).
1054
1055\begin{Shaded}
1056\begin{Highlighting}[]
1057\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1058\StringTok{ }\KeywordTok{kable_styling}\NormalTok{() }\OperatorTok{%>%}
1059\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{ =}\StringTok{ }\DecValTok{1}\NormalTok{, }\StringTok{"Group 1"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{, }\StringTok{"Group 2"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{, }\StringTok{"Group 3"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{))}
1060\end{Highlighting}
1061\end{Shaded}
1062
1063\begin{table}[H]
1064\centering
1065\begin{tabular}{lrrrrrr}
1066\toprule
1067\multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group 1} & \multicolumn{2}{c}{Group 2} & \multicolumn{2}{c}{Group 3} \\
1068\cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-5} \cmidrule(l{3pt}r{3pt}){6-7}
1069 & mpg & cyl & disp & hp & drat & wt\\
1070\midrule
1071Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
1072Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
1073Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
1074Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
1075Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
1076\bottomrule
1077\end{tabular}
1078\end{table}
1079
1080In fact, if you want to add another row of header on top, please feel
1081free to do so. Also, since kableExtra 0.3.0, you can specify
1082\texttt{bold} \& \texttt{italic} as you do in \texttt{row\_spec()}.
1083
1084\begin{Shaded}
1085\begin{Highlighting}[]
1086\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1087\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \StringTok{"striped"}\NormalTok{) }\OperatorTok{%>%}
1088\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 1"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{, }\StringTok{"Group 2"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{, }\StringTok{"Group 3"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{)) }\OperatorTok{%>%}
1089\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 4"}\NormalTok{ =}\StringTok{ }\DecValTok{4}\NormalTok{, }\StringTok{"Group 5"}\NormalTok{ =}\StringTok{ }\DecValTok{2}\NormalTok{)) }\OperatorTok{%>%}
1090\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 6"}\NormalTok{ =}\StringTok{ }\DecValTok{6}\NormalTok{), }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{italic =}\NormalTok{ T)}
1091\end{Highlighting}
1092\end{Shaded}
1093
1094\begin{table}[H]
1095\centering\rowcolors{2}{gray!6}{white}
1096
1097\begin{tabular}{lrrrrrr}
1098\hiderowcolors
1099\toprule
1100\multicolumn{1}{c}{\em{\textbf{ }}} & \multicolumn{6}{c}{\em{\textbf{Group 6}}} \\
1101\cmidrule(l{3pt}r{3pt}){2-7}
1102\multicolumn{1}{c}{ } & \multicolumn{4}{c}{Group 4} & \multicolumn{2}{c}{Group 5} \\
1103\cmidrule(l{3pt}r{3pt}){2-5} \cmidrule(l{3pt}r{3pt}){6-7}
1104\multicolumn{1}{c}{ } & \multicolumn{2}{c}{Group 1} & \multicolumn{2}{c}{Group 2} & \multicolumn{2}{c}{Group 3} \\
1105\cmidrule(l{3pt}r{3pt}){2-3} \cmidrule(l{3pt}r{3pt}){4-5} \cmidrule(l{3pt}r{3pt}){6-7}
1106 & mpg & cyl & disp & hp & drat & wt\\
1107\midrule
1108\showrowcolors
1109Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
1110Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
1111Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
1112Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
1113Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
1114\bottomrule
1115\end{tabular}
1116\rowcolors{2}{white}{white}
1117\end{table}
1118
1119\hypertarget{group-rows-via-labeling}{%
1120\subsection{Group rows via labeling}\label{group-rows-via-labeling}}
1121
1122Sometimes we want a few rows of the table being grouped together. They
1123might be items under the same topic (e.g., animals in one species) or
1124just different data groups for a categorical variable (e.g., age
1125\textless{} 40, age \textgreater{} 40). With the new function
1126\texttt{group\_rows()} in \texttt{kableExtra}, this kind of task can be
1127completed in one line. Please see the example below. Note that when you
1128count for the start/end rows of the group, you don't need to count for
1129the header rows nor other group label rows. You only need to think about
1130the row numbers in the ``original R dataframe''.
1131
1132\begin{Shaded}
1133\begin{Highlighting}[]
1134\KeywordTok{kable}\NormalTok{(mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{6}\NormalTok{], }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{caption =} \StringTok{"Group Rows"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1135\StringTok{ }\KeywordTok{kable_styling}\NormalTok{() }\OperatorTok{%>%}
1136\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\StringTok{"Group 1"}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{7}\NormalTok{) }\OperatorTok{%>%}
1137\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\StringTok{"Group 2"}\NormalTok{, }\DecValTok{8}\NormalTok{, }\DecValTok{10}\NormalTok{)}
1138\end{Highlighting}
1139\end{Shaded}
1140
1141\begin{table}[t]
1142
1143\caption{\label{tab:unnamed-chunk-24}Group Rows}
1144\centering
1145\begin{tabular}{lrrrrrr}
1146\toprule
1147 & mpg & cyl & disp & hp & drat & wt\\
1148\midrule
1149Mazda RX4 & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.620\\
1150Mazda RX4 Wag & 21.0 & 6 & 160.0 & 110 & 3.90 & 2.875\\
1151Datsun 710 & 22.8 & 4 & 108.0 & 93 & 3.85 & 2.320\\
1152\addlinespace[0.3em]
1153\multicolumn{7}{l}{\textbf{Group 1}}\\
1154\hspace{1em}Hornet 4 Drive & 21.4 & 6 & 258.0 & 110 & 3.08 & 3.215\\
1155\hspace{1em}Hornet Sportabout & 18.7 & 8 & 360.0 & 175 & 3.15 & 3.440\\
1156\hspace{1em}Valiant & 18.1 & 6 & 225.0 & 105 & 2.76 & 3.460\\
1157\hspace{1em}Duster 360 & 14.3 & 8 & 360.0 & 245 & 3.21 & 3.570\\
1158\addlinespace[0.3em]
1159\multicolumn{7}{l}{\textbf{Group 2}}\\
1160\hspace{1em}Merc 240D & 24.4 & 4 & 146.7 & 62 & 3.69 & 3.190\\
1161\hspace{1em}Merc 230 & 22.8 & 4 & 140.8 & 95 & 3.92 & 3.150\\
1162\hspace{1em}Merc 280 & 19.2 & 6 & 167.6 & 123 & 3.92 & 3.440\\
1163\bottomrule
1164\end{tabular}
1165\end{table}
1166
1167In case some users need it, you can define your own gapping spaces
1168between the group labeling row and previous rows. The default value is
1169\texttt{0.5em}.
1170
1171\begin{Shaded}
1172\begin{Highlighting}[]
1173\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1174\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\StringTok{"Group 1"}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{, }\DataTypeTok{latex_gap_space =} \StringTok{"2em"}\NormalTok{)}
1175\end{Highlighting}
1176\end{Shaded}
1177
1178\begin{tabular}{lrrrrrr}
1179\toprule
1180 & mpg & cyl & disp & hp & drat & wt\\
1181\midrule
1182Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
1183Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
1184Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
1185\addlinespace[2em]
1186\multicolumn{7}{l}{\textbf{Group 1}}\\
1187\hspace{1em}Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
1188\hspace{1em}Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
1189\bottomrule
1190\end{tabular}
1191
1192If you prefer to build multiple groups in one step, you can use the
1193short-hand \texttt{index} option. Basically, you can use it in the same
1194way as you use \texttt{add\_header\_above}. However, since
1195\texttt{group\_row} only support one layer of grouping, you can't add
1196multiple layers of grouping header as you can do in
1197\texttt{add\_header\_above}.
1198
1199\begin{Shaded}
1200\begin{Highlighting}[]
1201\KeywordTok{kable}\NormalTok{(mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{10}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{6}\NormalTok{], }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{caption =} \StringTok{"Group Rows"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1202\StringTok{ }\KeywordTok{kable_styling}\NormalTok{() }\OperatorTok{%>%}
1203\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\DataTypeTok{index=}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{ =}\StringTok{ }\DecValTok{3}\NormalTok{, }\StringTok{"Group 1"}\NormalTok{ =}\StringTok{ }\DecValTok{4}\NormalTok{, }\StringTok{"Group 2"}\NormalTok{ =}\StringTok{ }\DecValTok{3}\NormalTok{))}
1204\CommentTok{# Not evaluated. The code above should have the same result as the first example in this section.}
1205\end{Highlighting}
1206\end{Shaded}
1207
1208Note that \texttt{kable} has a relatively special feature to handle
1209\texttt{align} and it may bring troubles to you if you are not using it
1210correctly. In the documentation of the \texttt{align} argument of
1211\texttt{kable}, it says:
1212
1213\begin{quote}
1214If \texttt{length(align)\ ==\ 1L}, the string will be expanded to a
1215vector of individual letters, e.g.
1216\texttt{\textquotesingle{}clc\textquotesingle{}} becomes
1217\texttt{c(\textquotesingle{}c\textquotesingle{},\ \textquotesingle{}l\textquotesingle{},\ \textquotesingle{}c\textquotesingle{})},
1218\textbf{unless the output format is LaTeX}.
1219\end{quote}
1220
1221For example,
1222
1223\begin{Shaded}
1224\begin{Highlighting}[]
1225\KeywordTok{kable}\NormalTok{(mtcars[}\DecValTok{1}\OperatorTok{:}\DecValTok{2}\NormalTok{, }\DecValTok{1}\OperatorTok{:}\DecValTok{2}\NormalTok{], }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \KeywordTok{c}\NormalTok{(}\StringTok{"cl"}\NormalTok{))}
1226\CommentTok{# \textbackslash{}begin\{tabular\}\{l|cl|cl\} # Note the column alignment here}
1227\CommentTok{# \textbackslash{}hline}
1228\CommentTok{# & mpg & cyl\textbackslash{}\textbackslash{}}
1229\CommentTok{# ...}
1230\end{Highlighting}
1231\end{Shaded}
1232
1233LaTeX, somehow shows surprisingly high tolerance on that, which is quite
1234unusual. As a result, it won't throw an error if you are just using
1235\texttt{kable} to make some simple tables. However, when you use
1236\texttt{kableExtra} to make some advanced modification, it will start to
1237throw some bugs. As a result, please try to form a habit of using a
1238vector in the \texttt{align} argument for \texttt{kable} (tip: you can
1239use \texttt{rep} function to replicate elements. For example,
1240\texttt{c("c",\ rep("l",\ 10))}).
1241
1242\hypertarget{row-indentation}{%
1243\subsection{Row indentation}\label{row-indentation}}
1244
1245Unlike \texttt{group\_rows()}, which will insert a labeling row,
1246sometimes we want to list a few sub groups under a total one. In that
1247case, \texttt{add\_indent()} is probably more appropriate.\\
1248For advanced users, you can even define your own css for the group
1249labeling.
1250
1251\begin{Shaded}
1252\begin{Highlighting}[]
1253\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1254\StringTok{ }\KeywordTok{add_indent}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DecValTok{3}\NormalTok{, }\DecValTok{5}\NormalTok{))}
1255\end{Highlighting}
1256\end{Shaded}
1257
1258\begin{tabular}{lrrrrrr}
1259\toprule
1260 & mpg & cyl & disp & hp & drat & wt\\
1261\midrule
1262\hspace{1em}Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
1263Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
1264\hspace{1em}Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
1265Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
1266\hspace{1em}Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
1267\bottomrule
1268\end{tabular}
1269
1270\hypertarget{group-rows-via-multi-row-cell}{%
1271\subsection{Group rows via multi-row
1272cell}\label{group-rows-via-multi-row-cell}}
1273
1274Function \texttt{group\_rows} is great for showing simple structural
1275information on rows but sometimes people may need to show structural
1276information with multiple layers. When it happens, you may consider
1277using \texttt{collapse\_rows} instead, which will put repeating cells in
1278columns into multi-row cells.
1279
1280In LaTeX, \texttt{collapse\_rows} adds some extra hlines to help
1281differentiate groups. You can customize this behavior using the
1282\texttt{latex\_hline} argument. You can choose from \texttt{full}
1283(default), \texttt{major} and \texttt{none}. Vertical alignment of cells
1284is controlled by the \texttt{valign} option. You can choose from
1285``top'', ``middle''(default) and ``bottom''. Be cautious that the
1286vertical alignment option was only introduced in multirow in 2016. If
1287you are using a legacy LaTeX distribution, you will run into trouble if
1288you set \texttt{valign} to be either ``top'' or ``bottom''.
1289
1290\begin{Shaded}
1291\begin{Highlighting}[]
1292\NormalTok{collapse_rows_dt <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}\DataTypeTok{C1 =} \KeywordTok{c}\NormalTok{(}\KeywordTok{rep}\NormalTok{(}\StringTok{"a"}\NormalTok{, }\DecValTok{10}\NormalTok{), }\KeywordTok{rep}\NormalTok{(}\StringTok{"b"}\NormalTok{, }\DecValTok{5}\NormalTok{)),}
1293 \DataTypeTok{C2 =} \KeywordTok{c}\NormalTok{(}\KeywordTok{rep}\NormalTok{(}\StringTok{"c"}\NormalTok{, }\DecValTok{7}\NormalTok{), }\KeywordTok{rep}\NormalTok{(}\StringTok{"d"}\NormalTok{, }\DecValTok{3}\NormalTok{), }\KeywordTok{rep}\NormalTok{(}\StringTok{"c"}\NormalTok{, }\DecValTok{2}\NormalTok{), }\KeywordTok{rep}\NormalTok{(}\StringTok{"d"}\NormalTok{, }\DecValTok{3}\NormalTok{)),}
1294 \DataTypeTok{C3 =} \DecValTok{1}\OperatorTok{:}\DecValTok{15}\NormalTok{,}
1295 \DataTypeTok{C4 =} \KeywordTok{sample}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{0}\NormalTok{,}\DecValTok{1}\NormalTok{), }\DecValTok{15}\NormalTok{, }\DataTypeTok{replace =} \OtherTok{TRUE}\NormalTok{))}
1296\KeywordTok{kable}\NormalTok{(collapse_rows_dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{) }\OperatorTok{%>%}
1297\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold=}\NormalTok{T) }\OperatorTok{%>%}
1298\StringTok{ }\KeywordTok{collapse_rows}\NormalTok{(}\DataTypeTok{columns =} \DecValTok{1}\OperatorTok{:}\DecValTok{2}\NormalTok{, }\DataTypeTok{latex_hline =} \StringTok{"major"}\NormalTok{, }\DataTypeTok{valign =} \StringTok{"middle"}\NormalTok{)}
1299\end{Highlighting}
1300\end{Shaded}
1301
1302\begin{tabular}{>{\bfseries}cccc}
1303\toprule
1304C1 & C2 & C3 & C4\\
1305\midrule
1306 & & 1 & 1\\
1307
1308 & & 2 & 0\\
1309
1310 & & 3 & 0\\
1311
1312 & & 4 & 1\\
1313
1314 & & 5 & 1\\
1315
1316 & & 6 & 1\\
1317
1318 & \multirow{-7}{*}{\centering\arraybackslash c} & 7 & 1\\
1319
1320 & & 8 & 0\\
1321
1322 & & 9 & 0\\
1323
1324\multirow{-10}{*}{\centering\arraybackslash a} & \multirow{-3}{*}{\centering\arraybackslash d} & 10 & 1\\
1325\cmidrule{1-4}
1326 & & 11 & 0\\
1327
1328 & \multirow{-2}{*}{\centering\arraybackslash c} & 12 & 1\\
1329
1330 & & 13 & 0\\
1331
1332 & & 14 & 1\\
1333
1334\multirow{-5}{*}{\centering\arraybackslash b} & \multirow{-3}{*}{\centering\arraybackslash d} & 15 & 0\\
1335\bottomrule
1336\end{tabular}
1337
1338Right now, you can't automatically make striped rows based on collapsed
1339rows but you can do it manually via the \texttt{extra\_latex\_after}
1340option in \texttt{row\_spec}. This feature is not officially supported.
1341I'm only document it here if you want to give it a try.
1342
1343\begin{Shaded}
1344\begin{Highlighting}[]
1345\KeywordTok{kable}\NormalTok{(collapse_rows_dt[}\OperatorTok{-}\DecValTok{1}\NormalTok{], }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1346\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{width =} \StringTok{"5em"}\NormalTok{) }\OperatorTok{%>%}
1347\StringTok{ }\KeywordTok{row_spec}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{7}\NormalTok{, }\DecValTok{11}\OperatorTok{:}\DecValTok{12}\NormalTok{) }\OperatorTok{-}\StringTok{ }\DecValTok{1}\NormalTok{, }\DataTypeTok{extra_latex_after =} \StringTok{"}\CharTok{\textbackslash{}\textbackslash{}}\StringTok{rowcolor\{gray!6\}"}\NormalTok{) }\OperatorTok{%>%}
1348\StringTok{ }\KeywordTok{collapse_rows}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{latex_hline =} \StringTok{"none"}\NormalTok{)}
1349\end{Highlighting}
1350\end{Shaded}
1351
1352\begin{tabular}{>{\bfseries\centering\arraybackslash}p{5em}cc}
1353\toprule
1354C2 & C3 & C4\\
1355\rowcolor{gray!6}
1356\midrule
1357 & 1 & 1\\
1358
1359\rowcolor{gray!6}
1360 & 2 & 0\\
1361
1362\rowcolor{gray!6}
1363 & 3 & 0\\
1364
1365\rowcolor{gray!6}
1366 & 4 & 1\\
1367
1368\rowcolor{gray!6}
1369 & 5 & 1\\
1370
1371\rowcolor{gray!6}
1372 & 6 & 1\\
1373
1374\rowcolor{gray!6}
1375\multirow{-7}{5em}{\centering\arraybackslash c} & 7 & 1\\
1376
1377 & 8 & 0\\
1378
1379 & 9 & 0\\
1380
1381\multirow{-3}{5em}{\centering\arraybackslash d} & 10 & 1\\
1382
1383\rowcolor{gray!6}
1384 & 11 & 0\\
1385
1386\rowcolor{gray!6}
1387\multirow{-2}{5em}{\centering\arraybackslash c} & 12 & 1\\
1388
1389 & 13 & 0\\
1390
1391 & 14 & 1\\
1392
1393\multirow{-3}{5em}{\centering\arraybackslash d} & 15 & 0\\
1394\bottomrule
1395\end{tabular}
1396
1397When there are too many layers, sometimes the table can become too wide.
1398You can choose to stack the first few layers by setting
1399\texttt{row\_group\_label\_position} to \texttt{stack}.
1400
1401\begin{Shaded}
1402\begin{Highlighting}[]
1403\NormalTok{collapse_rows_dt <-}\StringTok{ }\KeywordTok{expand.grid}\NormalTok{(}
1404 \DataTypeTok{Country =} \KeywordTok{sprintf}\NormalTok{(}\StringTok{'Country with a long name %s'}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\StringTok{'A'}\NormalTok{, }\StringTok{'B'}\NormalTok{)),}
1405 \DataTypeTok{State =} \KeywordTok{sprintf}\NormalTok{(}\StringTok{'State %s'}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\StringTok{'a'}\NormalTok{, }\StringTok{'b'}\NormalTok{)),}
1406 \DataTypeTok{City =} \KeywordTok{sprintf}\NormalTok{(}\StringTok{'City %s'}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\StringTok{'1'}\NormalTok{, }\StringTok{'2'}\NormalTok{)),}
1407 \DataTypeTok{District =} \KeywordTok{sprintf}\NormalTok{(}\StringTok{'District %s'}\NormalTok{, }\KeywordTok{c}\NormalTok{(}\StringTok{'1'}\NormalTok{, }\StringTok{'2'}\NormalTok{))}
1408\NormalTok{) }\OperatorTok{%>%}\StringTok{ }\KeywordTok{arrange}\NormalTok{(Country, State, City) }\OperatorTok{%>%}
1409\StringTok{ }\KeywordTok{mutate_all}\NormalTok{(as.character) }\OperatorTok{%>%}
1410\StringTok{ }\KeywordTok{mutate}\NormalTok{(}\DataTypeTok{C1 =} \KeywordTok{rnorm}\NormalTok{(}\KeywordTok{n}\NormalTok{()),}
1411 \DataTypeTok{C2 =} \KeywordTok{rnorm}\NormalTok{(}\KeywordTok{n}\NormalTok{()))}
1412
1413\KeywordTok{kable}\NormalTok{(collapse_rows_dt, }\StringTok{"latex"}\NormalTok{, }
1414 \DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{linesep =} \StringTok{''}\NormalTok{) }\OperatorTok{%>%}
1415\StringTok{ }\KeywordTok{collapse_rows}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{, }\DataTypeTok{row_group_label_position =} \StringTok{'stack'}\NormalTok{) }
1416\end{Highlighting}
1417\end{Shaded}
1418
1419\begin{tabular}{cccccc}
1420\toprule
1421 & & City & District & C1 & C2\\
1422\midrule
1423\addlinespace[0.3em]
1424\multicolumn{6}{l}{\textbf{Country with a long name A}}\\
1425\addlinespace[0.3em]
1426\multicolumn{6}{l}{\textit{State a}}\\
1427\hspace{1em}\hspace{1em} & & City 1 & District 1 & -0.1770795 & -0.2171630\\
1428\cmidrule{4-6}
1429\hspace{1em}\hspace{1em} & & & District 2 & -0.7480018 & -0.9850862\\
1430\cmidrule{3-6}
1431\hspace{1em}\hspace{1em} & & City 2 & District 1 & 0.7337258 & -0.5222169\\
1432\cmidrule{4-6}
1433\hspace{1em}\hspace{1em} & & & District 2 & -0.4759916 & -1.0309802\\
1434\cmidrule{2-6}
1435\addlinespace[0.3em]
1436\multicolumn{6}{l}{\textit{State b}}\\
1437\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.6750550 & -2.4739793\\
1438\cmidrule{4-6}
1439\hspace{1em}\hspace{1em} & & & District 2 & 0.3049046 & -1.2631447\\
1440\cmidrule{3-6}
1441\hspace{1em}\hspace{1em} & & City 2 & District 1 & 0.0286753 & 0.2100736\\
1442\cmidrule{4-6}
1443\hspace{1em}\hspace{1em} & & & District 2 & 0.5833306 & -0.1407191\\
1444\cmidrule{1-6}
1445\addlinespace[0.3em]
1446\multicolumn{6}{l}{\textbf{Country with a long name B}}\\
1447\addlinespace[0.3em]
1448\multicolumn{6}{l}{\textit{State a}}\\
1449\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.0431486 & -0.9681845\\
1450\cmidrule{4-6}
1451\hspace{1em}\hspace{1em} & & & District 2 & -0.6211451 & -1.4915492\\
1452\cmidrule{3-6}
1453\hspace{1em}\hspace{1em} & & City 2 & District 1 & -0.1115081 & 0.2991019\\
1454\cmidrule{4-6}
1455\hspace{1em}\hspace{1em} & & & District 2 & -1.1478388 & -1.4530953\\
1456\cmidrule{2-6}
1457\addlinespace[0.3em]
1458\multicolumn{6}{l}{\textit{State b}}\\
1459\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.1440275 & -0.5458611\\
1460\cmidrule{4-6}
1461\hspace{1em}\hspace{1em} & & & District 2 & 0.7375365 & 0.9307218\\
1462\cmidrule{3-6}
1463\hspace{1em}\hspace{1em} & & City 2 & District 1 & -0.2804654 & -0.6222302\\
1464\cmidrule{4-6}
1465\hspace{1em}\hspace{1em} & & & District 2 & 0.3877343 & 1.9276181\\
1466\bottomrule
1467\end{tabular}
1468
1469To better distinguish different layers, you can format the each layer
1470using \texttt{row\_group\_label\_fonts}. You can also customize the
1471hlines to better differentiate groups.
1472
1473\begin{Shaded}
1474\begin{Highlighting}[]
1475\NormalTok{row_group_label_fonts <-}\StringTok{ }\KeywordTok{list}\NormalTok{(}
1476 \KeywordTok{list}\NormalTok{(}\DataTypeTok{bold =}\NormalTok{ T, }\DataTypeTok{italic =}\NormalTok{ T), }
1477 \KeywordTok{list}\NormalTok{(}\DataTypeTok{bold =}\NormalTok{ F, }\DataTypeTok{italic =}\NormalTok{ F)}
1478\NormalTok{ )}
1479\KeywordTok{kable}\NormalTok{(collapse_rows_dt, }\StringTok{"latex"}\NormalTok{, }
1480 \DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{linesep =} \StringTok{''}\NormalTok{) }\OperatorTok{%>%}
1481\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold=}\NormalTok{T) }\OperatorTok{%>%}
1482\StringTok{ }\KeywordTok{collapse_rows}\NormalTok{(}\DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{, }\DataTypeTok{latex_hline =} \StringTok{'custom'}\NormalTok{, }\DataTypeTok{custom_latex_hline =} \DecValTok{1}\OperatorTok{:}\DecValTok{3}\NormalTok{, }
1483 \DataTypeTok{row_group_label_position =} \StringTok{'stack'}\NormalTok{, }
1484 \DataTypeTok{row_group_label_fonts =}\NormalTok{ row_group_label_fonts) }
1485\end{Highlighting}
1486\end{Shaded}
1487
1488\begin{tabular}{>{\bfseries}cccccc}
1489\toprule
1490 & & City & District & C1 & C2\\
1491\midrule
1492\addlinespace[0.3em]
1493\multicolumn{6}{l}{\textit{\textbf{Country with a long name A}}}\\
1494\addlinespace[0.3em]
1495\multicolumn{6}{l}{State a}\\
1496\hspace{1em}\hspace{1em} & & City 1 & District 1 & -0.1770795 & -0.2171630\\
1497
1498\hspace{1em}\hspace{1em} & & & District 2 & -0.7480018 & -0.9850862\\
1499\cmidrule{3-6}
1500\hspace{1em}\hspace{1em} & & City 2 & District 1 & 0.7337258 & -0.5222169\\
1501
1502\hspace{1em}\hspace{1em} & & & District 2 & -0.4759916 & -1.0309802\\
1503\cmidrule{2-6}
1504\addlinespace[0.3em]
1505\multicolumn{6}{l}{State b}\\
1506\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.6750550 & -2.4739793\\
1507
1508\hspace{1em}\hspace{1em} & & & District 2 & 0.3049046 & -1.2631447\\
1509\cmidrule{3-6}
1510\hspace{1em}\hspace{1em} & & City 2 & District 1 & 0.0286753 & 0.2100736\\
1511
1512\hspace{1em}\hspace{1em} & & & District 2 & 0.5833306 & -0.1407191\\
1513\cmidrule{1-6}
1514\addlinespace[0.3em]
1515\multicolumn{6}{l}{\textit{\textbf{Country with a long name B}}}\\
1516\addlinespace[0.3em]
1517\multicolumn{6}{l}{State a}\\
1518\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.0431486 & -0.9681845\\
1519
1520\hspace{1em}\hspace{1em} & & & District 2 & -0.6211451 & -1.4915492\\
1521\cmidrule{3-6}
1522\hspace{1em}\hspace{1em} & & City 2 & District 1 & -0.1115081 & 0.2991019\\
1523
1524\hspace{1em}\hspace{1em} & & & District 2 & -1.1478388 & -1.4530953\\
1525\cmidrule{2-6}
1526\addlinespace[0.3em]
1527\multicolumn{6}{l}{State b}\\
1528\hspace{1em}\hspace{1em} & & City 1 & District 1 & 0.1440275 & -0.5458611\\
1529
1530\hspace{1em}\hspace{1em} & & & District 2 & 0.7375365 & 0.9307218\\
1531\cmidrule{3-6}
1532\hspace{1em}\hspace{1em} & & City 2 & District 1 & -0.2804654 & -0.6222302\\
1533
1534\hspace{1em}\hspace{1em} & & & District 2 & 0.3877343 & 1.9276181\\
1535\bottomrule
1536\end{tabular}
1537
1538\hypertarget{table-footnote}{%
1539\section{Table Footnote}\label{table-footnote}}
1540
1541\begin{quote}
1542Now it's recommended to use the new \texttt{footnote} function instead
1543of \texttt{add\_footnote} to make table footnotes.
1544\end{quote}
1545
1546Documentations for \texttt{add\_footnote} can be found
1547\href{http://haozhu233.github.io/kableExtra/legacy_features\#add_footnote}{here}.
1548
1549There are four notation systems in \texttt{footnote}, namely
1550\texttt{general}, \texttt{number}, \texttt{alphabet} and
1551\texttt{symbol}. The last three types of footnotes will be labeled with
1552corresponding marks while \texttt{general} won't be labeled. You can
1553pick any one of these systems or choose to display them all for
1554fulfilling the APA table footnotes requirements.
1555
1556\begin{Shaded}
1557\begin{Highlighting}[]
1558\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{) }\OperatorTok{%>%}
1559\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{full_width =}\NormalTok{ F) }\OperatorTok{%>%}
1560\StringTok{ }\KeywordTok{footnote}\NormalTok{(}\DataTypeTok{general =} \StringTok{"Here is a general comments of the table. "}\NormalTok{,}
1561 \DataTypeTok{number =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote 1; "}\NormalTok{, }\StringTok{"Footnote 2; "}\NormalTok{),}
1562 \DataTypeTok{alphabet =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote A; "}\NormalTok{, }\StringTok{"Footnote B; "}\NormalTok{),}
1563 \DataTypeTok{symbol =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote Symbol 1; "}\NormalTok{, }\StringTok{"Footnote Symbol 2"}\NormalTok{)}
1564\NormalTok{ )}
1565\end{Highlighting}
1566\end{Shaded}
1567
1568\begin{table}[H]
1569\centering
1570\begin{tabular}{l|c|c|c|c|c|c}
1571\hline
1572 & mpg & cyl & disp & hp & drat & wt\\
1573\hline
1574Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
1575\hline
1576Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
1577\hline
1578Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
1579\hline
1580Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
1581\hline
1582Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
1583\hline
1584\multicolumn{7}{l}{\textit{Note: }}\\
1585\multicolumn{7}{l}{Here is a general comments of the table. }\\
1586\multicolumn{7}{l}{\textsuperscript{1} Footnote 1; }\\
1587\multicolumn{7}{l}{\textsuperscript{2} Footnote 2; }\\
1588\multicolumn{7}{l}{\textsuperscript{a} Footnote A; }\\
1589\multicolumn{7}{l}{\textsuperscript{b} Footnote B; }\\
1590\multicolumn{7}{l}{\textsuperscript{*} Footnote Symbol 1; }\\
1591\multicolumn{7}{l}{\textsuperscript{\dag} Footnote Symbol 2}\\
1592\end{tabular}
1593\end{table}
1594
1595You can also specify title for each category by using the
1596\texttt{***\_title} arguments. Default value for \texttt{general\_title}
1597is ``Note:'' and "" for the rest three. You can also change the order
1598using \texttt{footnote\_order}. You can even display footnote as chunk
1599texts (default is as a list) using \texttt{footnote\_as\_chunk}. The
1600font format of the titles are controlled by \texttt{title\_format} with
1601options including ``italic'' (default), ``bold'' and ``underline''.
1602
1603\begin{Shaded}
1604\begin{Highlighting}[]
1605\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1606\StringTok{ }\KeywordTok{footnote}\NormalTok{(}\DataTypeTok{general =} \StringTok{"Here is a general comments of the table. "}\NormalTok{,}
1607 \DataTypeTok{number =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote 1; "}\NormalTok{, }\StringTok{"Footnote 2; "}\NormalTok{),}
1608 \DataTypeTok{alphabet =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote A; "}\NormalTok{, }\StringTok{"Footnote B; "}\NormalTok{),}
1609 \DataTypeTok{symbol =} \KeywordTok{c}\NormalTok{(}\StringTok{"Footnote Symbol 1; "}\NormalTok{, }\StringTok{"Footnote Symbol 2"}\NormalTok{),}
1610 \DataTypeTok{general_title =} \StringTok{"General: "}\NormalTok{, }\DataTypeTok{number_title =} \StringTok{"Type I: "}\NormalTok{,}
1611 \DataTypeTok{alphabet_title =} \StringTok{"Type II: "}\NormalTok{, }\DataTypeTok{symbol_title =} \StringTok{"Type III: "}\NormalTok{,}
1612 \DataTypeTok{footnote_as_chunk =}\NormalTok{ T, }\DataTypeTok{title_format =} \KeywordTok{c}\NormalTok{(}\StringTok{"italic"}\NormalTok{, }\StringTok{"underline"}\NormalTok{)}
1613\NormalTok{ )}
1614\end{Highlighting}
1615\end{Shaded}
1616
1617\begin{tabular}{lcccccc}
1618\toprule
1619 & mpg & cyl & disp & hp & drat & wt\\
1620\midrule
1621Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
1622Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
1623Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
1624Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
1625Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
1626\bottomrule
1627\multicolumn{7}{l}{\underline{\textit{General: }} Here is a general comments of the table. }\\
1628\multicolumn{7}{l}{\underline{\textit{Type I: }} \textsuperscript{1} Footnote 1; \textsuperscript{2} Footnote 2; }\\
1629\multicolumn{7}{l}{\underline{\textit{Type II: }} \textsuperscript{a} Footnote A; \textsuperscript{b} Footnote B; }\\
1630\multicolumn{7}{l}{\underline{\textit{Type III: }} \textsuperscript{*} Footnote Symbol 1; \textsuperscript{\dag} Footnote Symbol 2}\\
1631\end{tabular}
1632
1633If you need to add footnote marks in a table, you need to do it manually
1634(no fancy) using \texttt{footnote\_marker\_***()}. Remember that similar
1635with \texttt{cell\_spec}, you need to tell this function whether you
1636want it to do it in \texttt{HTML} (default) or \texttt{LaTeX}. You can
1637set it for all using the \texttt{knitr.table.format} global option.
1638Also, if you have ever used \texttt{footnote\_marker\_***()}, you need
1639to put \texttt{escape\ =\ F} in your \texttt{kable} function to avoid
1640escaping of special characters. Note that if you want to use these
1641\texttt{footnote\_marker} functions in \texttt{kableExtra} functions
1642like \texttt{group\_rows} (for the row label) or
1643\texttt{add\_header\_above}, you need to set
1644\texttt{double\_escape\ =\ T} and \texttt{escape\ =\ F} in those
1645functions. I'm trying to find other ways around. Please let me know if
1646you have a good idea and are willing to contribute.
1647
1648\begin{Shaded}
1649\begin{Highlighting}[]
1650\NormalTok{dt_footnote <-}\StringTok{ }\NormalTok{dt}
1651\KeywordTok{names}\NormalTok{(dt_footnote)[}\DecValTok{2}\NormalTok{] <-}\StringTok{ }\KeywordTok{paste0}\NormalTok{(}\KeywordTok{names}\NormalTok{(dt_footnote)[}\DecValTok{2}\NormalTok{], }
1652 \CommentTok{# That "latex" can be eliminated if defined in global}
1653 \KeywordTok{footnote_marker_symbol}\NormalTok{(}\DecValTok{1}\NormalTok{, }\StringTok{"latex"}\NormalTok{))}
1654\KeywordTok{row.names}\NormalTok{(dt_footnote)[}\DecValTok{4}\NormalTok{] <-}\StringTok{ }\KeywordTok{paste0}\NormalTok{(}\KeywordTok{row.names}\NormalTok{(dt_footnote)[}\DecValTok{4}\NormalTok{], }
1655 \KeywordTok{footnote_marker_alphabet}\NormalTok{(}\DecValTok{1}\NormalTok{))}
1656\KeywordTok{kable}\NormalTok{(dt_footnote, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T,}
1657 \CommentTok{# Remember this escape = F}
1658 \DataTypeTok{escape =}\NormalTok{ F) }\OperatorTok{%>%}
1659\StringTok{ }\KeywordTok{footnote}\NormalTok{(}\DataTypeTok{alphabet =} \StringTok{"Footnote A; "}\NormalTok{,}
1660 \DataTypeTok{symbol =} \StringTok{"Footnote Symbol 1; "}\NormalTok{,}
1661 \DataTypeTok{alphabet_title =} \StringTok{"Type II: "}\NormalTok{, }\DataTypeTok{symbol_title =} \StringTok{"Type III: "}\NormalTok{,}
1662 \DataTypeTok{footnote_as_chunk =}\NormalTok{ T)}
1663\end{Highlighting}
1664\end{Shaded}
1665
1666\begin{tabular}{lcccccc}
1667\toprule
1668 & mpg & cyl\textsuperscript{*} & disp & hp & drat & wt\\
1669\midrule
1670Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
1671Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
1672Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
1673Hornet 4 Drive\textsuperscript{a} & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
1674Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
1675\bottomrule
1676\multicolumn{7}{l}{\textit{Type II: } \textsuperscript{a} Footnote A; }\\
1677\multicolumn{7}{l}{\textit{Type III: } \textsuperscript{*} Footnote Symbol 1; }\\
1678\end{tabular}
1679
1680If your table footnote is very long, please consider to put your table
1681in a \texttt{ThreePartTable} frame. Note that, in kableExtra version
1682\textless{}= 0.7.0, we were using \texttt{threeparttable} but since
1683kableExtra 0.8.0, we start to use \texttt{ThreePartTable} from
1684\texttt{threeparttablex} instead. \texttt{ThreePartTable} supports both
1685the \texttt{longtable} and \texttt{tabu} environments.
1686
1687\begin{Shaded}
1688\begin{Highlighting}[]
1689\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{caption =} \StringTok{"s"}\NormalTok{) }\OperatorTok{%>%}
1690\StringTok{ }\KeywordTok{footnote}\NormalTok{(}\DataTypeTok{general =} \StringTok{"Here is a very very very very very very very very very very very very very very very very very very very very long footnote"}\NormalTok{, }
1691 \DataTypeTok{threeparttable =}\NormalTok{ T)}
1692\end{Highlighting}
1693\end{Shaded}
1694
1695\begin{table}[t]
1696
1697\caption{\label{tab:unnamed-chunk-36}s}
1698\centering
1699\begin{threeparttable}
1700\begin{tabular}{lcccccc}
1701\toprule
1702 & mpg & cyl & disp & hp & drat & wt\\
1703\midrule
1704Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
1705Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
1706Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
1707Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
1708Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
1709\bottomrule
1710\end{tabular}
1711\begin{tablenotes}
1712\item \textit{Note: }
1713\item Here is a very very very very very very very very very very very very very very very very very very very very long footnote
1714\end{tablenotes}
1715\end{threeparttable}
1716\end{table}
1717
1718\hypertarget{latex-only-features}{%
1719\section{LaTeX Only Features}\label{latex-only-features}}
1720
1721\hypertarget{linebreak-processor}{%
1722\subsection{Linebreak processor}\label{linebreak-processor}}
1723
1724Unlike in HTML, where you can use \texttt{\textless{}br\textgreater{}}
1725at any time, in LaTeX, it's actually quite difficult to make a linebreak
1726in a table. Therefore I created the \texttt{linebreak} function to
1727facilitate this process. Please see the
1728\href{http://haozhu233.github.io/kableExtra/best_practice_for_newline_in_latex_table.pdf}{Best
1729Practice for Newline in LaTeX Table} for details.
1730
1731\begin{Shaded}
1732\begin{Highlighting}[]
1733\NormalTok{dt_lb <-}\StringTok{ }\KeywordTok{data.frame}\NormalTok{(}
1734 \DataTypeTok{Item =} \KeywordTok{c}\NormalTok{(}\StringTok{"Hello}\CharTok{\textbackslash{}n}\StringTok{World"}\NormalTok{, }\StringTok{"This}\CharTok{\textbackslash{}n}\StringTok{is a cat"}\NormalTok{), }
1735 \DataTypeTok{Value =} \KeywordTok{c}\NormalTok{(}\DecValTok{10}\NormalTok{, }\DecValTok{100}\NormalTok{)}
1736\NormalTok{)}
1737
1738\NormalTok{dt_lb }\OperatorTok{%>%}
1739\StringTok{ }\KeywordTok{mutate_all}\NormalTok{(linebreak) }\OperatorTok{%>%}
1740\StringTok{ }\KeywordTok{kable}\NormalTok{(}\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T, }\DataTypeTok{escape =}\NormalTok{ F,}
1741 \DataTypeTok{col.names =} \KeywordTok{linebreak}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{"Item}\CharTok{\textbackslash{}n}\StringTok{(Name)"}\NormalTok{, }\StringTok{"Value}\CharTok{\textbackslash{}n}\StringTok{(Number)"}\NormalTok{), }\DataTypeTok{align =} \StringTok{"c"}\NormalTok{))}
1742\end{Highlighting}
1743\end{Shaded}
1744
1745\begin{tabular}{lr}
1746\toprule
1747\makecell[c]{Item\\(Name)} & \makecell[c]{Value\\(Number)}\\
1748\midrule
1749\makecell[l]{Hello\\World} & 10\\
1750\makecell[l]{This\\is a cat} & 100\\
1751\bottomrule
1752\end{tabular}
1753
1754At the same time, since \texttt{kableExtra\ 0.8.0}, all
1755\texttt{kableExtra} functions that have some contents input (such as
1756\texttt{footnote} or \texttt{group\_rows}) will automatically convert
1757\texttt{\textbackslash{}n} to linebreaks for you in both LaTeX and HTML.
1758
1759\hypertarget{table-on-a-landscape-page}{%
1760\subsection{Table on a Landscape Page}\label{table-on-a-landscape-page}}
1761
1762Sometimes when we have a wide table, we want it to sit on a designated
1763landscape page. The new function \texttt{landscape()} can help you on
1764that. Unlike other functions, this little function only serves LaTeX and
1765doesn't have a HTML side.
1766
1767\begin{Shaded}
1768\begin{Highlighting}[]
1769\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{caption =} \StringTok{"Demo Table (Landscape)[note]"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1770\StringTok{ }\KeywordTok{kable_styling}\NormalTok{(}\DataTypeTok{latex_options =} \KeywordTok{c}\NormalTok{(}\StringTok{"hold_position"}\NormalTok{)) }\OperatorTok{%>%}
1771\StringTok{ }\KeywordTok{add_header_above}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{" "}\NormalTok{, }\StringTok{"Group 1[note]"}\NormalTok{ =}\StringTok{ }\DecValTok{3}\NormalTok{, }\StringTok{"Group 2[note]"}\NormalTok{ =}\StringTok{ }\DecValTok{3}\NormalTok{)) }\OperatorTok{%>%}
1772\StringTok{ }\KeywordTok{add_footnote}\NormalTok{(}\KeywordTok{c}\NormalTok{(}\StringTok{"This table is from mtcars"}\NormalTok{, }
1773 \StringTok{"Group 1 contains mpg, cyl and disp"}\NormalTok{, }
1774 \StringTok{"Group 2 contains hp, drat and wt"}\NormalTok{), }
1775 \DataTypeTok{notation =} \StringTok{"symbol"}\NormalTok{) }\OperatorTok{%>%}
1776\StringTok{ }\KeywordTok{group_rows}\NormalTok{(}\StringTok{"Group 1"}\NormalTok{, }\DecValTok{4}\NormalTok{, }\DecValTok{5}\NormalTok{) }\OperatorTok{%>%}
1777\StringTok{ }\KeywordTok{landscape}\NormalTok{()}
1778\end{Highlighting}
1779\end{Shaded}
1780
1781\begin{landscape}\begin{table}[!h]
1782
1783\caption{\label{tab:unnamed-chunk-38}Demo Table (Landscape)\textsuperscript{*}}
1784\centering
1785\begin{tabular}{lrrrrrr}
1786\toprule
1787\multicolumn{1}{c}{ } & \multicolumn{3}{c}{Group 1\textsuperscript{\dag}} & \multicolumn{3}{c}{Group 2\textsuperscript{\ddag}} \\
1788\cmidrule(l{3pt}r{3pt}){2-4} \cmidrule(l{3pt}r{3pt}){5-7}
1789 & mpg & cyl & disp & hp & drat & wt\\
1790\midrule
1791Mazda RX4 & 21.0 & 6 & 160 & 110 & 3.90 & 2.620\\
1792Mazda RX4 Wag & 21.0 & 6 & 160 & 110 & 3.90 & 2.875\\
1793Datsun 710 & 22.8 & 4 & 108 & 93 & 3.85 & 2.320\\
1794\addlinespace[0.3em]
1795\multicolumn{7}{l}{\textbf{Group 1}}\\
1796\hspace{1em}Hornet 4 Drive & 21.4 & 6 & 258 & 110 & 3.08 & 3.215\\
1797\hspace{1em}Hornet Sportabout & 18.7 & 8 & 360 & 175 & 3.15 & 3.440\\
1798\bottomrule
1799\multicolumn{7}{l}{\textsuperscript{*} This table is from mtcars}\\
1800\multicolumn{7}{l}{\textsuperscript{\dag} Group 1 contains mpg, cyl and disp}\\
1801\multicolumn{7}{l}{\textsuperscript{\ddag} Group 2 contains hp, drat and wt}\\
1802\end{tabular}
1803\end{table}
1804\end{landscape}
1805
1806\hypertarget{use-latex-table-in-html-or-word}{%
1807\subsection{Use LaTeX table in HTML or
1808Word}\label{use-latex-table-in-html-or-word}}
1809
1810If you want to include a LaTeX rendered table in your HTML or Word
1811document, or if you just want to save table as an image, you may
1812consider using \texttt{kable\_as\_image()}. Note that this feature
1813requires you to have \href{https://github.com/ropensci/magick}{magick}
1814installed (\texttt{install.packages("magick")}). Also, if you are
1815planning to use it on Windows, you need to install
1816\href{https://www.ghostscript.com/}{Ghostscript}. This feature may not
1817work if you are using tinytex. If you are using tinytex, please consider
1818using other alternatives to this function.
1819
1820\begin{Shaded}
1821\begin{Highlighting}[]
1822\CommentTok{# Not evaluated. }
1823
1824\CommentTok{# The code below will automatically include the image in the rmarkdown document}
1825\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1826\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%}
1827\StringTok{ }\KeywordTok{kable_as_image}\NormalTok{()}
1828
1829\CommentTok{# If you want to save the image locally, just provide a name}
1830\KeywordTok{kable}\NormalTok{(dt, }\StringTok{"latex"}\NormalTok{, }\DataTypeTok{booktabs =}\NormalTok{ T) }\OperatorTok{%>%}
1831\StringTok{ }\KeywordTok{column_spec}\NormalTok{(}\DecValTok{1}\NormalTok{, }\DataTypeTok{bold =}\NormalTok{ T) }\OperatorTok{%>%}
1832\StringTok{ }\KeywordTok{kable_as_image}\NormalTok{(}\StringTok{"my_latex_table"}\NormalTok{)}
1833\end{Highlighting}
1834\end{Shaded}
1835
1836\hypertarget{from-other-packages}{%
1837\section{From other packages}\label{from-other-packages}}
1838
1839Since the structure of \texttt{kable} is relatively simple, it shouldn't
1840be too difficult to convert HTML or LaTeX tables generated by other
1841packages to a \texttt{kable} object and then use \texttt{kableExtra} to
1842modify the outputs. If you are a package author, feel free to reach out
1843to me and we can collaborate.
1844
1845\hypertarget{tables}{%
1846\subsection{\texorpdfstring{\texttt{tables}}{tables}}\label{tables}}
1847
1848The latest version of
1849\href{https://CRAN.R-project.org/package=tables}{\texttt{tables}} comes
1850with a \texttt{toKable()} function, which is compatiable with functions
1851in \texttt{kableExtra} (\textgreater{}=0.9.0).
1852
1853
1854\end{document}