Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kableExtra question (Not a csasdown bug) #225

Open
SOLV-Code opened this issue Feb 7, 2023 · 5 comments
Open

kableExtra question (Not a csasdown bug) #225

SOLV-Code opened this issue Feb 7, 2023 · 5 comments

Comments

@SOLV-Code
Copy link

SOLV-Code commented Feb 7, 2023

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
@cgrandin
Copy link
Contributor

cgrandin commented Feb 7, 2023

Did you try escape = TRUE ?

@seananderson
Copy link
Member

seananderson commented Feb 7, 2023 via email

@SOLV-Code
Copy link
Author

SOLV-Code commented Feb 7, 2023

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
Copy link
Author

SOLV-Code commented Feb 12, 2023

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
Copy link
Author

SOLV-Code commented Feb 12, 2023

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")




Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants