Caption not displaying table - knitr

When I use kable to create a table in my Rnw file, the table outputs as expected. However when I include the caption, the table ceases to output at all. Below is my latex and sample knitr chunk.
\documentclass[titlepage]{article}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{afterpage}
\usepackage{booktabs}
\usepackage{longtable}
\pagecolor{ForestGreen}\afterpage{\nopagecolor}
\color{white}
\title{%
\noindent\makebox[\linewidth]{\rule{9cm}{0.9pt}}
\huge \textbf{\textit{Key Distributor Report}}\\
\LARGE \vspace{2mm} \textbf{\textit{A detailed quarterly review}} \\
\noindent\makebox[\linewidth]{\rule{9cm}{0.9pt}}
\vspace{6mm} \LARGE Macdonald Corp.}
\pagenumbering{arabic}
\begin{document}
\maketitle
\color{black}
\newpage
\tableofcontents
\newpage
\section{Overview of Key Distributors}
\section{Company A}
\subsection{National Overview}
\subsubsection{Account base structure}
<<
tester, echo=FALSE, message=FALSE, warning=FALSE, results='asis',fig.height=3, fig.width=3, fig.align='center', eval=TRUE >>=
library(knitr)
kable(head(mtcars[,1:3]), booktabs=TRUE, caption='This is a caption')
#
\end{document}

This seems to be a problem of coloring the texts. If you try to select all content in the pdf you realize, that the table is actually there, but the text is white. So simply change the way of coloring the title page (use \color on \maketitle instead of \title):
\documentclass[titlepage]{article}
\usepackage{graphicx}
\usepackage[dvipsnames]{xcolor}
\usepackage{afterpage}
\usepackage{booktabs}
\usepackage{longtable}
\pagecolor{ForestGreen}\afterpage{\nopagecolor}
\title{%
\noindent\makebox[\linewidth]{\rule{9cm}{0.9pt}}
\huge \textbf{\textit{Key Distributor Report}}\\
\LARGE \vspace{2mm} \textbf{\textit{A detailed quarterly review}} \\
\noindent\makebox[\linewidth]{\rule{9cm}{0.9pt}}
\vspace{6mm} \LARGE Macdonald Corp.}
\pagenumbering{arabic}
\begin{document}
{\color{white}%
\maketitle
}
\color{black}
\newpage
\tableofcontents
\newpage
\section{Overview of Key Distributors}
\section{Company A}
\subsection{National Overview}
\subsubsection{Account base structure}
<<tester, echo=FALSE, message=FALSE, warning=FALSE,results='asis',fig.height=3, fig.width=3, fig.align='center', eval=TRUE >>=
library(knitr)
library(kableExtra)
library(dplyr)
kable(head(mtcars[,1:3]), booktabs=T, caption='This is a caption') %>%
kable_styling(latex_options = "hold_position")
#
\end{document}

Related

Package inputenc Error: Unicode character 하 (U+D558) (inputenc) not set up for use with LaTeX. (Automating Unicode Input)

I have read all the related answers around this issue and for most people using \DeclareUnicodeCharacter{}{} works for them. However, I'm generating my TEX file from an input file and I don't know the Unicode characters/words which I'm going to be given in advance, so it would be difficult to use the DeclareUnicodeCharacter declaration.
I'm using pdflatex which some StackExchange answers said wouldn't be as useful for Unicode characters, so I also tried xetex and luatex, but these didn't output as expected. Any suggestions how I could modify my script to output a tex file so that pdflatex will not barf on the Unicode characters? Or one which would be acceptable to xetex or luatex?
I've done simple pdflatex stuff but never used Unicode before.
I'm attempting to modify a script I found on github which generates bingo cards. The original program only dealt with English words in the "squares" input file.
I've added one line to the script in order to get it to see Unicode.
\usepackage[utf8]{inputenc}
the bash script:
#!/usr/bin/bash
## text file which contains bingo squares
input_text=squares
## latex code for center square
center_square='\includegraphics[width=0.9\textwidth]{center.png}'
#center_square=FREE
node=( AA AB AC AD AE BA BB BC BD BE CA CB CD CE DA DB DC DD DE EA EB EC ED EE )
cat > bingo.tex << EOF
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{xstring}
\usepackage[none]{hyphenat}
\usepackage{tikz}
\usetikzlibrary{calc}
\renewcommand{\familydefault}{\sfdefault}
\newcommand{\Size}{3cm}% Adjust size of square as desired
\def\NumOfColumns{5}%
\def\Sequence{1/A, 2/B, 3/C, 4/D, 5/E}% This needs to match \NumOfColumns
\tikzset{Square/.style={
inner sep=0pt,
text width=\Size,
minimum size=\Size,
draw=black,
align=center
}
}
EOF
i=0
OLDIFS=$IFS
IFS=$'\n'
for line in $(sort -R $input_text | head -n 24)
do
echo "\newcommand{\Node${node[i]}}{$line}%" >> bingo.tex
(( i++ ))
done
IFS=$OLDIFS
echo "\newcommand{\NodeCC}{$center_square}%" >> bingo.tex
cat >> bingo.tex << EOF
\pagestyle{empty}
\begin{document}
\centering
% Optional Title
\vspace*{\fill}
\begin{center}
\begin{tikzpicture}[draw=black, ultra thick, x=\Size,y=\Size]
\node at (0.5,-0.8) {\Huge B};
\node at (1.5,-0.8) {\Huge I};
\node at (2.5,-0.8) {\Huge N};
\node at (3.5,-0.8) {\Huge G};
\node at (4.5,-0.8) {\Huge O};
\foreach \col/\colLetter in \Sequence {%
\foreach \row/\rowLetter in \Sequence{%
\pgfmathtruncatemacro{\value}{\col+\NumOfColumns*(\row-1)}
\def\NodeText{\expandafter\csname Node\rowLetter\colLetter\endcsname}
\node [Square] at (\$(\col,-\row)-(0.5,0.5)\$) {\NodeText};
}
}
\end{tikzpicture}
\end{center}
\vspace*{\fill}
\end{document}
EOF
pdflatex bingo.tex
#luatex bingo.tex
#xetex bingo.tex
which generates the following bingo.tex file:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{xstring}
\usepackage[none]{hyphenat}
\usepackage{tikz}
\usetikzlibrary{calc}
\renewcommand{\familydefault}{\sfdefault}
\newcommand{\Size}{3cm}% Adjust size of square as desired
\def\NumOfColumns{5}%
\def\Sequence{1/A, 2/B, 3/C, 4/D, 5/E}% This needs to match \NumOfColumns
\tikzset{Square/.style={
inner sep=0pt,
text width=\Size,
minimum size=\Size,
draw=black,
align=center
}
}
\newcommand{\NodeAA}{주다}%
\newcommand{\NodeAB}{되다}%
\newcommand{\NodeAC}{않다}%
\newcommand{\NodeAD}{것}%
\newcommand{\NodeAE}{보다}%
\newcommand{\NodeBA}{사람}%
\newcommand{\NodeBB}{거}%
\newcommand{\NodeBC}{없다}%
\newcommand{\NodeBD}{나}%
\newcommand{\NodeBE}{아니다}%
\newcommand{\NodeCA}{하다}%
\newcommand{\NodeCB}{그}%
\newcommand{\NodeCD}{수}%
\newcommand{\NodeCE}{이}%
\newcommand{\NodeDA}{있다}%
\newcommand{\NodeDB}{때}%
\newcommand{\NodeDC}{가다}%
\newcommand{\NodeDD}{우리}%
\newcommand{\NodeDE}{같다}%
\newcommand{\NodeCC}{\includegraphics[width=0.9\textwidth]{center.png}}%
\pagestyle{empty}
\begin{document}
\centering
% Optional Title
\vspace*{\fill}
\begin{center}
\begin{tikzpicture}[draw=black, ultra thick, x=\Size,y=\Size]
\node at (0.5,-0.8) {\Huge B};
\node at (1.5,-0.8) {\Huge I};
\node at (2.5,-0.8) {\Huge N};
\node at (3.5,-0.8) {\Huge G};
\node at (4.5,-0.8) {\Huge O};
\foreach \col/\colLetter in \Sequence {%
\foreach \row/\rowLetter in \Sequence{%
\pgfmathtruncatemacro{\value}{\col+\NumOfColumns*(\row-1)}
\def\NodeText{\expandafter\csname Node\rowLetter\colLetter\endcsname}
\node [Square] at ($(\col,-\row)-(0.5,0.5)$) {\NodeText};
}
}
\end{tikzpicture}
\end{center}
\vspace*{\fill}
\end{document}
You can see it output:
\newcommand{\NodeAA}{주다}%
as unicode to be input into the square, but pdflatex doesn't like it.
If you compile with lualatex or xelatex, you'll just have to find a font which has all your glyphs. In the example below, I'm using Arial Unicode MS.
To see which fonts you can use for your glyphes, you can use the command line tool albatross, see https://stackoverflow.com/a/69721465/2777074 for an example how to use it.
% !TeX TS-program = lualatex
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper,margin=2cm]{geometry}
\usepackage{xstring}
\usepackage[none]{hyphenat}
\usepackage{tikz}
\usetikzlibrary{calc}
\renewcommand{\familydefault}{\sfdefault}
\newcommand{\Size}{3cm}% Adjust size of square as desired
\def\NumOfColumns{5}%
\def\Sequence{1/A, 2/B, 3/C, 4/D, 5/E}% This needs to match \NumOfColumns
\tikzset{Square/.style={
inner sep=0pt,
text width=\Size,
minimum size=\Size,
draw=black,
align=center
}
}
\newcommand{\NodeAA}{주다}%
\newcommand{\NodeAB}{되다}%
\newcommand{\NodeAC}{않다}%
\newcommand{\NodeAD}{것}%
\newcommand{\NodeAE}{보다}%
\newcommand{\NodeBA}{사람}%
\newcommand{\NodeBB}{거}%
\newcommand{\NodeBC}{없다}%
\newcommand{\NodeBD}{나}%
\newcommand{\NodeBE}{아니다}%
\newcommand{\NodeCA}{하다}%
\newcommand{\NodeCB}{그}%
\newcommand{\NodeCD}{수}%
\newcommand{\NodeCE}{이}%
\newcommand{\NodeDA}{있다}%
\newcommand{\NodeDB}{때}%
\newcommand{\NodeDC}{가다}%
\newcommand{\NodeDD}{우리}%
\newcommand{\NodeDE}{같다}%
\newcommand{\NodeCC}{\includegraphics[width=0.9\textwidth]{example-image-duck}}%
\pagestyle{empty}
\usepackage{fontspec}
\setsansfont{Arial Unicode MS}
\begin{document}
\centering
% Optional Title
\vspace*{\fill}
\begin{center}
\begin{tikzpicture}[draw=black, ultra thick, x=\Size,y=\Size]
\node at (0.5,-0.8) {\Huge B};
\node at (1.5,-0.8) {\Huge I};
\node at (2.5,-0.8) {\Huge N};
\node at (3.5,-0.8) {\Huge G};
\node at (4.5,-0.8) {\Huge O};
\foreach \col/\colLetter in \Sequence {%
\foreach \row/\rowLetter in \Sequence{%
\pgfmathtruncatemacro{\value}{\col+\NumOfColumns*(\row-1)}
\def\NodeText{\expandafter\csname Node\rowLetter\colLetter\endcsname}
\node [Square] at ($(\col,-\row)-(0.5,0.5)$) {\NodeText};
}
}
\end{tikzpicture}
\end{center}
\vspace*{\fill}
\end{document}

Multicolumns tables in LaTeX writing are not in correct order

I am writing an article and the template is in LaTeX, and I need to insert a multicolumn table, the problem is, I have knowledgement just with the basics about LaTeX writing and i am finding some trouble with the organisation of the columns.
This is how the table should look like (image inside Word doc): Word
And this is how the code that i am using is being compiled inside LaTeX: LaTeX
Here is the code:
\begin{table*}[!htb]
\centering%
\caption{Parâmetros obtidos para o ajuste utilizando o modelo de Langmuir.}%
\label{tab:resul}
\centering%
\begin{tabular}{c c c c}
\hline
Modelo de Langmuir:
\begin{minipage}{7cm}{\begin{equation}\label{eq:Uxy}
\frac{\\Qmax\ K_L Ceq}{\\1\ +K_L Ceq} = Qe
\end{equation}}
\end{minipage} \\
\cline{1-4}
\multicolumn{2}{c|}{\textbf{HDL Calc}} & \multicolumn{2}{c}{\textbf{HDL SDS Calc}} \\
\hline
Qmax & K(L/mg) & Qmax(mg/g) & K(L/mg)\\
\hline
690,6 & 0,011 & 132,3 & 0,136\\
\bottomrule
\addlinespace
\end{tabular}
\fonte{Autoria própria (2021).}
\end{table*}
Any help with the code?

LaTeX math expressions in Knitr / RMarkdown (Beamer)

I am having difficulty getting LaTeX math expressions to properly render when using knitr and kableExtra to compile a table in a Beamer presentation. I was originally use xelatex as my engine, and then switched to pdflatex to see if this would resolve the issue. I'm stumped on this one.
TeX preamble
% For resizing caption font size
\usepackage{caption}
\captionsetup[figure]{font=tiny}
% Other packages
\usepackage{fancyvrb}
\usepackage{color,xcolor}
\usepackage{framed}
\usepackage{booktabs}
\usepackage{longtable}
\usepackage{array}
\usepackage{multirow}
\usepackage{wrapfig}
\usepackage{float}
\usepackage{colortbl}
\usepackage{pdflscape}
\usepackage{tabu}
\usepackage{threeparttable}
\usepackage{threeparttablex}
\usepackage[normalem]{ulem}
\usepackage{makecell}
% Colour formatting
\definecolor{UBCblue}{rgb}{0.04706, 0.13725, 0.26667} % UBC Blue (primary)
\definecolor{UBCgrey}{rgb}{0.3686, 0.5255, 0.6235} % UBC Grey (secondary)
\setbeamercolor{title}{fg=UBCblue} % slide title
\setbeamercolor{frametitle}{fg=UBCblue} % slide title
\setbeamercolor{structure}{fg=darkgray} % more list stuff, I think
\setbeamertemplate{itemize items}[circle] % for list itemes
% Page numbers
\setbeamertemplate{navigation symbols}{}
\setbeamertemplate{footline}[page number]
% Margins
\setbeamersize{text margin left=8mm,text margin right=8mm}
% make code-output smaller
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{fontsize=\footnotesize,commandchars=\\\{\}}
% make console-output smaller:
\makeatletter
\def\verbatim{\footnotesize\#verbatim \frenchspacing\#vobeyspaces \#xverbatim}
\makeatother
\setlength{\parskip}{0pt}
\setlength{\OuterFrameSep}{-4pt}
\makeatletter
\preto{\#verbatim}{\topsep=-10pt \partopsep=-10pt }
\makeatother
% For better spacing between the list bullets
\renewcommand{\tightlist}{\setlength{\itemsep}{1ex}\setlength{\parskip}{0pt}}
RMarkdown Document
---
title: "My Title"
subtitle: "My Subtitle"
author: Speleo4Life
date: "March 11th, 2021"
output:
beamer_presentation:
latex_engine: pdflatex
includes:
in_header: preamble.tex
theme: "default"
colortheme: "orchid"
fonttheme: "serif"
slide_level: 2
keep_tex: true
urlcolor: blue
linkcolor: purple
---
```{r setup, eval=TRUE, include=FALSE, echo=FALSE}
library(tidyverse)
library(ggplot2)
library(kableExtra)
library(psych)
```
```{r, eval=TRUE, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(knitr.table.format = "latex")
```
## Review: Null Hypothesis Significance Testing
```{r, echo=FALSE}
c1 <- c('$\\text{Reject } H_0$', '$\\text{Retain } H_0$')
c2 <- c('$\\text{Type I Error } (\\alpha)$', 'Correct Retention')
c3 <- c('$\\text{Power } (1-\\beta)$', '$\\text{Type II Error } \\beta$')
df <- data.frame(c1,c2,c3)
colnames(df) <- c('$\\textbf{Decision}$', '$\\mathbf{H_0} \\textbf{\\textit{is true}}$',
'$\\mathbf{H_0} \\textbf{\\textit{is false}}$')
kbl(df, booktabs = T) %>%
add_header_above(c(" ", "State of Reality" = 2))
```
Relevant Output
Just add escape = F to kbl:
---
title: "My Title"
subtitle: "My Subtitle"
author: Speleo4Life
date: "March 11th, 2021"
output:
beamer_presentation:
latex_engine: pdflatex
includes:
in_header: preamble.tex
theme: "default"
colortheme: "orchid"
fonttheme: "serif"
slide_level: 2
keep_tex: true
urlcolor: blue
linkcolor: purple
---
```{r setup, eval=TRUE, include=FALSE, echo=FALSE}
library(tidyverse)
library(ggplot2)
library(kableExtra)
library(psych)
```
```{r, eval=TRUE, echo=FALSE}
knitr::opts_chunk$set(echo = TRUE)
options(knitr.table.format = "latex")
```
## Review: Null Hypothesis Significance Testing
```{r, echo=FALSE}
c1 <- c('$\\text{Reject } H_0$', '$\\text{Retain } H_0$')
c2 <- c('$\\text{Type I Error } (\\alpha)$', 'Correct Retention')
c3 <- c('$\\text{Power } (1-\\beta)$', '$\\text{Type II Error } \\beta$')
df <- data.frame(c1,c2,c3)
colnames(df) <- c('$\\textbf{Decision}$', '$\\mathbf{H_0} \\textbf{\\textit{is true}}$',
'$\\mathbf{H_0} \\textbf{\\textit{is false}}$')
kbl(df, booktabs = T, escape = F) %>%
add_header_above(c(" ", "State of Reality" = 2))
```
-output

htmlTable in Rmd - conversion to Word docx

I have the following Rmd file, which produces an html file, which I then copy-paste into a docx file (for collaborators). Here are things I'd like to know how to do with the tables, but I can't find answers in the vignettes here:
A. I want to know how to remove the blank column that gets inserted in Word in between Cgroup 1 and Cgroup 2.
B. I want to know how to set the width of the column with the row names ("1st row",...)
C. How can I change the font and font size? I tried following this but it doesn't work to have output: word_document with htmlTable()
D. To ease the conversion to Word, is there a way to specify page breaks? Landscape orientation?
Thank you so much!
---
title: "Example"
output:
Gmisc::docx_document:
fig_caption: TRUE
force_captions: TRUE
---
Results
=======
```{r, echo = FALSE}
library(htmlTable)
library(Gmisc)
library(knitr)
mx <-
matrix(ncol=6, nrow=8)
rownames(mx) <- paste(c("1st", "2nd",
"3rd",
paste0(4:8, "th")),
"row")
colnames(mx) <- paste(c("1st", "2nd",
"3rd",
paste0(4:6, "th")),
"hdr")
for (nr in 1:nrow(mx)){
for (nc in 1:ncol(mx)){
mx[nr, nc] <-
paste0(nr, ":", nc)
}
}
htmlTable(mx,
cgroup = c("Cgroup 1", "Cgroup 2"),
n.cgroup = c(2,4))
```
The styling seemed to be off for the row names and it is now fixed in version 1.10.1 that you can download using the devtools package: devtools::install_github("gforge/htmlTable", ref="develop")
Regarding the styling the function allows almost any CSS-style you could image. Unfortunately it requires copy-pasting into Word and this functionality hasn't been Microsofts highest priority. You can easily adapt you example to accomodate the requiered changes using the css.cell:
library(htmlTable)
library(knitr)
mx <-
matrix(ncol=6, nrow=8)
rownames(mx) <- paste(c("1st", "2nd",
"3rd",
paste0(4:8, "th")),
"row")
colnames(mx) <- paste(c("1st", "2nd",
"3rd",
paste0(4:6, "th")),
"hdr")
for (nr in 1:nrow(mx)){
for (nc in 1:ncol(mx)){
mx[nr, nc] <-
paste0(nr, ":", nc)
}
}
css.cell = rep("font-size: 1.5em;", times = ncol(mx) + 1)
css.cell[1] = "width: 4cm; font-size: 2em;"
htmlTable(mx,
css.cell=css.cell,
css.cgroup = "color: red",
css.table = "color: blue",
cgroup = c("Cgroup 1", "Cgroup 2"),
n.cgroup = c(2,4))
There is no way to remove the empty column generated by cgroups. This was required for the table to look nice and is a conscious design choice.
Regarding page-breaks I doubt there is any elegant way for doing that. An alternative could possibly be the ReporteRs package. I haven't used it myself but it's closer integrated with Word and could possibly be a solution.

Org mode: describe a chess position and automatically generate image of chessboard

I write articles on chess. I often need to describe a chess position, using a standard format named FEN, and would like it to be automatically converted to a png image, and when I export the org document as LaTeX or html the image to be inlined. I think it can be done because ditaa seems to work similarly.
For example, with the FEN string "8/pppr1kpp/8/8/8/5P2/PPP1RKPP/8 w - - 0 1" as input, I create a tex file named chessboard.tex:
\documentclass[border={0 0 3 0}, convert={density=150}]{standalone}
\usepackage{xskak}
\usepackage{chessboard}
\usepackage{chessfss}
\usepackage{fontspec}
\begin{document}
\setchessboard{normalboard, showmover=true, moverstyle=triangle, label=false}
\setboardfontfamily{merida}
\fenboard{8/pppr1kpp/8/8/8/5P2/PPP1RKPP/8 w - - 0 1} \chessboard
\end{document}
which, with the shell command $> xelatex --shell-escape chessboard.tex, produces the following png image (thanks to the use of the standalone package).
How can I automate this process, so that I could put in an org file something like
#+BEGIN_SRC chessfen
8/pppr1kpp/8/8/8/5P2/PPP1RKPP/8 w - - 0 1
#+END_SRC
and have the above process automated?
Something like this?
#+title: Chess
#+startup: inlineimages
#+property: header-args:latex+ :imagemagick yes :iminoptions -density 300 -resize 400
#+property: header-args:latex+ :headers '("\\usepackage{chessboard}\\usepackage{xskak}\\usepackage{chessfss}") :border 1pt
* Chess
#+begin_src latex :file chess.png :results raw
\setchessboard{normalboard, showmover=true, moverstyle=triangle, label=false}
\fenboard{8/pppr1kpp/8/8/8/5P2/PPP1RKPP/8 w - - 0 1} \chessboard
#+end_src
This modification of rvf0068's answer works for me, after manually installing the necessary LaTeX packages (that can be found by inspecting in the error log output):
#+property: header-args:latex+ :headers '("\\usepackage{chessboard}\\usepackage{xskak}\\usepackage{chessfss}") :border 1pt
#+header: :imagemagick "t"
#+header: :results file raw
#+header: :exports results
#+header: :fit yes :border 0.5cm
#+header: :iminoptions -density 600 :imoutoptions -geometry 600
#+begin_src latex :file alburt_vs_kasparov_1978.png
\setchessboard{normalboard, showmover=false, moverstyle=triangle, label=false}
\fenboard{8/pppr1kpp/8/8/8/5P2/PPP1RKPP/8 w - - 0 1} \chessboard
#+end_src
# Local Variables:
# org-latex-pdf-process: ("xelatex -shell-escape -interaction=nonstopmode -output-directory=%o %f"
# "xelatex -shell-escape -interaction=nonstopmode -output-directory=%o %f"
# "xelatex -shell-escape -interaction=nonstopmode -output-directory=%o %f")
# End:
I use LaTeX environments directly to typeset chess, with no src blocks:
Then, the white brings the king
- to the closest rank to the opposing king and
- to the file one next to that of the opposing king toward the center.
\begin{center}
\fenboard{7k/R7/8/8/8/8/8/7K w - - 0 2}
\mainline{2. Kg2 Kf8 3. Kf3 Ke8 4. Ke4 Kd8 5. Kd5 Kc8 6. Kd6}
\par
\showboard
\end{center}