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:
... and my options seem to be:
<span>
explicitly for HTML, and \textcolor
explicitly for PDF via Latex, which forces me to keep two versions of the same markdown documentviolets are [blue]{color="blue"}
- which will definitely not be parsed by whatever Markdown-to-HTML engines used by Gitlab and suchSo, 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?
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{}{} .
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.
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.
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.
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:
(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+);?")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With