Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using span (f)or font color in Pandoc markdown for both html and pdf?

I would like to write in Markdown, then use either <font color="red">red text</font> or <span style="color: red;">red text</span> to color text, so that when the .md file is checked in say Gitlab, the font color is automatically parsed when browsing the HTML formatted version - but I also want to use the same .md file as a source for a PDF via (xe)latex.

I have seen:

  • How to change the font color?
  • https://bookdown.org/yihui/rmarkdown-cookbook/font-color.html

... and my options seem to be:

  • Use <span> explicitly for HTML, and \textcolor explicitly for PDF via Latex, which forces me to keep two versions of the same markdown document
  • Use Lua filter, and write like violets are [blue]{color="blue"} - which will definitely not be parsed by whatever Markdown-to-HTML engines used by Gitlab and such

So, I was thinking - it must be possible, that I write <span> or <font> in my file (which would/should be recognized by Gitlab and such parsers), and then have a Lua filter for pandoc, that would transform these into \textcolor, when using the .md file as a source to create PDF.

Unfortunately, I suck at Lua, and definitely do not know the internal document model of Pandoc enough, to be able to easily guess how could I get to these kinds of tags in a Markdown file. So my question is: is there an existing filter or setting in pandoc already that does this - or lacking that, a Lua filter script that looks up such tags in a markdown document, that I could use a base for this kind of filtering?

like image 700
sdbbs Avatar asked Jul 10 '20 09:07

sdbbs


People also ask

How do I change the font color on my RMD?

The Markdown syntax has no built-in method for changing text colors. We can use HTML and LaTeX syntax to change the formatting of words: For HTML, we can wrap the text in the <span> tag and set color with CSS, e.g., <span style="color: red;">text</span> . For PDF, we can use the LaTeX command \textcolor{}{} .

How do I use pandoc Markdown PDF?

Generating PDF from Markdown with Pandoc There are actually two steps involved in converting a Markdown file to a PDF file: The Markdown source file is converted to a LaTeX source file. Pandoc invokes the pdflatex or xelatex or other TeX command and converts the . tex source file to a PDF file.

Can pandoc convert HTML to PDF?

Using Pandoc to automate the boring stuff Pandoc is great for converting code documentation for source control (Hello, reStructuredText to Markdown), but it's also useful for converting HTML help docs embedded in apps to PDF.

Can pandoc convert from PDF?

You can't. You can try opening the PDF in Word or Google Docs and saving in a format from which pandoc can convert directly.


2 Answers

Ok, I think I got somewhere - this is color-text-span.lua (is a bit crappy, because the regex explicitly demands a space after the color: colon, but hey - better than nothing):

-- https://stackoverflow.com/questions/62831191/using-span-for-font-color-in-pandoc-markdown-for-both-html-and-pdf
-- https://bookdown.org/yihui/rmarkdown-cookbook/font-color.html
-- https://ulriklyngs.com/post/2019/02/20/how-to-use-pandoc-filters-for-advanced-customisation-of-your-r-markdown-documents/

function Span (el)
  if string.find(el.attributes.style, "color") then
    stylestr = el.attributes.style
    thecolor = string.match(stylestr, "color: (%a+);")
    --print(thecolor)
    if FORMAT:match 'latex' then
      -- encapsulate in latex code
      table.insert(
        el.content, 1,
        pandoc.RawInline('latex', '\\textcolor{'..thecolor..'}{')
      )
      table.insert(
        el.content,
        pandoc.RawInline('latex', '}')
      )
      -- returns only span content
      return el.content
    else
      -- for other format return unchanged
      return el
    end
  else
    return el
  end
end

Test file - test.md:

---
title: "Test of color-text-span.lua"
author: Bob Alice
date: July 07, 2010
geometry: margin=2cm
fontsize: 12pt
output:
  pdf_document:
    pandoc_args: ["--lua-filter=color-text-span.lua"]
---

Hello, <span style="color: red;">red text</span>

And hello <span style="color: green;">green text</span>

Call command:

pandoc test.md --lua-filter=color-text-span.lua --pdf-engine=xelatex -o test.pdf

Output:

output

like image 168
sdbbs Avatar answered Sep 20 '22 15:09

sdbbs


(appreciate if someone can make this a comment, I have too low reputation to comment)

If you remove the space in the regex pattern you can omit the explicit space. Or even better allow for whitespaces and a non-mandatory semi colon:

Change the line thecolor = string.match(stylestr, "color: (%a+);") to thecolor = string.match(stylestr, "color:%s*(%a+);?")

like image 25
Bennix Deprîx Avatar answered Sep 17 '22 15:09

Bennix Deprîx