Figure position in markdown when converting to PDF with knitr and pandoc
I'm not aware of such an option for pandoc
to set the floating option of figures when converting a Markdown document to LaTeX. If you choose Markdown for its simplicity, you should not expect too much power from it, even with powerful tools like pandoc
. Bottom line: Markdown is not LaTeX. It was designed for HTML instead of LaTeX.
Two ways to go:
use the Rnw syntax (R + LaTeX) instead of Rmd (R Markdown) (examples); then you will be able to use the chunk option
fig.pos='H'
after you\usepackage{float}
in the preamble; in this case, you have full power of LaTeX, and pandoc will not be involvedhack at the LaTeX document generated by pandoc, e.g. something like
library(knitr)
knit('foo.Rmd') # gives foo.md
pandoc('foo.md', format='latex') # gives foo.tex
x = readLines('foo.tex')
# insert the float package
x = sub('(\\\\begin\\{document\\})', '\\\\usepackage{float}\n\\1', x)
# add the H option for all figures
x = gsub('(\\\\begin\\{figure\\})', '\\1[H]', x)
# write the processed tex file back
writeLines(x, 'foo.tex')
# compile to pdf
tools::texi2pdf('foo.tex') # gives foo.pdf
If you do not like these solutions, consider requesting a new feature to pandoc on Github, then sit back and wait.
How to hold figure position with figure caption in pdf output of knitr?
As Yihui mentioned in his answer (Figure position in markdown when converting to PDF with knitr and pandoc), we cannot expect too much about formatting from mardown. To workaround this problem, just write some R scripts to replace htbp
to H
.
Compared with knit
from knitr package, I found render
from rmarkdown is better to export a tex
file. Just remember to add keep_tex: yes
in the yaml header of your rmarkdown file.
library(rmarkdown)
render('filepath.Rmd')
x <- readLines('filepath.tex')
pos <- grep('begin\\{figure\\}\\[htbp\\]', x)
x[pos] <- gsub('htbp', 'H', x[pos])
writeLines(x, 'filepath.tex')
tools::texi2pdf('filepath.tex', clean = TRUE) # gives foo.pdf
file.remove('filepath.tex')
Pandoc Markdown to PDF image position
Did you try to deactivate the implicit_figures
as in
pandoc -f markdown-implicit_figures -t pdf myfile.md
To solve the size problem you could also try to fix the size within the markdown file with an attribute. Something like that can do the trick:
![Caption text](/path/to/image){ width=50% }
Pandoc: how to keep order of images and subtitles when rendering md to pdf
Found my answer here:
Pandoc Markdown to PDF image position
I needed to render using this command, which deactivates the implicit_figures
:
pandoc 2-respondents.md -f markdown-implicit_figures -o example.pdf
How to force image to text when converting markdown to pdf using pandoc
Pandoc uses LaTeX for PDF creation by default.
Using an external file
Put the following in e.g. header.tex
:
\makeatletter
\def\fps@figure{h}
\makeatother
Or alternatively, the following:
\usepackage{float}
\let\origfigure\figure
\let\endorigfigure\endfigure
\renewenvironment{figure}[1][2] {
\expandafter\origfigure\expandafter[H]
} {
\endorigfigure
}
Then use:
pandoc input.md --include-in-header header.tex -o output.pdf
Using only a markdown file
Or instead of using a header.tex
, you can also embed it in your markdown file's YAML metadata block:
---
header-includes: |
\makeatletter
\def\fps@figure{h}
\makeatother
---
# my markdown header
Using table caption on R markdown file using knitr to use in pandoc to convert to pdf
If you do not insist on using a LaTeX/HTML-only solution with the otherwise awesome xtable
package, you might achieve the same with Pandoc's markdown. One option is to add the caption manually below the table, or use my R Pandoc writer package:
> library(pander) # load pkg
> panderOptions('table.split.table', Inf) # not to split table
> set.caption('Hello Fisher!') # add caption
> pander(head(iris)) # show (almost) any R object in markdown
-------------------------------------------------------------------
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
-------------- ------------- -------------- ------------- ---------
5.1 3.5 1.4 0.2 setosa
4.9 3.0 1.4 0.2 setosa
4.7 3.2 1.3 0.2 setosa
4.6 3.1 1.5 0.2 setosa
5.0 3.6 1.4 0.2 setosa
5.4 3.9 1.7 0.4 setosa
-------------------------------------------------------------------
Table: Hello Fisher!
Then use Pandoc to convert this markdown file to HTML, LaTeX, docx, odt or any other popular document formats.
Set margin size when converting from Markdown to PDF with pandoc
Recent versions of rmarkdown and pandoc
In more recent versions of rmarkdown, the settings of margins can be done in the YAML header via the top-level element geometry
. What you specify in the geometry
tag will be piped into the LaTeX template that ships with Pandoc via the following LaTeX snippet
$if(geometry)$
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
For example, to specify margins that are 2cm in width one would include
---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: margin=2cm
output: pdf_document
---
For more complex specifications to be passed to the geometry LaTeX package, string options together as you would with LaTeX:
---
title: "Habits"
author: John Doe
date: March 22, 2005
geometry: "left=3cm,right=3cm,top=2cm,bottom=2cm"
output: pdf_document
---
Original answer
This is a LaTeX question as Pandoc is rendering to PDF via LaTeX - what you linked to represents the default margins on a LaTeX document.
The geometry LaTeX package for example can be used to alter the margins of the page. However you'll need a way to tell Pandoc to use this by including it ins the LaTeX header applied to the converted md file.
How you do this is documented in the Pandoc User Guide. See in particular the --template=FILE
command line argument and the Templates section. Essentially, either find and modify the default template to include the LaTeX instructions you want to use or start your own template from scratch and place it in the appropriate location; see the --data-dir
command line argument.
Another alternative if you are using a recent version of Pandoc is to use the variable argument (set either with -V KEY[=VAL]
or --variable=KEY[:VAL]
). The geometry
package was added to the default LaTeX template in May 2012 (see this discussion). As such, if you wanted to change the page margins, you can use:
pandoc -V geometry:margin=1in -o output.pdf input.md
You can specify multiple variable values too. For instance, if you wanted to create a 4 by 6 inch pdf with half-inch margins, you can use:
pandoc -V geometry:paperwidth=4in -V geometry:paperheight=6in -V geometry:margin=.5in -o output.pdf input.md
Related Topics
How Does One Reorder Columns in a Data Frame
Group by Multiple Columns in Dplyr, Using String Vector Input
Select First and Last Row from Grouped Data
Method to Extract Stat_Smooth Line Fit
How to Extract a Single Column from a Data.Frame as a Data.Frame
Painless Way to Install a New Version of R
How to Remove All Whitespace from a String
Incomplete Final Line' Warning When Trying to Read a .Csv File into R
How to Use Grep()/Gsub() to Find Exact Match
How to Display Only Integer Values on an Axis Using Ggplot2
What Is Meaning of First Tilde in Purrr::Map
R Ifelse to Replace Values in a Column
How to Calculate the Co-Occurrence in the Table