Incosistency between RMardkown preview and pdf output - r

I have this latex code in an RMarkdown file in R studio:
---
title: "matrix"
date: "November 2, 2016"
output:
pdf_document: default
---
$$
\mathbf{y} = \left[\begin{array}
{rrr}
y_{1,1} \\
y_{4,3}
\end{array}\right]
\mathbf{X} = \left[\begin{array}
{rrr}
1 & 0 & 0 & 0 \\
1 & 0 & 0 & 0
\end{array}\right]
$$
The preview shows up fine in Rstudio:
But when I knit to pdf I get this error message:
! LaTeX Error: \mathbf allowed only in math mode.
See the LaTeX manual or LaTeX Companion for explanation.
Type H <return> for immediate help.
...
l.88 \$\$ \mathbf{y}
pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43
Execution halted
How can I fix it?

Works okay if you change to an explicit math environment:
---
title: "matrix"
date: "November 2, 2016"
output:
pdf_document: default
---
\begin{equation}
\mathbf{y} = \left[\begin{array}
{r}
y_{1,1} \\
y_{4,3}
\end{array}\right]
\mathbf{X} = \left[\begin{array}
{rrrr}
1 & 0 & 0 & 0 \\
1 & 0 & 0 & 0
\end{array}\right]
\end{equation}
except you also want to fix your array alignments, otherwise
Extra alignment tab has been changed to \cr.
(you need just {r} for the first, and {rrrr} for the second)

Related

Unwanted sanitizing (escaping) in knitr due to update

Last Spring, I used code similar to my minimum working example (MWE) below to make this: https://cdn.advocacy.sba.gov/wp-content/uploads/2019/04/23142650/2019-Small-Business-Profiles-MA.pdf
According to the knitr documentation, the table code should not get sanitized. If you knit the MWE however, you will see that several characters do get sanitized (escaped). For example, \ is sometimes turned into \textbackslash{}. So, it seems that some package update has changed when code gets sanitized. If you simply copy the RMD table code from below and paste it into the TeX output, it compiles fine.
I have tried reverting back to many of last year's packages, yet to no avail.
---
title: "MWE"
output:
pdf_document:
keep_tex: yes
---
\begin{tabular}{#{}lll}
\begin{tabular}{ll}
A & B \\
A & B \\
\end{tabular}
&
\begin{tabular}{ll}
A & B \\
A & B \\
\end{tabular}
&
\begin{tabular}{ll}
A & B \\
A & B \\
\end{tabular}
\end{tabular}
*[In my original code it was part of an "asis" chunk. You can try this, and find the same problem.]
This is a known problem and the solution is to add raw attributes to pandoc, which ensures that content inside the block is not escaped.
1) Add md_extensions: +raw_attribute
2) Add a latex block
For your examples this works as follows:
---
title: "MWE"
output:
pdf_document:
md_extensions: +raw_attribute
keep_tex: yes
---
```{=latex}
\begin{tabular}{#{}lll}
\begin{tabular}{ll}
A & B \\
A & B \\
\end{tabular}
&
\begin{tabular}{ll}
A & B \\
A & B \\
\end{tabular}
&
\begin{tabular}{ll}
A & B \\
A & B \\
\end{tabular}
\end{tabular}
```
or when using it inside a code chunk:
```{r, results = "asis"}
cat("```{=latex}")
// your latex code
cat("```")
```
P.S.: pandoc version 2.0.0 is needed for this feature

rmarkdown matrices not functioning as expected or seen in rstudio

I am trying create a blogdown post using Rmarkdown but the matrix I am trying to create is not giving the correct output. In Rstudio it looks like the image below, however when I run the rmarkdown I get a different result.
Gives output in blogdown:
Rmarkdown code:
$$
\left(\begin{array}{cccc}
\text{var } & ... & -1 \\
\text{cov } & ... & ...\\
\text{mat } & ... & -1 \\
... & ... & -1\\
1 & ... & 0
\end{array}\right)
\left(\begin{array}{c}
w_{1} \\
... \\
... \\
... \\
w_{k} \\
\lambda
\end{array}\right)
=
\left(\begin{array}{c}
0 \\
... \\
... \\
... \\
0 \\
1
\end{array}\right)
$$
Any ideas on where I am going wrong and how I can make a matrix equation be on the same line as in the RStudio output?
The empty lines are breaking things. From https://tex.stackexchange.com/q/230338
An empty line in TeX should never be used just to arrange the source. It is an instruction exactly the same as the command \par which ends a paragraph and starts TeX's line breaking algorithm to split a paragraph into lines.
So it is not allowed in display math and it should never be used on the line before display math either, in that position it does not generate an error but forces TeX to take recovery action that produces a spurious white paragraph on the line above the display.
David Carlisle, Feb 26 '15 at 21:29
While it might not seem obvious that a LaTeX limitation would impose itself on an HTML target, the rendering uses MathJax (javascript library) that renders LaTeX math mode.
If you remove them, it seems to render correctly.
---
title: Quux
output: html_document
---
$$
\left(\begin{array}{cccc}
\text{var } & ... & -1 \\
\text{cov } & ... & ...\\
\text{mat } & ... & -1 \\
... & ... & -1\\
1 & ... & 0
\end{array}\right)
\left(\begin{array}{c}
w_{1} \\
... \\
... \\
... \\
w_{k} \\
\lambda
\end{array}\right)
=
\left(\begin{array}{c}
0 \\
... \\
... \\
... \\
0 \\
1
\end{array}\right)
$$
It also works with output: pdf_document:
R.version
# _
# platform x86_64-w64-mingw32
# arch x86_64
# os mingw32
# system x86_64, mingw32
# status
# major 3
# minor 5.3
# year 2019
# month 03
# day 11
# svn rev 76217
# language R
# version.string R version 3.5.3 (2019-03-11)
# nickname Great Truth
packageVersion("knitr")
# [1] '1.23'
packageVersion("rmarkdown")
# [1] '1.13'

! Illegal unit of measure (pt inserted) PDF Knit Error

I'm trying to knit a report as a PDF in RStudio. The code I have so far works fine (I've been testing it chunk by chunk) until it gets to the below chunk:
## HCT-CI
```{r}
summaryTable(y=wdata$HCTCI.score, table.title="HCT-CI Raw Score", y.labels="Number of Readmissions")
summaryTable(y=wdata$HCTCI.score, table.title="Readmissions", freq.tab=T, y.name="HCT-CI Raw Score")
summaryTable(y=wdata$HCTCI.3cat, table.title="HCT-CI Score (3 Groups)", caption_heading="Note:", caption="1: Score=0; 2: Score=1-2; 3: Score=3+", freq.tab=T, y.labels=c("0", "1-2", "3+"))
```
\pagebreak
My goal is to generate the output successfully as a PDF.
It's the same functions and format I've used in all the other chunks. When I run the chunk by itself, it also works. I've looked up the issue on google and other stackoverflow posts, but am having a hard time applying their solutions as they all use HTML or a different language.
My full report looks something like this (with some chunks taken out for the sake of brevity):
DOCUMENT BEGINS
---
title:
- \vspace{2in}Analysis for Study$\colon$ Readmissions
subtitle:
- \vspace{.2in}\emph{`r format(Sys.time(), '%d %B, %Y')`}
author:
- \emph{PI$\colon$ Name}
- \emph{College}
- \vspace{.5in}\emph{Author}
- \emph{Position}
- \emph{email}
date:
- \vspace{4in}
header-includes:
- \usepackage{fancyhdr}
- \usepackage{titling}
- \usepackage{caption}
- \fancyhead[L]{\includegraphics[width=2cm]{pic.png}}
- \fancyhead[R]{\includegraphics[width=4cm]{pic.png}}
- \fancyfoot[C]{Department}
- \fancypagestyle{plain}{\pagestyle{fancy}}
geometry: left=.5in,right=.5in,top=1in,bottom=1in
mainfont: Times New Roman
output:
pdf_document:
keep_tex: true
highlight: tango
toc: true
toc_depth: 4
number_sections: true
fig_caption: true
df_print: kable
fontsize: 12pt
---
\captionsetup[table]{labelformat=empty}
```{r setup, include=FALSE}
source("Revised Code.R")
knitr::opts_chunk$set(fig.path='Figs/', fig.align='center', fig.pos="H", echo=FALSE, warning=FALSE, message=FALSE, results = 'asis', fig.width = 6.5, fig.height = 4.5, dev = 'pdf')
\pagebreak
Summary of ___ within 100 days
summaryTable(wdata, table.title="Within 100 Days", y.labels="Number of Readmissions")
summaryTable(wdata, table.title="Within 100 Days", y.labels="Number of Readmissions", freq.tab=T)
summaryTable(wdata, table.title="Within 100 Days", freq.tab=T, y.name="Readmission", y.labels=c("No Readmission", "Any Readmission"))
\pagebreak
Association Between Readmissions and Donor Type
summaryTable(y=wdata$donor, table.title="Donor Type", freq.tab=T, y.name="Donor Type")
summaryTable(y=wdata$donor.3cat, table.title="Association Between Readmissions and Donor Type (3 Groups)", freq.tab=T, y.name="Donor Type", y.labels=c("Matched Sibs", "MUD", "Others"))
regTable.logistic(formula=wdata$readm.any~as.factor(wdata$donor.3cat), response = "Readmission", y.name="Donor Type (3 Groups)", y.labels=c("MUD vs Matched Sibs", "Others vs Matched Sibs"))
summaryTable(y=wdata$readm.any, bygroup=as.numeric(wdata$donor.3cat), freq.tab=T, bygroup.name="Donor Type", y.name="Readmissions", bygroup.labels=c("Matched Sibs", "MUD", "Others"), caption_heading="Note:", caption = "0: No Readmission; 1: Any Readmission")
\pagebreak
Association
HCT-CI
summaryTable(y=wdata, table.title="Raw Score", y.labels="Number of Readmissions")
summaryTable(y=wdata, table.title="Readmissions", freq.tab=T, y.name="Raw Score")
summaryTable(y=wdata, table.title="Score (3 Groups)", caption_heading="Note:", caption="1: Score=0; 2: Score=1-2; 3: Score=3+", freq.tab=T, y.labels=c("0", "1-2", "3+"))
\pagebreak
DOCUMENT ENDS
It's only when I attempt to knit it as a PDF do I get the following error:
This is pdfTeX, Version 3.14159265-2.6-1.40.20 (TeX Live 2019) (preloaded format=pdflatex)
restricted \write18 enabled.
entering extended mode
! Illegal unit of measure (pt inserted).
<to be read again>
)
l.1949 \item
\underline{\textit{Note:}}
Error: Failed to compile Revised_Code.tex. See https://yihui.name/tinytex/r/#debugging for debugging tips. See Revised_Code.log for more info.
Execution halted
How do I resolve the issue?
The "Illegal unit of measure (pt inserted)." error may also appear when a numerical value is entered for the chunk option out.height. For example out.height=200 generates this error, it should be replaced by out.height="200px".

Error in YAML when knitting RMarkdown document - Scanner error

I'm trying to knit an RMD file in RStudio but I get the following error
Error in yaml::yaml.load(string, ...) : Scanner error: while scanning a plain scalar at line 31, column 5 found a tab character
that violate intendation at line 34, column 1 Calls: ...
parse_yaml_front_matter -> yaml_load_utf8 -> Execution
halted
The header of the rmd file is as following:
> ---
> title: "Homework 4"
> author: NAME OF THE AUTHOR, Fall 2018
> date: Due Friday, September 30, 11:59pm
> subparagraph: yes
> output:
> pdf_document:
> number_sections: true header-includes:
> - \setcounter{secnumdepth}{1}
> - \pagenumbering{gobble}
> - \sloppy
> - \hypersetup{ breaklinks=true,
> pdftitle={\#title},
> pdfauthor={\#author},
> colorlinks=true,
> breaklinks=true,
> urlcolor= blue,
> linkcolor= blue,
> citecolor=blue,
> bookmarksopen=false,
> pdftoolbar=false,
> pdfmenubar=false
> }
> - \usepackage{titlesec}
> - \renewcommand{\thesection}{\arabic{section}.}
> - \usepackage{mathpazo}
> - \usepackage{etoolbox}
> - \usepackage{geometry}
> - \usepackage{graphicx}
> - \newcommand{\protectbracket}[1]{#1}
> - \makeatletter
> \protectbracket{\preto\Gin#extensions{svg,}}
> \DeclareGraphicsRule{.svg}{pdf}{.pdf}{\noexpand\Gin#base.pdf} \makeatother
> - \geometry{verbose,tmargin=1in,bmargin=1in,lmargin=1in,rmargin=1in}
> - \usepackage[most]{tcolorbox}
> - \definecolor{block-gray}{gray}{0.85}
> - \newtcolorbox{darkquote}{colback=block-gray,grow to right by=-10mm,grow to left by=-10mm, boxrule=0pt,boxsep=0pt,breakable}
> - \renewenvironment{quote}{\begin{darkquote}}{\end{darkquote}\ignorespacesafterend}
> ---
Any ideas what is causing this? The error is pointing to character "n" in line 31 which I am not sure how to fix it. any help or suggestion is highly appreciated.
I have tried this with both RStudio installed on both mac and PC.
Assuming those > at the beginning of the line are left-overs from copy and paste (they for sure should not be in the file), you have a YAML document that starts like:
---
title: "Homework 4"
author: NAME OF THE AUTHOR, Fall 2018
date: Due Friday, September 30, 11:59pm
subparagraph: yes
output:
pdf_document:
number_sections: true header-includes:
- \setcounter{secnumdepth}{1}
and that is invalid as you you cannot have both a mapping as value for the key pdf_document as well as a sequence (less indented in addition).
That, probably combined with the combination of the inline colon-space and end-of-line-colon on one line, confuses that parser
You probably want to change that to something beginning with:
---
title: "Homework 4"
author: NAME OF THE AUTHOR, Fall 2018
date: Due Friday, September 30, 11:59pm
subparagraph: yes
output:
pdf_document:
number_sections: true
header-includes:
- \setcounter{secnumdepth}{1}

Rmd/Kntir: Markdown citations in LaTeX environments

I want to create a threeparttable in a Rmd/Knitr-document and to add a note to the bottom of the table. The table is created by a R-function inside a chunk with results = "asis". I did not add the function to the working example because it's quite verbose and the problem is evident from the pure LaTeX code.
This works and the result looks as expected.
---
title: "Untitled"
output: pdf_document
header-includes:
- \usepackage{threeparttable}
- \usepackage{booktabs}
- \usepackage{longtable}
references:
- id: rao2001basic
title: Basic Research in Parapsychology
author:
- family: Rao
given: K.R.
issued:
year: 2001
publisher: McFarland
type: book
---
\begin{table}[h]
\centering
\begin{threeparttable}
\caption{A summary table of the cars dataset.}
\begin{tabular}{lrr}
\toprule
Descriptives & speed & dist\\
\midrule
Mean & 15.4 & 42.98\\
SD & 5.29 & 25.77\\
Min & 4 & 2\\
Max & 25 & 120\\
\bottomrule
\end{tabular}
\tablenotes{\item\textit{Note.} This table was created by #rao2001basic. }
\end{threeparttable}
\end{table}
Unfortunately, the citation in the table caption is not working. It works fine if I take it out of the LaTeX environment, but not inside. Is there a way to parse the Markdown in the LaTeX environment?
This sort of issue is essentially an escaping issue or rather an avoidance issue of pandoc's automatic latex block begin/end recognition.
This particular case could be written with the environment commands directly as
\table[h]
\centering
\threeparttable
\caption{A summary table of the cars dataset.}
\begin{tabular}{lrr}
\toprule
Descriptives & speed & dist\\
\midrule
Mean & 15.4 & 42.98\\
SD & 5.29 & 25.77\\
Min & 4 & 2\\
Max & 25 & 120\\
\bottomrule
\end{tabular}
\tablenotes[flushleft]
\item\textit{Note.} This table was created by #rao2001basic.
\endtablenotes
\endthreeparttable
\endtable
but if the begin{env}/end{env} are truly needed then macros can be used like this
\def \btable{\begin{table}}
\def \etable{\end{table}}
\def \bthreeparttable{\begin{threeparttable}}
\def \ethreeparttable{\end{threeparttable}}
\def \btablenotes{\begin{tablenotes}}
\def \etablenotes{\end{tablenotes}}
It would be nice if a robust generic solution existed for renaming of begin{env}/end{env} that could allow selective markdown within tex blocks. Something like...
\newcommand\mdbegin[2]{%
\ifstrempty{#1}{%
\begin{#2}
}{%
\begin{#1}[#2]
}%
}
\newcommand\mdend[1]{%
\end{#1}
}
which works for this, using the etoolbox package, but I don't think it would be a recommended solution.
I found that if you are willing to use the bookdown::pdf_document2() format, you can use text references to solves this problem without having to mess with LaTeX:
---
title: "Untitled"
output: bookdown::pdf_document2
header-includes:
- \usepackage{threeparttable}
- \usepackage{booktabs}
- \usepackage{longtable}
references:
- id: rao2001basic
title: Basic Research in Parapsychology
author:
- family: Rao
given: K.R.
issued:
year: 2001
publisher: McFarland
type: book
---
(ref:tablenote)
This table was created by #rao2001basic.
\begin{table}[h]
\centering
\begin{threeparttable}
\caption{A summary table of the cars dataset.}
\begin{tabular}{lrr}
\toprule
Descriptives & speed & dist\\
\midrule
Mean & 15.4 & 42.98\\
SD & 5.29 & 25.77\\
Min & 4 & 2\\
Max & 25 & 120\\
\bottomrule
\end{tabular}
\tablenotes{\item\textit{Note.} (ref:tablenote)}
\end{threeparttable}
\end{table}
This even works when tables are created by in R:
```{r results = "asis"}
knitr::kable(mtcars[1:3, ], caption = "(ref:tablenote)")
```

Resources