Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reduce cell width and font size of table using pandoc.table()

I'm using knitr and pander to make a table in a markdown file. I'm converting the markdown file to a PDF using Pandoc from within R.

This code:

library(knitr)

```{r myTable, echo=FALSE, message=FALSE, results='asis', comment=""}

library(pander)
pandoc.table(head(iris))

``` 

then running this function within R:

knitsPDF <- function(name) {
  knit(paste0(name, ".Rmd"), encoding = "utf-8")
  callformat <-"pandoc -V geometry:margin=1in  %s.md -o %s.pdf"
  system(sprintf(callformat, name, name))
}

knitsPDF(name) # insert file name of .Rmd file

produces this table in the PDF file:

enter image description here

How can I 1. Reduce width of columns in table? 2. Reduce font size of table?

like image 888
luciano Avatar asked May 07 '13 16:05

luciano


People also ask

How do I reduce font size in RMD?

To change the font size, you don't need to know a lot of html for this. Open the html output with notepad ++. Control F search for "font-size". You should see a section with font sizes for the headers (h1, h2, h3,...).

What is Pandoc R?

Pandoc is a document converter. It can convert from a number of different markup formats to many other formats, such as . doc , . pdf etc. Pandoc is a command line tool with no GUI.

Is Pandoc an R package?

pandoc is currently an experimental R package primarily develop to help maintainers of R Markdown ecosystem.


2 Answers

If you do not want to split the table into multiple parts based on its width, you can specify that directly in split.tables parameter with pandoc.table or more generally in table.split.table in panderOptions. E.g.:

> pandoc.table(head(iris), split.table = Inf)

-------------------------------------------------------------------
 Sepal.Length   Sepal.Width   Petal.Length   Petal.Width   Species 
-------------- ------------- -------------- ------------- ---------
     5.1            3.5           1.4            0.2       setosa  

     4.9             3            1.4            0.2       setosa  

     4.7            3.2           1.3            0.2       setosa  

     4.6            3.1           1.5            0.2       setosa  

      5             3.6           1.4            0.2       setosa  

     5.4            3.9           1.7            0.4       setosa  
-------------------------------------------------------------------

> panderOptions('table.split.table', 300)
> pander(head(iris))

-------------------------------------------------------------------
 Sepal.Length   Sepal.Width   Petal.Length   Petal.Width   Species 
-------------- ------------- -------------- ------------- ---------
     5.1            3.5           1.4            0.2       setosa  

     4.9             3            1.4            0.2       setosa  

     4.7            3.2           1.3            0.2       setosa  

     4.6            3.1           1.5            0.2       setosa  

      5             3.6           1.4            0.2       setosa  

     5.4            3.9           1.7            0.4       setosa  
-------------------------------------------------------------------

About fontsize: Pandoc's markdown do not have any special syntax for that, so you might use LaTeX markup for your pdf. E.g. just issue a \footnotesize directive before your table. See possible font sizes for more details: http://en.wikibooks.org/wiki/LaTeX/Fonts#Sizing_text

like image 122
daroczig Avatar answered Oct 02 '22 06:10

daroczig


You can change the fontsize for specific cells in pander, and thus the entire table, but as a workaround using latex code.

Where your cell is, if the data populating your cell is "770" next to another cell populated by "$731,258", If you replace the first cell by paste0("\\scriptsize", "770"), your output will look like something like this:

enter image description here

as you can see, the "770" is in a smaller font. You can do this with any latex font tag.

like image 1
Devuroasts Avatar answered Oct 02 '22 06:10

Devuroasts