pillow
pillow

Reputation: 43

PYTHON/pypandoc: Can pypandoc be used to convert Rmd files to html?

I'm trying to write python code that can convert an rmd file to html. Rmd was not listed as one of the built in formats for pypandoc, so I tried to use regular markdown for the format parameter.

output = pypandoc.convert_file(
            f"{file_path}/{file}", 'html', format = 'markdown', outputfile= f"{file_path}/{file}.html", encoding = 'utf-8')

But I get the error that it could not be converted "[WARNING] Could not convert TeX math site."

Is there another way to convert the file without changing the TeX math?

Upvotes: 2

Views: 123

Answers (1)

atomasevic
atomasevic

Reputation: 356

It depends on the style of TeX math content used in the Rmd file, but if you used dollar signs, something like this should work

output = pypandoc.convert_file(
    f"{file_path}/{file}",
    'html',
    format='markdown+tex_math_dollars',
    outputfile=f"{file_path}/{file}.html",
    encoding='utf-8'
)

You can find examples for other styles (e.g. single backslash) here: https://pandoc.org/MANUAL.html#extension-tex_math_single_backslash

Also, you can take a look at https://pypi.org/project/knitpy/ which is a port of knitr for Python, as there are elements of RMarkdown which won't be recognized by pandoc.

Upvotes: 0

Related Questions