csasdown icon indicating copy to clipboard operation
csasdown copied to clipboard

kableExtra question (Not a csasdown bug)

Open SOLV-Code opened this issue 2 years ago • 5 comments

The issue

I'm trying to have some cross-references to sections inside a table generated with csas_table(). Searching online I found lots of examples of cross-references to a table, but only one short note here re: cross-reference in a table . However, I can't make their instruction re: "escaping the backslash" work.

This Vignette on creating tables with kableExtra also states on p 14 in Section Cell/Text Specification that "you have to escape special symbols including % manually by yourself", but doesn't give a working example.

Current Markdown



table.in <- read_csv("data/Reference Tables/Acronyms.csv") 

table.in  %>%     
   mutate_all(function(x){gsub("&", "\\\\&", x)}) %>% 
   mutate_all(function(x){gsub("%", "\\\\%", x)}) %>%
   mutate_all(function(x){gsub("\\\\n","\n", x)}) %>%
   csas_table(format = "latex", escape = FALSE, font_size = 10,align = c("l","l","l"),
                  caption = "(ref:TableAcronyms)")  %>%
	kableExtra::row_spec(1:dim(table.in)[1]-1, hline_after = TRUE) %>%
     kableExtra::column_spec(1, width = "4em") %>%
     kableExtra::column_spec(2, width = "24em") %>%
     kableExtra::column_spec(3, width = "12em")

Source csv

The source file looks like this

Col 1 Col2 Details
Text Text **cross-reference goes here
Text Text **cross-reference goes here

For the cross-ref, I've tried:

-  \\@ref(SRModels) : gives number of cross-referenced section, but in a new row in the first column
-  \\\@ref(SRModels) : gives ```ref(SRModels)``` in a new row in the first column, after a blank row
-  \\\\@ref(SRModels) : gives number of cross-referenced section in a new row in the first column, after a blank row

SOLV-Code avatar Feb 07 '23 06:02 SOLV-Code

Did you try escape = TRUE ?

cgrandin avatar Feb 07 '23 16:02 cgrandin

Is the output of your table code LaTeX not markdown and so you need LaTeX formatted cross references? If you can’t get that to work, try making a small reproducible example of some code we can paste into a csasdown document with small fake data created with R code and we can troubleshoot.

seananderson avatar Feb 07 '23 16:02 seananderson

Thank you for the quick responses! I've been tinkering with this, on and off, for a long while, but then thought I might be missing something totally silly and obvious, so started this issue. Also note: this cross-reference column is a "nice to have", not an essential revision of the res doc, so not worth going down any major rabbit holes. Just bugging me that I can't get it to work...

  • Just tried escape = TRUE: This removes all the line breaks, but just displays the cross-reference text as is, for all the variations listed above

  • The R-generated sample input is below. Note that anything with an odd number of backslashes fails in R with "Error: '@' is an unrecognized escape in character string starting ""Sec. \@""

acro.df <- data.frame(
	
	"Short Form" = 	c("BLDP","FSC","HCR"),
	Expansion = c("Babine Lake Development Project","Food, Social, and Ceremonial Fisheries",
								"Harvest Control Rule"),
	"Further Detail" = c("Sec. \\\\\\@ref(ChannelReview)","Sec. \\\\@ref(Project)","Sec. \\@ref(SimMethods)")
		
	)


acro.df

SOLV-Code avatar Feb 07 '23 17:02 SOLV-Code

Tried this approach and it sort of worked. The trick is to use format = "markdown" instead of format = "latex" in the call to csas_table(). With this I can have regular markdown cross-references \@ref(SRmethods) as well as bibtex citations like @WSP2005 or [@WSP2005] in the csv source file. But I have no idea why this works.

The resulting table format looks alright, but the subsequent specifications with row_spec() and column_spec() don't seem to work...

SOLV-Code avatar Feb 12 '23 17:02 SOLV-Code

Source file

ShortForm Expansion Further Detail
A Text @BibEntry1 Sec. \@ref(Project)
B Text [@BibEntry1] Sec. \@ref(ChannelReview)

Markdown

```{r TableAcronyms, echo = FALSE, results = "asis"}

table.in <- read_csv("data/Reference Tables/Acronyms.csv") 

table.in  %>%     
   mutate_all(function(x){gsub("&", "\\\\&", x)}) %>% 
   mutate_all(function(x){gsub("%", "\\\\%", x)}) %>%
   mutate_all(function(x){gsub("\\\\n","\n", x)}) %>%
   csas_table(format = "markdown", escape = FALSE, font_size = 10,align = c("l","l","l"),
                  caption = "(ref:TableAcronyms)")  %>%
	kableExtra::row_spec(1:dim(table.in)[1]-1, hline_after = TRUE) %>%
     kableExtra::column_spec(1, width = "10em") %>%
     kableExtra::column_spec(2, width = "24em")




SOLV-Code avatar Feb 12 '23 17:02 SOLV-Code