Modify latex template in Rmarkdown - latex

Can I modify the default Latex template used by Rmarkdown to compile Rmd files to pdf with the beamer class?
I am asking because of the difficulties I encounter using \includepdf (from the pdfpages package). Inspecting the generated tex source suggests that the root problem is the ignorenonframetext option in \documentclass[ignorenonframetext,]{beamer}.
(It is not a viable option for me to manually change the tex source each time)
In short: I want to modify the "Rmd beamer Latex template" (if that exists) to change the default ignorenonframetext

You don't need to modify the template, you can use \includepdf like this:
---
output:
beamer_presentation:
keep_tex: true
header-includes:
- \usepackage{pdfpages}
- \setbeamercolor{background canvas}{bg=}
- \makeatletter\beamer#ignorenonframefalse\makeatother
---
test
``` {=latex}
\end{frame}
\includepdf[pages=1-10]{example-image-duck}
\begin{frame}
```
test
(the line \setbeamercolor{background canvas}{bg=} is necessary for beamer versions < 3.64, a patch has been added in 9e3bb9)

Related

Export tex document which uses the exam class (Part 2)

I would like to export a text document which uses the exam class to markdown. To do so, I am currently using a workaround which was suggested in this answer, which relies on pseudo-definitions which in turn overwrite the definitions of the exam class such that pandoc can produce a clean markdown file.
Although, the workaround works for the suggested multiple-choice questions, I cannot adopt the solution to work for text with “fillin gaps” such as the document below:
\documentclass[answers]{exam}
\usepackage{minted}
\let\oldpart\part
\renewcommand{\part}[1][]{\oldpart[#1]{}}
\begin{document}
\begin{questions}
\question Exercise 1
\begin{parts}
\part[1] This fills in the \fillin[blanks][3cm]
\end{parts}
\end{questions}
\end{document}
If I use the following pseudo-definitions in a separate file:
% ignore \part
\renewcommand{\part}[0][1]{}
% Treat checkboxes like an itemized list
\newenvironment{checkboxes}{\begin{itemize}}{\end{itemize}}
\renewcommand{\CorrectChoice}{\item ☒ }
\renewcommand{\choice}{\item ☐ }
\renewcommand\fillin[2][{}]{\textbf{#1}}
I get the following broken markdown output
This fills in the **blanks**3cm\]
Moreover is there a way for pandoc to ignore \begin{parts} and \end{parts} so that there are no ::: in the final Markdown file?
The \fillin problem can be solved with
\newcommand{\fillin}[1][1]{\textbf{#1}\noop}
whereas the parts div can be removed with
\newenvironment{parts}{}{}

Theorem and Proof Environment in Beamer

I am currently trying to use quarto beamer to making lecture slides. I would like to use the theorem environment in beamer, the qmd file however cannot render properly. Rendering stopped with latex error showing that Command \theorem already defined. I am wondering what have I done wrong?
The sample codes are as follows
---
title: "Untitled"
format: beamer
editor: visual
---
## Quarto
Quarto enables you to weave together content and executable code into a finished presentation. To learn more about Quarto presentations see <https://quarto.org/docs/presentations/>.
::: {#thm-test}
## Just a quick test
:::
If possible, I hope that I can include theorem, definition, example environment in quarto beamer presentation.
Thanks for the help in advance.
Quarto very unhelpfully loads tons of unnecessary packages like amsthm, which beamer automatically loads, and then blindly tries to define a new theorem environment, even though beamer already has defined it. At the very minimum it should check if an environment is already defined before doing this.
You can work around the problem with the notheorems class options.
---
title: "Untitled"
format: beamer
editor: source
classoption: "notheorems"
---
## Quarto
Quarto enables you to weave together content and executable code into a finished presentation. To learn more about Quarto presentations see <https://quarto.org/docs/presentations/>.
::: {#thm-test}
## Just a quick test
:::

Latex: setting another directory for bibliography of natbib package (windows)

How to declare a different directory than the working folder with the natbib package using Tex Studio (Windows)? There is a directory for all my bibliographies (BiblioX.bib), and I use multiple per document. The bibliography path is : C:\Users\User\Dropbox\PDF\Bib ,and the working folder is : C:\Users\User\Dropbox\R&D\Project\Article\Work_Article .
The following code works:
\documentclass[12pt, a4paper]{article}
%Bibliography
\usepackage{natbib}
\bibliographystyle{newapa}
\begin{document}
\bibliography{../../../../../PDF/Bib/Biblio1,../../../../../PDF/Bib/Biblio2}
\end{document}
The goal would be to be able to use instead:
\bibliography{Biblio1,Biblio2}
Therefore declare a directory that Natbib would recognize : C:\Users\User\Dropbox\PDF\Bib
I thought about using a variable (or a new command):
\def \BibPath {C:/Users/User/Dropbox/PDF/Bib}
\bibliography{\BibPath/Biblio1,\BibPath/Biblio2}
But it does not work with the Natbib drop menu allowing to select the reference when writing \cite{ in Tex Studio. Is there a way to change the default of where Natbib/Tex Studio looks for bibliography?
Thanks!

Define Latex packages in reST file

We used Docutils to produce reST document, and then make a TeX file via rst2latex.
In the rst file, we have added a lot of LaTeX code like:
.. raw:: latex
~\\
\rule{\textwidth}{1pt}
~\\
But I do not know where to add packages like \usepackage{tabulary}.
If I add it in the rst file like I've shown above, even in the very beginning, this \usepackage line is automatically added after \begin{document} in the tex output file. This obviously generates an error.
Any idea where can I add \usepackage commands in reST?
You can use the LaTeX preamble (after Docutils 0.7) by
rst2latex foo.rst foo.tex --latex-preamble="\usepackage{tabulary}"
which will generate the following in foo.html
%%% Custom LaTeX preamble
\usepackage{tabulary}
Alternatively, a custom stylesheet can be provided by
rst2latex foo.rst foo.tex --stylesheet=preamble.tex
which will generate
%%% User specified packages and stylesheets
\input{preamble.tex}
in the right place.

Set double spacing and line numbers when converting from Markdown to PDF with pandoc

I am using markdown & pandoc for scientific writing, I know I can change margins of the final pdf using
Set margin size when converting from Markdown to PDF with pandoc
but very often the journals require double lines and line numbers in submitted manuscripts, the question is how to change these, I don't know much about LaTex so I am lost in that jungle.
Thanks!
In more recent version of Pandoc, you can use a YAML header and include a bunch of metadata in that, instead of using options on the command line. So, for instance, you can put this at the top of your .md file:
---
title: The Document Title
author:
- Your Name
- Co Author
date: \today{}
geometry: margin=2cm
header-includes:
- \usepackage{setspace}
- \doublespacing
- \usepackage{lineno}
- \linenumbers
---
Document Text
and pandoc will read those options and apply them automatically.
There is maybe an easy way: generate a file with the packages we need
\usepackage{setspace}
\doublespacing
\usepackage[vmargin=1in,hmargin=1in]{geometry}
\usepackage{lineno}
\linenumbers
I named it options.sty. And use the -H FILE option that includes the content of the FILE at the end of the preamble. (as used in https://github.com/karthikram/smb_git)
pandoc -H options.sty --bibliography mypaper.bib mypaper.md -o mypaper.pdf
The advantage is that we don't need to edit the template. To add linenumbers, change margins, and set spacing it works.
You'll need to use a custom LaTeX template. First, use pandoc to create a copy of the default template:
pandoc -D latex > mytemplate.latex
Now edit this template. Somewhere in the preamble (between \documentclass{...} and \begin{document}), insert the lines
\usepackage{setspace}
\doublespacing
Then, to use your custom template:
pandoc --template mytemplate.latex mypaper.txt -o mypaper.tex

Resources