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}
Related
I am fairly new to Latex.
I have written my thesis in Latex. I have 6 chapters and have made individual stand-alone files for each chapter. The main \thesis folder has a total pf 8 folders containing chapters 1 to 6, bibliography and acknowledgments, (\thesis\ChapterOne.. so on) for each chapter with all the necessary figures, tables, etc.
I tried to put the thesis together using main.tex
\documentclass[12pt,a4paper,oneside]{report}
\begin{document}
\input{\...\thesis\acknowledgements\cacknowledgements.tex}
\input{\...\thesis\ChapterOne\ChapterOne.tex}
\input{\...\thesis\ChapteroTwo\ChapteroTwo.tex}
.
.
\end{document}
I am not able to compile the file and get this error
Can be used only in preamble. \usepackage
How do I get around this?
I also need help with adding the table of contents and list of figures and tables.
Any help is greatly appreciated.
I managed to do something similar with the subfiles package and a conditional like the one used here:
http://xpt.sourceforge.net/techdocs/language/latex/latex23-Conditioning/ar01s02.html.
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.
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.
I am using Knitr in Rstudio, to generate markdown files. I display the tables via xtable package and it shows up nicely in html file. However, when I converd .md to latex via pandoc - the latex file does not contain the tables as it is supposed to be, but only the values in table without any command.
Markdown - Knitr input
In order to give a better idea, the following table provides a sample of
data rows:
```{r table, results='asis', echo=FALSE}
r = read.table("C:/aR_files/data.txt",sep=",", header=TRUE,as.is=TRUE)
r$X = NULL;
print(xtable(r), type='html')
```
Latex
In order to give a better idea, the following table provides a sample of
data rows:
Row1
Row2
Val1
Val1
I thought I may be missing a latex package, so I downloaded ctable.sty, but still I get the same output. Any ideas appreciated, thanks!
I use a very similar workflow to yours and your best bet is to abandon the often clunky xtable package and use the pander package to print your tables. You can wrap any object that you might want to display as a table in the generic pander() function. This is a wrapper for the pandoc.table() function which has several options. If you specify the option style = "XXX" you can achieve what you are asking about here. There are 4 different styles you can choose from; "multiline" (the default), "grid", "simple", or "rmarkdown". I frequently knit rmarkdown documents from within Rstudio and then convert them to Word documents using the pander package:
library(pander)
Pandoc.convert("C:/Users/BlahBlahBlah/Document.md", format="docx")
All of the 4 table styles get turned into table objects upon conversion to .docx format, but only one table style looks right in the .docx document and the .html file that results from the initial "knit". That style is "rmarkdown". You can implement this 2 ways. One would be:
```{r table, results='asis'}
pandoc.table(myTable, style = "rmarkdown")
```
I prefer to set the table style globally at the beginning of my document however, ensuring that all my tables have the same formatting and also allowing me to use the more succinct pander(x) instead of the more verbose pandoc.table(x, style = "someStyle"):
```{r table, results='asis'}
panderOptions("table.style", "rmarkdown")
pander(myTable)
```
There are some side effects of using the rmarkdown style however. Mainly, it does not support newline characters within cells, so buyer beware. I experimented with the different styles and eventually decided that I liked the default style of "multiline" because of it's flexibility with line breaks within cells, even though the .html files I generate look silly. This doesn't bother me though, as I really only use the .docx files that I convert from the .md files. I wrote a blog post about making nice tables that you might find useful. It weighs the pros and cons of several methods including xtable() and several pander() scenarios.
In my Master thesis i want to append a paper as an appendix to the thesis. I am writing in LateX in the "report" style. At the end of the thesis there are some code in Appendix A, and in Appendix B i want to add a paper, written in latex. How do i append the paper at the end, as a standalone paper, with its own bibliography etc?
And the cleaner option consists of using pdfpages. That way rebuilding your document doesn't require appending it again :). The other options aren't really necessary in your case I believe: they mainly concern selecting specific pages and changing the page layout to multiple pages per sheet.
Note very clean, but you could just compile the two files as PDF and the concatenate the them.