ulyngs / oxforddown

Template for writing an Oxford University thesis in R Markdown; uses the OxThesis LaTeX template and was inspired by thesisdown.
https://ulyngs.github.io/oxforddown/
MIT License
221 stars 81 forks source link

Formatting tables #14

Closed JoseBSL closed 3 years ago

JoseBSL commented 3 years ago

I have not been able to format tables as I do in markdown. Is there another way to format tables and control their sizes?

Here is an example of a table that I'm formatting using kableExtra, I'm not very familiar with bookdown so I may be missing something relevant, I do apologize if that's the case.

#load libraries
library(knitr)
library(kableExtra)

Species <- c("Brassica oleracea", "Brassica rapa", "Eruca sativa", "Sinapis alba", "Ipomoea aquatica", "Ipomoea purpurea", "Capsicum annuum", "Petunia integrifolia", "Solanum lycopersicum", "Solanum melongena")
Species <- as.character(Species)

Common_names <- c("Wild cabbage", "Pak choi", "Rocket", "White mustard", "Water spinach", "Morning glory", "Capsicum", "Petunia", "Tomato", "Eggplant")
Common_names <- as.character(Common_names)
Variety <- c("Capitata", "Chinensis", "", "", "", "", "California Wonder", "", "Tommy Toe", "Little Fingers") 
Variety <- as.character(Variety)
Source <- c("https://www.mrfothergills.com.au/", "https://www.mrfothergills.com.au/", "https://www.mrfothergills.com.au/", "https://www.mrfothergills.com.au/", "https://www.theseedcollection.com.au/", "http://www.shaman-australis.com.au", "https://www.edenseeds.com.au", "https://www.dianeseeds.com/", "https://www.mrfothergills.com.au/", "https://www.4seasonsseeds.com.au/")
Source <- as.character(Source)
t <- data.frame(Species, Common_names, Variety, Source)
colnames(t) <- c("Species", "Common names", "Varieties", "Source")
kable(t, format = "latex", longtable = T, booktabs = T,linesep = "\\addlinespace",
             caption = "\\textbf{Table S1.} Species names, common names, varieties and sources of the different seeds.") %>%
  kable_styling(latex_options = c("repeat_header","striped"), font_size = 10, full_width=T) %>% row_spec(0,bold=TRUE)  %>%
  column_spec(1,italic =T)

This table in markdown runs well with this YAML

output: pdf_document: keep_tex: yes fig_caption: yes toc: no html_document: toc: no df_print: paged fontsize: 12pt header-includes:

However, in bookdown I'm unable to run it with the kable_styling specifications. Sorry if this is not an issue!

ulyngs commented 3 years ago

Hi @JoseBSL ,

does this chapter provide what you need? https://ulyngs.github.io/oxforddown/tables.html :)

Ulrik

JoseBSL commented 3 years ago

Thanks Ulrik! That is indeed a superuseful resource!

I'm not sure what happens but I'm still unable to run the code that I posted (which runs fine in markdown) in bookdown. I guess it may be a latex thing or KableExtra related (I have reinstall tex and KableExtra for mac and still got the same issue).

JoseBSL commented 3 years ago

Ok just made it, I needed to add to the template ociamthesis.cls

\usepackage{booktabs} \usepackage{longtable} \usepackage{array} \usepackage{multirow} \usepackage{wrapfig} \usepackage{float} \usepackage{colortbl} \usepackage{pdflscape} \usepackage{tabu} \usepackage{threeparttable} \usepackage{threeparttablex} \usepackage[normalem]{ulem} \usepackage{makecell} \usepackage{xcolor}

as recommended in http://haozhu233.github.io/kableExtra/awesome_table_in_pdf.pdf

Seems that was not loading the kableExtra packages properly (don't know why).

Sorry for the trouble and thanks a lot for the help!

ulyngs commented 3 years ago

Hi Jose,

I think the current version should be fixed so that the kableExtra packages are included whenever you load the kableExtra library somewhere - can you test if it works for you in the current version without needed to add anything to ociamthesis.cls?

JoseBSL commented 3 years ago

I jus tried and without that packages I was not able to build the document. It might be other latex expression that I'm using to format the tables that requires one of these packages. I'm running it with KableExtra version 1.3.4

I get this error when I do not add these packages to ociamthesis.cls:

[pandoc warning] Could not parse YAML header: mapping values are not allowed in this context "source" (line 159, column 25) ! LaTeX Error: Environment longtabu undefined.

ulyngs commented 3 years ago

interesting - I assume it works for you also if you add these packages in templates/template.tex instead of in ociamthesis.cls?

Sounds like I should just revert to putting the packages explicitly in the template instead of being clever about it!

JoseBSL commented 3 years ago

Yes, you are right, it also works if I add the same list of packages in the section of your own personal macros of template.tex

ulyngs commented 3 years ago

Jose, just to be sure, did you try downloading a fresh copy of the current oxforddown on GitHub and try? I think it should work for you without adding the packages, as long as you do library(kableExtra) somewhere?

(if it still doesn't, then i'll add the kableExtra packages back in explicitly)

JoseBSL commented 3 years ago

Ha! This is getting tricky Ulrik (a new problem came up), I just downloaded the latest oxforwdon but I cannot build (I haven't added my documents on it yet).

Execution halted make: *** [pdf] Error 1

ulyngs commented 3 years ago

is this the error message you're getting:

make: Rscript: No such file or directory
make: *** [pdf] Error 1

Does it compile as normal when you simply knit index.Rmd?

JoseBSL commented 3 years ago

The index.Rmd does not compile either, I'm getting this error when I build all:

output file: _main.knit.md

pandoc: unrecognized option `--lua-filter=scripts_and_filters/colour_and_highlight.lua'
Try pandoc --help for more information.
Error: pandoc document conversion failed with error 2
In addition: Warning messages:
1: In yaml::yaml.load(..., eval.expr = TRUE) :
  NAs introduced by coercion: 10,052 is not an integer
2: In yaml::yaml.load(..., eval.expr = TRUE) :
  NAs introduced by coercion: 10,052 is not an integer
3: In yaml::yaml.load(..., eval.expr = TRUE) :
  NAs introduced by coercion: 10,052 is not an integer
Execution halted
make: *** [pdf] Error 1

Exited with status 2.
ulyngs commented 3 years ago

try not 'build all' but simply knit index.Rmd - what happens then?

(also, which operating system are you on? I am guessing mac os with big sur, as this has caused me similar trouble)

JoseBSL commented 3 years ago

Yes, I'm on Mac too, macOS Mojave Version 10.14.6, maybe time to update?

Yes when iI knit just index.Rmd I also get a similar error. The weird thing is that I don't get it for the bookdown project that I'm working with.

output file: _main.knit.md

pandoc: unrecognized option `--lua-filter=scripts_and_filters/colour_and_highlight.lua'
Try pandoc --help for more information.
Error: pandoc document conversion failed with error 2
In addition: Warning messages:
1: In yaml::yaml.load(..., eval.expr = TRUE) :
  NAs introduced by coercion: 10,052 is not an integer
2: In yaml::yaml.load(..., eval.expr = TRUE) :
  NAs introduced by coercion: 10,052 is not an integer
3: In yaml::yaml.load(..., eval.expr = TRUE) :
  NAs introduced by coercion: 10,052 is not an integer
Execution halted
ulyngs commented 3 years ago

strange - what happens if you remove the lua-filter option, given the error message?

JoseBSL commented 3 years ago

Yep, that works!

ulyngs commented 3 years ago

ok, it's not obvious to me why that happens.

Back to the topic of this issue: in the current version, can you confirm that you are able to use kableExtra by simply loading the kableExtra library and not needing to add additional latex packages explicitly?

JoseBSL commented 3 years ago

I confimed it Ulrik, no need to add extra packages with the most updated version of oxfordown, kableExtra works fine by just loading the library :)

Do I miss new things if I keep working with the "old one"?

ulyngs commented 3 years ago

I just flagged the current version as a release and documented the changes here: https://github.com/ulyngs/oxforddown/releases/tag/3.0