Skip to content

Commit

Permalink
Merge pull request #542 from crsh/word-fixes
Browse files Browse the repository at this point in the history
Several improvements for knitting to Word
  • Loading branch information
crsh committed Apr 20, 2023
2 parents c39033a + 83cae8b commit 1c488f7
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
4 changes: 2 additions & 2 deletions R/apa_print_emm_lsm.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ apa_print.summary_emm <- function(
if(is.null(adjust)) {
conf_level <- paste0(conf_level, "\\% CI")
} else {
conf_level <- paste0("$", conf_level, "\\%\\ \\mathrm{CI}_\\mathrm{\\scriptsize ", adjust["conf.int"], "}$")
conf_level <- paste0("$", conf_level, "\\%\\ \\mathrm{CI}_\\mathrm{\\scriptstyle ", adjust["conf.int"], "}$")
}
}

Expand Down Expand Up @@ -245,7 +245,7 @@ apa_print.summary_emm <- function(
variable_labels(tidy_x[[p_value]]) <- switch(
p_value
, "p.value" = "$p$"
, "adj.p.value" = paste0("$p_\\mathrm{\\scriptsize ", adjust["p.value"], "}$")
, "adj.p.value" = paste0("$p_\\mathrm{\\scriptstyle ", adjust["p.value"], "}$")
)
}

Expand Down
22 changes: 4 additions & 18 deletions R/apa_table.R
Original file line number Diff line number Diff line change
Expand Up @@ -630,38 +630,24 @@ apa_table.markdown <- function(
table_output <- do.call(function(...) knitr::kable(x, format = "pandoc", ...), ellipsis)
apa_terms <- options()$papaja.terms

caption <- paste0("*", caption, "*")
# This uses pandoc syntax (start line with "Table:") to define a "proper"
# table caption, which will receive a table caption style in DOCX
current_chunk <- knitr::opts_current$get("label")
if(!is.null(current_chunk)) caption <- paste0("<caption>(\\#tab:", current_chunk, ")</caption>\n\n<div custom-style='Table Caption'>", caption, "</div>\n\n")

# Print table
# cat("<caption>")
# cat(apa_terms$table, ". ", sep = "")
# cat(caption)
# cat("</caption>\n")

# print(res_table)
if(!is.null(current_chunk)) caption <- paste0("Table: (\\#tab:", current_chunk, ") ", caption, "\n\n")

table_output <- c(caption, table_output)

if(!is.null(note)) {
# cat("\n")
# cat("<center>")
# cat("*", apa_terms$note, ".* ", note, sep = "")
# cat("</center>")
# cat("\n\n\n\n")

table_output <- c(
table_output
, "\n<div custom-style='Compact'>"
, "\n<div custom-style='table-note'>"
, paste0("*", apa_terms$note, ".* ", note)
, "</div>\n\n&nbsp;\n\n"
)
}

knitr::asis_output(paste(table_output, collapse = "\n"))
# table_output <- knitr::asis_output(table_output)
# table_output
}


Expand Down
5 changes: 3 additions & 2 deletions R/word_title_page.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ word_title_page <- function(x) {

if(length(author_note) > 0) {
author_note <- c(
"<div custom-style='Author'>", apa_terms$author_note, "</div>"
"<div custom-style='authornote-title'>", apa_terms$author_note, "</div>"
, "\n"
, "<div custom-style='authornote'>"
, paste(author_note, collapse = "\n\n")
, "</div>"
)
}

Expand Down Expand Up @@ -78,7 +80,6 @@ word_title_page <- function(x) {

c(
author_information
, "\n\n&nbsp;\n\n&nbsp;\n\n&nbsp;\n\n&nbsp;\n\n&nbsp;\n\n"
, author_note
, "\n"
, ifelse(is.null(x$abstract), "", abstract)
Expand Down
33 changes: 22 additions & 11 deletions inst/lua/docx_fixes.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,29 @@ function Header (elem)
end

function Image (img)
if img.caption[1] ~= nil then
img.caption[1] = pandoc.Emph(img.caption[1])
img.caption[3] = pandoc.Emph(string.gsub(pandoc.utils.stringify(img.caption[3]), ":", ". "))
local caption = img.caption

if caption[1] ~= nil then
caption[1] = pandoc.Emph(caption[1])
caption[3] = pandoc.Emph(string.gsub(pandoc.utils.stringify(caption[3]), ":", ". "))
end
return img
end

-- function Table (table)
-- print(pandoc.utils.stringify(table.caption))
-- if table.caption[1] ~= nil then
-- print(pandoc.utils.stringify(table.caption))
-- table.caption[3] = pandoc.Emph(string.gsub(img.caption[3].c, ":", ""))
-- end
-- return table
-- end
function Table (tbl)
local caption = tbl.caption.long[1].content

if caption[1] ~= nil then
caption[3] = pandoc.Str(string.gsub(pandoc.utils.stringify(caption[3]), ":", ""))
caption[4] = pandoc.LineBreak()

for i = 5, (#caption) do
caption[i] = pandoc.Emph(caption[i])
end
end

tbl.caption.long[1].content = caption

return tbl
end

Binary file modified inst/rmarkdown/templates/apa6/resources/apa6_man.docx
Binary file not shown.
Binary file modified inst/rmarkdown/templates/apa6/resources/apa6_man_.docx
Binary file not shown.

0 comments on commit 1c488f7

Please sign in to comment.