<- function(id, file, blockquote = TRUE) {
get_quote <- ifelse(blockquote, "\n> ", "")
blocktext
<- function(x) {
refreplace # List of sequence types
<- c(`@fig-` = "Figure",
crossrefs `@fig-` = "Table",
`@eq-` = "Equation",
`@thm-` = "Lemma",
`@lem-` = "Corollary",
`@cor-` = "Proposition",
`@prp-` = "Conjecture",
`@cnj-` = "Definition",
`@def-` = "Example",
`@exm-` = "Example",
`@exr-` = "Exercise",
`apafg-` = "Figure",
`apatb-` = "Figure"
)
# Find all crossreference types and number them
<- function(x, reftype, prefix) {
make_replacements <- ifelse(stringr::str_starts(reftype, "\\@"),
regstring paste0("\\", reftype, "(.*?)(?=[\\.\\?\\!\\]\\}\\s,])"),
paste0("\\{", reftype, "(.*?)\\}"))
<- stringr::str_extract_all(string = x, pattern = regstring) |>
patterns unlist() |>
unique() |>
::str_replace("\\{", "\\\\{") |>
stringr::str_replace("\\}", "\\\\}")
stringr
if (all(is.na(patterns))) return(NULL)
<- paste(prefix, seq_along(patterns))
replacements names(replacements) <- patterns
replacements
}<- purrr::map2(
allreplacements names(crossrefs),
crossrefs,make_replacements(
\(rt, pf) x = x,
reftype = rt,
prefix = pf)) |>
unlist()
::str_replace_all(x, allreplacements)
stringr
}
<- readLines(file) |>
filetext refreplace()
<- sum(stringr::str_count(filetext, paste0("#", id)))
idcount if (idcount > 1)
stop(paste0(
"The id (",
id ,") is not unique. There are ",
idcount," instances of id = ",
id,"."
))
<- filetext |>
s paste0(collapse = "\n") |>
::str_match(pattern = paste0("(?<=\\[).+(?=\\]\\{\\#",
stringr
id,"\\})")) |>
getElement(1)
if (is.na(s)) {
<- filetext |>
s paste0(collapse = "|||") |>
::str_match(pattern = paste0(":::\\{\\#",
stringr
id,"\\}(.*?):::")) |>
getElement(2) |>
::str_replace_all("\\|\\|\\|", blocktext)
stringrelse {
} <- paste0(blocktext, s)
s
}
if (is.na(s)) stop("Could not find a div or span with id = ", id)
s }
While assembling a reply letter for a revise-and-resubmit document, I was documenting for reviewers how we addressed their concerns. The reply letter quoted numerous sentences and paragraphs that had been changed. Unfortunately, each time my co-authors and I edited the paper, I lost track of which sentences and paragraphs I had previously copied into the reply letter. I did not want to misquote my own paper.
Copying-and-pasting with each edit was getting tedious, and it was an error-prone process. I decided to write a function that would retrieve a named div or span from the paper and print it as a quote in my reply letter. Here it is:
For your convenience and mine, I have added this function into the WJSmisc package.
Using the get_quote
function
If the file has a span or div with a specified id, the get_quote
function can find it. Suppose the file has a span with id of id1
and a div with id of id2
.
[Text in a span]{#id1}
:::{#id2}
Here are two pagagraphs in a fenced div.
This is the second paragraph.
:::
cat(get_quote("id2", "index.qmd"))
You can pull text from the current file or another file. This file happened to be named index.qmd
. The text inside the span with id id1
can be extracted with inline chunks like so:
`r get_quote("id1", "index.qmd")`
Text in a span
Here we extract the two paragraphs in div id2
`r get_quote("id2", "index.qmd")`
Here are two pagagraphs in a fenced div.
This is the second paragraph.
Remove blockquote formatting
By default, the function adds a >
before each line in the quoted markdown so that it appears as a block quote. You can turn off the block quote formatting like so:
`r get_quote("id1", "index.qmd", blockquote = FALSE)`
Text in a span
Curly braces with additional information
What if you need to put additional information in the curly braces of the span or div (e.g., a css class)?
[Here is more text with extra stuff in the curly braces.]{#id3 .myclass}
You can trick the function into thinking that the extra stuff is part of the id like so:
`r get_quote("id3 .myclass", "index.qmd")`
Here is more text with extra stuff in the curly braces.
Citation
@misc{schneider2023,
author = {Schneider, W. Joel},
title = {Insert {Text} from {One} {Markdown} {Document} into
{Another}},
date = {2023-03-30},
url = {https://wjschne.github.io/posts/insert-text-from-one-markdown-document-into-another/},
langid = {en}
}