cites_convert: Convert citations between LaTeX and Markdown formats

View source: R/cites_convert.R

single_cites_convertR Documentation

Convert citations between LaTeX and Markdown formats

Description

This function converts citations in a given text between LaTeX and Markdown formats. It can handle multiple LaTeX citation styles and produces the appropriate format based on the specified direction. Note that the input text must have a length of 1.

the cites_convert function applies single_cites_convert to each element of the input vector.

Usage

single_cites_convert(
  text,
  pattern = NULL,
  tex2md = TRUE,
  latex_prefix = c("cite", "upcite", "citep", "citet")
)

cites_convert(x, ...)

Arguments

text

A character string containing the text with citations, Note that the input text must have a length of 1.

pattern

A regular expression pattern for matching citations. If NULL, defaults to a pattern based on the citation direction.

tex2md

Logical; if TRUE, converts from LaTeX to Markdown; if FALSE, converts from Markdown to LaTeX.

latex_prefix

A character vector of LaTeX citation prefixes to use. Defaults to c("cite", "upcite", "citep", "citet").

x

A character vector containing texts with citations.

...

Additional arguments passed to single_cites_convert.

Value

the single_cites_convert function return a character string with citations converted to the specified format.

the cites_convert function return a character vector with citations converted to the specified format.

Examples

# Example text containing various LaTeX citations
# Convert the LaTeX citations in the text to Markdown format
text = "This is a citation \\upcite{key1,key2} and \\citep{key3-a }.
        paper \\cite{key3-b, cky3-c}, paper \\citet{key4-a, cky4-b}"
converted_md = single_cites_convert(text, tex2md = TRUE)
print(converted_md)



# Example text containing Markdown citations
# Convert the Markdown citations back to LaTeX format

text = "This is a citation [@key1; @key2] and [@key3-a].
        paper [@key3-b; @cky3-c], paper [@key4-a; @cky4-b]"
# defalut prefix "cite"
converted_latex = single_cites_convert(text, tex2md = FALSE)
print(converted_latex)

# Convert Markdown citations to LaTeX using a custom prefix
custom_prefix = single_cites_convert(text, tex2md = FALSE, latex_prefix = "upcite")
print(custom_prefix)


# A vector of texts with Markdown citations
tex =c("This is a citation [@key1; @key2] and [@key3-a].",
        "This is a citation2 [@key1; @key2] and [@key3-a].",
        "this is no citation",
        "",
        "This is a citation3 [@key1; @key2] and [@key3-a].")

# vector version
tex = cites_convert(tex, tex2md = FALSE)
print(tex)



# Another vector of texts containing LaTeX citations
tex2= c("This is a citation \\upcite{key1,key2} and \\citep{key3-a }",
         "This is a citation \\citet{key1,key2} and \\citet{key3-a }",
         "this is no citation",
         "",
         "This is a citation \\upcite{key1,key2} and \\cite{key3-a }")
# Convert the vector of LaTeX citations to Markdown format
tex2 = cites_convert(tex2, tex2md = TRUE)
print(tex2)



zoushucai/journalabbr documentation built on Dec. 6, 2024, 4:41 p.m.