svmiller / steveproj

An R package for getting academic projects/papers started.
http://svmiller.com/steveproj
30 stars 3 forks source link

Incorporate table/figure cross-references #8

Closed svmiller closed 3 years ago

svmiller commented 3 years ago

There are a few packaging options for this and I know my PDF template that I use now plays well with {bookdown}. Relatively well, that is. The default options in pdf_document2 and how they differ from pdf_document in {rmarkdown} are not 100% clear to me. Potentially worth figuring out though.

svmiller commented 3 years ago

Leaving this open now as I figure out the conditional evaluation problem this poses.

svmiller commented 3 years ago

I'm probably overthinking this. There is a simple if-else function to store in the table-making/figure-making chunk that conditionally formats based on the pandoc variables being TRUE (i.e. is_docx and is_latex). In this interpretation, the figure might be a teensy bit more problematic because I'm using the chunk to adjust the dpi for the Word figures and not the LaTeX figures.

svmiller commented 3 years ago

Now that we're on this topic, the bookdown::word_document2 does not appear to recognize the field for a title caption. It treats it as text body. This means there'd be no way around that in Word (because Word is very limited in what it can do for this purpose) and it would require manual formatting. At least, as far as I know it.

I suppose that's fair? This system isn't designed to publish to Word, only to make it suitable for anonymous peer review. The user is probably having to stick those chunks at the end of the document and on their own dedicated pages anyhow. There's invariably going to be some minor futzing to do here.

svmiller commented 3 years ago

Okay, solution ahoy. I'll need to update the post on my blog on this.

Gone go the conditional chunks for tables and figures. Instead, we have one chunk. For this to work (well and for cases of conditional table formatting), go to the setup chunk. At the top of it (before loading libraries and data, put this stuff there.

is_docx <- knitr::opts_knit$get("rmarkdown.pandoc.to") == 'docx'
is_latex <- knitr::opts_knit$get("rmarkdown.pandoc.to") == 'latex'

# I don't know how Texas Instruments smart this is, but p-sure default DPI is 96.
# That's not a problem for LaTeX, but it looks not-so-great for Word.
# For Word, let's up that to 600. This should allow for cross-referencing 
# in {bookdown} while allowing for conditional DPI

if (is_latex) {
  conditional_dpi <- 96
  return(conditional_dpi)
} else if (is_docx) {
  conditional_dpi <- 600
  return(conditional_dpi)
} else {}

knitr::opts_chunk$set(echo = FALSE,
                      message=FALSE, warning=FALSE,
                      fig.path='doc/figs/',
                      cache.path = 'doc/_cache/',
                      fig.width = 8.5, dpi = conditional_dpi,
                      fig.process = function(x) {
                      x2 = sub('-\\d+([.][a-z]+)$', '\\1', x)
                      if (file.rename(x, x2)) x2 else x
                      })

What's happening here: we move the is_docx and is_latex stuff to the top, before anything else. Importantly, but for purely cosmetic sake, we will conditionally place the dpi. By default, it's 96 for R Markdown. That works well for PDF images, but not-so-well for PNG images. Thus, if is_docx is TRUE, set conditional_dpi to 600. If is_latex is TRUE, have it be the default (96).

Figures are straightforward, as you would any chunk. The DPI is conditionally set. However, *if you elect to format your tables and acknowledging that right now you'll need {huxtable} for Word and {kableExtra} for LaTeX for tables created by {modelsummary}, you're going to want to do something like this. Note that this is just basically ganking the previous two table code chunks and sticking them in a gaudy if in the code chunk.

if (is_latex) {
  tab_reg %>%
  row_spec(0, bold=TRUE) %>%
  pack_rows("The Important Stuff",1, 12) %>%
  pack_rows("Stupid Thing I Added", 13, 14)
} else if (is_docx) {
  tab_reg %>%
  set_font_size(10) %>%
  set_left_padding(1) %>%
  set_top_padding(1) %>%
  set_bottom_padding(1) %>%
  set_right_padding(1) %>%
  insert_row(., "Stupid Thing I Added", "", "", after=13) %>%
  insert_row(., "The Important Stuff","", "", after=1) %>%
  set_bold(., 1, 1:3, TRUE) %>%
  set_bold(2, 1:3, TRUE) %>%
  set_bold(15, 1:3, TRUE) %>%
  as_flextable()
} else {
  print("No other format available.")
}

Cross-reference as you would do in {bookdown}, per the manual.