Let knitr/kable display latex code for further editing - latex

I have to put all my code from a Rmarkdown document into pure LaTeX format in Overleaf. For this reason, I can't use my knitr code anymore to produce latex tables right away, but I need to write the LaTeX code by myself.
I through, although kable/kableExtra do now show LaTeX code in RStudio, the code must be produced in the background to make LaTeX able to read it.
Now my question: Is there any option to let me see the full LaTeX code produced by kable/kableExtra in order to take it and copy it into my pure LaTeX document?
I would be happy to hear your suggestions.
Best,
Moritz

There are different ways to get to the .tex file from an .Rmd file.
Convert only to LaTeX by using in your YAML header
---
[...]
output: rmarkdown::latex_document
---
Convert to PDF but keep the LaTeX file by using in your YAML header
---
[...]
output:
rmarkdown::pdf_document
keep_tex: yes
---
I prefer the second approach since it allows for previewing the document easily while editing the .Rmd file. Note that there are other output functions besides rmarkdown::pdf_function that support the keep_tex argument.

Related

Pandoc conversion to latex not complete

I have been writing a paper in Markdown and successfully converting to a pdf using pandoc. Now I need to output to a latex file. When I do pandoc does not output a full latex file.
I have recreated the problem in a smaller document.
---
title: Test Document
author: Rusty
date: 2022 Jul 26
---
# Body #
This is the body.
Using the command pandoc --read=markdown --write=latex --output=testdoc.pdf testdoc.md creates the following document which is perfect.
However, when I try to output to latex without converting to pdf a lot of the document is missing. I use a similar command pandoc --read=markdown --write=latex --output=testdoc.tex testdoc.md
This is the entire document.
\hypertarget{body}{%
\section{Body}\label{body}}
This is the body.
How can I get the latex output to include everything and act as a full latex document?
You can add the --standalone option:
pandoc --read=markdown --write=latex --output=testdoc.tex --standalone test.md
(but beware that this will just dump every package but the kitchen sink into your document - many of these package are unnecessary and cause a trillion "funny" problems)

Rmarkdown with pandoc templates, apply lua filter on intermediate .tex

I'm trying to use lua filters to capture images in my manuscript and list their caption in a special \section at the end of it.
I am working on a rmarkdown document that itself uses a .tex template.
I wasn't able to get anywhere, so I run a very simple filter:
function Header (head) print(pandoc.utils.stringify(head)) end
and noticed that just the headers in the markdown were recognized, not the ones in the ones in the template.
The only way I found to have lua filters recognize the elements in the template was to rerun the produced .tex file with pandoc:
pandoc -f latex -t latex -o test2.tex --lua-filter=my_filters.lua test.tex
but that removed all latex formatting and structure content outside the body, e.g., \documentclass, \usepackage and other custom commands. So it's a no go.
So the question is, is there a way to force lua filter to be applied after the integration of a latex template when knitting a rmarkdown document?
There might be a way, but it most likely won't do what you need.
When pandoc reads a document, it parses it and converts it into it's internal data structure. That internal structure can then be modified with a filter. LaTeX is a very expressive and complex document format, and any conversion from LaTeX into pandoc's internal format will result in a loss of (layout) information. That's good enough in most cases, but would be a problem in your case.
There are two possible ways to do this: one is to post-process the output, which is probably tedious and error-prone. The other is to find a way to generate the desired output, e.g. via a pandoc filter, without adding it to the template first.
I believe your other question is the right way to go.

Rmarkdown with LaTex-document in appendix?

I need to create an Rmarkdown document in which I have some text and 3 appendices showing 3 different LaTex files. I found many resources showing how to append Rcode in an appenidx, but have been unsuccessful for other kinds of appendices. As a structure, I use a thesisdown-template. The template has several chapters that are all compiled together in the end. There is also an appendix-template but I haven't figured out how to include a LaTex file in there. But given that I can have LaTex code in a Markdown file, it should be possible?
I do not need to use thesis-down, it could also be similar to the following:
Any ideas are welcome!
If I understand correctly, then you are only interested in PDF output. You can embed raw LaTeX commands in R Markdown, so the following should do what you need:
\appendix
## Appendix 1
\input{/path/to/my/LaTeXfile.tex}
## Appendix 2
\input{/path/to/my/other/LaTeXfile.tex}

Indent R Markdown code block in PDF/LaTeX output

I am currently writing a document in RMarkdown and knitting the output to pdf. It would be desirable for me to indent the code blocks to be consistent with the rest of my formatting.
Here is the document as written:
I would like the gray portion of the block to be in line with "This is a sentence."
Is there a way to do this in R markdown, perhaps by dropping down into LaTeX?
If you want to change the formatting of the box, the key line in the LaTeX template is:
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}
The snugshade command is defined within the framed LaTeX package, which is useful for creating shaded boxes as included within RMarkdown. However, it lacks many controls to simply edit the styling.
Based off this answer, you could consider using the mdframed package which offers more advanced controls than the framed package. As explained here The margins can be tweaked using the renewenvironment command, which we will use to redefine the Shaded function with our custom style:
To get this working in RMarkdown, we need to replace the Shaded environment with our new definition. This can be achieved as follows:
---
output:
pdf_document:
keep_tex: TRUE
header-includes:
- \usepackage{mdframed}
- \definecolor{shadecolor}{gray}{.95}
- \renewenvironment{Shaded}{\begin{mdframed}[
backgroundcolor=shadecolor,
linecolor = shadecolor,
leftmargin=\dimexpr\leftmargin-2pt\relax,
innerleftmargin=1.6pt,
innertopmargin=5pt,
skipabove=10pt,skipbelow=3pt
]}{\end{mdframed}}
---
Some Text
```{r cars}
summary(cars)
```
Note: as you haven't provided a reproducible example, you will have to fine tune the settings to get this working with your configuration.
If you start getting more LaTeX commands, you can considering saving this as a separate .tex file as explained here

Combine rmarkdown generated latex documents in Rstudio without having to manually delete preambles

The problem:
I have several Rmarkdown documents representing different sections of my thesis (e.g. chapter 1 results, chapter 2 results), and I would like to combine the knitted latex versions of these documents with other TeX files into a master TeX 'thesis' document. The problem is, that when knitting to PDF and keeping the TeX file to include in the master TeX document, Rstudio auto-generates a bunch of preamble that ends up clashing with my preamble in the master.TeX document.
A less than ideal, workaround:
I have been working around this issue by deleting this preamble by hand before including the knitted output into the master.tex file.
So far my current workflow is as follows:
set the output to pdf_document in the YAML header, with the keep_tex option set to true.
knitPDF using the Rstudio button
delete the preamble at the beginning of the knitted TeX files by hand.
include the files into my master.tex file using \include{}
The question:
I would like to convert Rmd to LaTeX, without having to delete the preamble manually before including the knitted TeX files into a master TeX file. How do I do this?
Sweave is not an option:
I'm sure I could achieve this by working with Sweave, however I like the flexibility of output formats that Rmarkdown offers: I can knit to html and view in my browser as I'm writing, to quickly view the progress of my work and ensure that there are no bugs, and I can also choose to knit to word to share with my supervisor who works only in word. Then, when I'm ready to compile everything into my master LaTeX document for printing, I can select knit to PDF in Rstudio to obtain the latex output.
I have since come up with a workaround:
create_latex <- function(f){
knitr::knit(f, 'tmp-outputfile.md');
newname <- paste0(tools::file_path_sans_ext(f), ".tex")
mess <- paste('pandoc -f markdown -t latex -p -o', shQuote(newname),"tmp-outputfile.md")
system(mess)}
The function above takes an Rmd file as its input, knits the file to md, and then converts it to TeX by calling pandoc from the command-line.
The secret ingredient lies in the pandoc call... When knitting using Rstudio, Rstudio must be calling the pandoc standalone -s flag when it compiles the pdf. This generates a 'standalone' document, i.e. one that contains latex preamble. This is obviously necessary when you want to view the PDF, but conflicts with my needs.
I am instead seeking to generate a latex 'fragment' from Rmd with knitr, that can be later incorporated into my master Latex file. So the solution was simply to create a pandoc command line call that omits the -s standalone flag. I achieved this by calling pandoc from R with system() inside the above function.
Hope this helps anyone out there having this problem, but would be great if I was able to change Rstudio's settings to avoid bothering with this hack. Suggestions and feedback welcome.
Here is a simpler solution using the LaTeX package docmute.
Your main Rmarkdown document, (e.g., main.Rmd) should load the docmute package and include your other files (e.g., part1.Rmd and part.Rmd), once knitted and located in the same directory, with \input or \includelike this:
---
title: "Main"
output: pdf_document
header-includes:
- \usepackage{docmute}
---
# Part 1
\input{part1.tex}
# Part 2
\input{part1.tex}
Then your other files just need to have a keep_tex argument in the YAML front matter. Like this:
---
title: "Part 1"
output:
pdf_document:
keep_tex: true
---
Something
and this:
---
title: "Part 2"
output:
pdf_document:
keep_tex: true
---
Something else
Then all you need to do is knit part*.Rmd's before main.Rmd and all the content will appear in main.tex and main.pdf. The docmute package strips the preamble from the input .tex files so you will need to make sure anything you need before \begin{docunent} in the input files is also in main.Rmd.

Resources