Word output issues using R Markdown - ms-word

I was trying to output a Word document using APA bibliography style. When I compiled my document in order to output a PDF, there was no problem at all: citations were listed correctly in the references section. It was the same with HTML output. On the other hand, when I compiled my document using Word output, my references were not listed correctly... It was my debut. This is a correct version of my MWE:
This is a reference bib I use:
#Book{Assoun1981,
author = {Assoun, Paul-Laurent},
title = {Introduction à l'épistémologie freudienne},
publisher = {Éditions Payot},
address = {Paris},
year = {1981}, }
And this is my MWE:
---
title: "A title"
output:
pdf_document:
latex_engine: xelatex
citation_package: biblatex
toc: yes
toc_depth: 4
number_sections: yes
html_document:
toc: yes
toc_depth: 4
df_print: paged
word_document:
toc: yes
toc_depth: 4
date: "August 2022"
bibliography: YourLibraryName.bib
fontsize: 12pt
geometry: left=4cm,right=4cm,top=4cm,bottom=4cm
linestretch: 1.5
toc-title: Plan
links-as-notes: yes
link-citations: yes
header-includes:
- \usepackage{fontspec}
- \setmainfont[Numbers=OldStyle,Mapping=tex-text]{Janson Text LT Std}
- \usepackage{fancyhdr}
- \pagestyle{headings}
- \fancyfoot[LE,RO]{\thepage}
- \usepackage [french]{babel}
- \usepackage[backend=biber,style=apa]{biblatex}
- \DeclareLanguageMapping{french}{french-apa}
- \DefineBibliographyExtras{french}{\restorecommand\mkbibnamefamily}
---
# Introduction
[#Assoun1981]
# Références

I edited my MWE and added a correct version.

Related

Using tikz graphdrawing library within RMarkdown ... Need to use lualatex engine, but can't get it to work

I have the following code in an rmd file which leverages tikz for diagrams:
---
title: "TestNonTufteLua"
author: "Me"
output:
pdf_document :
latex_engine: lualatex
---
Prove tikz works:
```{r tikTest1, engine = "tikz"}
\usetikzlibrary{shapes}
\begin{tikzpicture}
\node[ellipse, draw=black, align = center] (Data) {Data $y_{n}$};
\end{tikzpicture}
```
Then, when you set `eval = TRUE` in the below code, it will not work.
```{r tikTest2, eval = FALSE, engine = "tikz"}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
% A and F are horizontally aligned if you also set weight=0.5 for A -- C.
A -- [minimum layers=2] C -- F,
{ [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
```
When changing to eval=TRUE in the second chunk, I get the following
error:
Quitting from lines 24-29 (testNonTufteLua.Rmd) Error: running
'texi2dvi' on '.\tikz36747a021b22.tex' failed
LaTeX errors: rarygraphdrawing.code.tex:22: Package pgf Error: You
need to run LuaTeX to use the graph drawing library.
This error occurs when using the knit button from RStudio or using render("testNonTufteLua.Rmd", output_format = pdf_document(keep_tex = TRUE, latex_engine = "lualatex"). I have also experimented with setting options(tikzDefaultEngine = "luatex") to get tikzDevice to handle it properly, but it still does not work. I just can't seem to get the graphdrawing library to work even though the tikz-shapes library can be loaded and also that the rest of the document seems to be compiled with lualatex. Thanks for any help!!
Update: Meanwhile knitr no longer uses tools::texi2dvi but tinytex::latexmk. One therefore has to use options(tinytex.engine = 'lualatex') in a set-up chunk.
This is rather tricky, since you are not using tikzDevice but the tikz engine, which uses tools::texi2dvi to convert to PDF. You can change this using options(texi2dvi = "lualatex"). However, the default template does not work with LuaLaTeX. I have therefore created a modified template:
\RequirePackage{luatex85}
\documentclass{article}
\usepackage[luatex,active,tightpage]{preview}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{preview}
%% TIKZ_CODE %%
\end{preview}
\end{document}
And specify that file with engine.opts = list(template = "tikz2pdf.tex"). Putting it all together here my working file:
---
title: "TestNonTufteLua"
author: "Me"
output:
pdf_document :
latex_engine: lualatex
---
```{r}
options(texi2dvi = "lualatex")
```
```{r tikTest2, eval = TRUE, engine = "tikz", engine.opts = list(template = "tikz2pdf.tex")}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
% A and F are horizontally aligned if you also set weight=0.5 for A -- C.
A -- [minimum layers=2] C -- F,
{ [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
```
Result:
References:
how to set engine options
preview and LuaLaTeX
knitr using texi2pdf
A small running variation of the example above is the following using tinytex.
---
title: "lualatex. Using `tinytex.engine`"
output:
html_document:
df_print: paged
pdf_document:
latex_engine: lualatex
---
## Latex engines
By default, PDF documents are rendered using `pdflatex`. You can specify an
alternate engine using the `latex_engine` option. Available engines
are `pdflatex`, `xelatex`, and `lualatex.`
```{r setup}
options(tinytex.engine = "lualatex")
```
```{r tikzLua, eval = TRUE, engine = "tikz", engine.opts = list(template = "tikz2pdf.tex")}
\usetikzlibrary{graphs, graphdrawing}
\usegdlibrary{layered}
\tikz [gr/.style={gray!50}, font=\bfseries]
\graph [layered layout] {
% A and F are horizontally aligned if you also set weight=0.5 for A -- C.
A -- [minimum layers=2] C -- F,
{ [nodes=gr, edges=gr] A -- B -- { E, D -- F } }
};
```
After an update in knitr the example above stopped running.

Caption not displaying table

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}

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}

The equations doesn't display in a word(*.docx) document that converted from a markdown file using knitr

Environment: win7 64bit; pandoc; RStudio; knitr
Microsoft Office Word 2007
Code:
<!--pandoc
t: html
s:
mathjax:
number-sections:
bibliography: foo.bib
o: output.html
-->
---
title: "test06"
author: "Franklin"
date: "Tuesday, June 24, 2014"
output: word_document
---
Some mathematics:
$$
e^{(i\pi)}+1 = 0
$$
Inline test $\sin(x)$.
I typed the commands in the Console of RStudio as below,
pandoc('test05.md', format='docx')
While the equations in the word document don't display at all.