LaTeX formatting incorrect terms in RMarkdown table? - r-markdown

I am trying to create a table of terms in an rmarkdown pdf file. Basically, I would have the columns be the category of term on the left (using kable collapse_rows() feature), the quantity itself, then finally a description.
I am having trouble formatting my terms within the r code chunk for my kable- essentially, it is not reading them as LaTeX-styled terms. I have looked at previous questions and seen that I need to have the results = "asis" and use \\ instead of \ but still can't quite get it to work.
Here is my code chunk:
{r, results='asis'}
flux = c("$\\j_{FI}$","$\\j_{VG}$")
p_label = rep("flux", length(flux))
deets = c("Ingestion","Structural growth")
table = cbind(p_label,flux,deets)
kable(table,col.names = c("Quantity Type","Quantity","Description")) %>%
collapse_rows(columns = 1, latex_hline = "major", valign = "middle")
It produces this:
I'd like my terms to look like they do in the rest of the markdown document (when they are outside of code chunks)
Thanks!

Related

RMarkdown, knitr::kable shows "\begin{table}" after knitting to pdf document

One of cells in my RMarkdown document
```{r echo=FALSE}
head(data,3) %>% knitr::kable(caption = "Pierwsze 3 wiersze ze zbioru danych Lista_1.csv", digits = 2, booktabs = T)
gives weird result after knitting to pdf:
Of course there shouldn't be "\begin{table}" ,"\caption{}" and "\end{table}" parts. I use knitr::kable often and it never worked this way. Does anyone know how to fix it?
Edit: I have also noticed that all section headers (like "##Section2") below the table are centered. They shouldn't.
I've just found this question: How do I prevent kable from leaving raw latex in the final document if I include a caption in a table?
and used tip from comment (format = pandoc). It worked for me.

How to create own table in Bookdown and reference it within the text?

I am currently writing my thesis in RMarkdown using the template Oxforddown (which is ultimately based on bookdown). I have been reading the documentation but I confess I am lost. I am trying to create a table that contains an overview of the experimental conditions and items I used in my empirical study, so it is not data that I can load into R and then use the kable function on. However, I do not understand how I could generate such a table. Generating RMarkdown tables outside code chunks seems to work, but then the captions and referencing are very different than the rest of the captions used so far, which I usually set up within code chunks. Example below:
{r pilot-short7, echo=FALSE, fig.scap="Pilot 2: ....", out.width="65%", message=FALSE, fig.pos='H', fig.align = 'center'}
When I am trying to include RMarkdown tables inside a code chunks, things go wrong. What would my options be?
Any help would be very much appreciated!
I prepared a markdown template for you.
Here I made a table with flextable library.
But you can use another, which you like, f.e.: kableExtra, gt etc.
As you can see, you should put \label{tab:caption} and after refer in the text by \ref{tab:caption}.
---
title: "Hello World"
header-includes:
- \usepackage{caption}
output:
pdf_document:
latex_engine: xelatex
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, include = FALSE}
library(flextable)
library(dplyr)
table2 <- flextable(mtcars[1:5, 1:6])
```
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
\begin{table}[hbtp]
\captionof{table}{Text of the caption.}
\label{tab:caption}
`r table2`
\end{table}
Table \ref{tab:caption} is the baddest table in the World

R markdown summary output

I would like to keep all the columns of the summary output in same line, do you have a solution?
as shown in the picture, the last column goes to a new line, how to avoid it?
Thanks a lot in advance!
Without a code example, I can't help edit the code. In your Rmarkdown initial set up chunk, put the code below
```
{r setup}
knitr::opts_chunk$set(echo = TRUE)
options(width = 1000)
```
the width = 1000 will need to be adjusted to meet your needs.
here is a link that might help you get more information

Knit Markdown code blocks with Rmd Chunk Code Styling

I have many markdown files, each with many codeblocks, see an example below. (They were converted to this format via pandoc from other file types)
I would like to knit these in as Rmd files. Right now, the codeblocks have no decorators. When I knit the file below, there is no code syntax styling/coloring. I do not want to evaluate the code, I just want to print them out, hence: knitr::opts_chunk$set(warning=FALSE, message=FALSE, cache=FALSE).
Suppose all the code are MATLAB code, is there something I can add like: knitr::opts_chunk$set(code=MATLAB), so that they would all get MATLAB code styling/coloring?
My code chunks are actually all MATLAB code, so MATLAB stlying/coloring would be more helpful, but any code styling would be great to make the code chunks in outputted HTML/PDF etc easier to read.
---
title: matlab code in blocks
output: html_document
---
# RMD file with Markdown Code Blocks
```{r global_options, include = FALSE}
knitr::opts_chunk$set(warning=FALSE, message=FALSE, cache=FALSE)
```
## Example 1
Here is a code block A
fl_fig_wdt = 3;
fl_fig_hgt = 2.65;
figure('PaperPosition', [0 0 fl_fig_wdt fl_fig_hgt], 'Renderer', 'Painters');
x = rand([10,1]);
y = rand([10,1]);
scatter(x, y, 'filled');
grid on;
grid minor;
## Section 2
Here is a code block B
fl_fig_wdt = 5;
fl_fig_hgt = 5.65;
figure('PaperPosition', [0 0 fl_fig_wdt fl_fig_hgt], 'Renderer', 'Painters');
x = rand([20,1]);
y = rand([20,1]);
scatter(x, y, 'filled');
grid on;
grid minor;
End of file.
If you need a general solution, you need to use a Pandoc Lua filter (I'm not able to write one for you; see the Github issue that you cross-posted). If you only need HTML output, you may use JavaScript to add a class name to the code blocks, e.g., add this js code chunk to the end of your document:
```{js, echo=FALSE}
document.querySelectorAll('pre').forEach(function(el) {
// the class name is the language name
// (I'm using R here, although it's not the right name)
if (!el.className) el.className = 'r';
});
```
The output looks like this:

Caption numbering not in sequential order when citing the captions with captioner in Rmarkdown

I am using captioner (https://cran.r-project.org/web/packages/captioner/vignettes/using_captioner.html) to create table captions in Rmarkdown - the main reason is because I am using huxtable for conditional formatting and exporting to word. This is the only I have found to have numbered captions.
I was trying to reference the captions but the caption number is not in sequential order when citing the captions but only if the table_nums(..., display="cite") is before the tables. I was trying to give the range of table numbers and it changed the number of the last table. I The number isn't changed if the r table_nums('third_cars_table',display = "cite") is put after the captions. Is there a way to make sure that table numbers remain in sequential order? I'd also be happy with a better solution for numbered captions.
Reproducible example:
---
title: "Untitled"
output: bookdown::word_document2
---
```{r setup, include=FALSE}
library(captioner)
library(huxtable)
library(knitr)
library(pander)
table_nums <- captioner(prefix = "Table")
fig_nums <- captioner(prefix = "Figure")
knitr::opts_chunk$set(echo = TRUE)
```
## Description of tables
I am trying to put a description of tables
and say that these results are shown table numbers ranging
from the first table (`r table_nums('first_cars_table',display = "cite")`)
to the last table (`r table_nums('third_cars_table',display = "cite")`)
```{r, results='asis',echo=FALSE,eval.after=TRUE}
tablecap1=cat(table_nums(name="first_cars_table",caption='First car table'))
kable((cars[1:5,]))
tablecap2=cat(table_nums(name="second_cars_table",caption='second car table'))
kable(cars[6:10,])
tablecap3=cat(table_nums(name="third_cars_table",caption='third car table'))
kable(cars[10:15,])
```
The results:
A (terrible) workaround is to manually give the number ordering using display = FALSE. For example, inserting the following at the start of the document will ensure t1-t5 are sequentially numbered, no matter where the tables or first citations appear:
`r table_nums('t1', display = FALSE)`
`r table_nums('t2', display = FALSE)`
`r table_nums('t3', display = FALSE)`
`r table_nums('t4', display = FALSE)`
`r table_nums('t5', display = FALSE)`
I have not examined the captioner code but I expect that the document is read from top to bottom once and hence the numbering is stored in a first come, first served basis. Thus, I am not sure there are any other ways to get around this as it would involve some kind of pre-processing stage.