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
Related
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}
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.
Pandoc can now generate powerpoint presentations from markdown. This seems to work quite well.
However, when I try to include an equation – even something as simple as $a=2$ – the whole contents of the slide disappears. Is this a bug or a feature? Can you include equations in PowerPoint presentations? I was hoping that I would be finally able to write my lectures (which need to be in powerpoint because of reasons) using TeX math syntax in a plain text file.
EDIT:
The command to convert the markdown document saved in the file test.rmd was
render("test.rmd")
Minimal example #1:
---
output: powerpoint_presentation
---
## Math test
This is a test (no maths)
Output:
Test example #2:
---
output: powerpoint_presentation
---
## Math test
This is a test $a=2$
Output:
Versions:
> rmarkdown::pandoc_version()
[1] ‘2.7.1’
> packageVersion("rmarkdown")
[1] ‘1.10’
MS PowerPoint 2007. Note that if Word output is chosen, the formula appears as expected.
The problem seems to be with PowerPoint. From what you found, and from what I can tell from searching the web, is seems safe to say that PowerPoint 2007 does not have full support for Microsoft's OMML math format (although Word 2007 does have support).
Unfortunately, inserting inline PNGs through pandoc is not possible with PowerPoint, so inserting formulas rendered as PNG won't work either. The only option would be to insert equation images as figures, but that would limit you to one equation per slide (or two when used with columns).
From within the jupyter notebook, is there a way to add latex figure caption below each inline matplotlib figure? This is desired so that each figure is annotated when running nbconvert --to latex.
But I am not clear on how to position the LaTeX relative to the figure which ends up in \begin{verbatim} block. I can place it in a markdown cell just after the plot; but, that does not wrap the figure as I want.
You can add captions by following the example at Making Publication Ready Notebooks a blog post by a Mr. Julius Schulz. The technique basically boils down to adding the caption as part of the JSON metadata for the cell in which you generate the figure and then providing the right instructions in the template you pass to nbconvert. I have directly copy pasted the section of Julius' template file which draws the figures as I am not too hot on the Jinja templates front. The blog post was very helpful for me.
A bit of a workaround but the following helper function calls plt.close() to keep the inline figures from being displayed leaving only the generated LaTeX block for the figure.
bShowInline = True # Set = False for document generation
def makeplot( plt, figlabel, figcaption):
figname = figlabel+'.png'
plt.savefig(figname)
if bShowInline:
plt.show()
else:
plt.close()
strLatex="""
\\begin{figure}[b]
\centering
\includegraphics[totalheight=10.0cm]{%s}
\caption{%s}
\label{fig:%s}
\end{figure}"""%(figname, figcaption, figlabel)
return display(Latex(strLatex))
Is there a cleaner way?
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.