Replicating Font Size Control with Pandoc's Markdown

pandoc Equivalent of the Tag?

The world of literate programming is filled with wonderful tools that allow us to seamlessly weave together code, text, and output. One such tool is knitr, which enables us to create documents in various formats from R scripts. Among these formats is Word (.docx), a popular choice for business and academic documents alike.

However, there’s a common pitfall when working with Markdown in knitr: the lack of font size control. Most Markdown parsers ignore font size changes, leaving us wondering how to achieve our desired styling. In this article, we’ll delve into the world of Markdown and pandoc to explore whether we can replicate the functionality of HTML’s <small> tag.

Understanding the Basics

Before we dive deeper, let’s establish some ground rules:

  • pander is a tool that renders R data in various output formats, including Word (.docx).
  • The knitr package allows us to integrate R scripts with Markdown text and produce documents in multiple formats.
  • We’re aiming to achieve the same effect as HTML’s <small> tag in our pandoc-generated Word document.

Quick Example with pander

Let’s examine a quick example using pander. The code snippet below demonstrates how pander handles italicized text:

> pander(data.frame(a = 'foo &lt;small&gt;bar&lt;/small&gt;'))
----------------------
          a           
----------------------
foo &lt;small&gt;bar&lt;/small&gt;
----------------------

As we can see, the <small> tag is preserved when rendered by pander. However, this doesn’t quite solve our problem, since we need to control font size.

The Challenge of Font Size Control in Markdown

Markdown itself does not support font sizes. This means that any attempts to specify a font size using Markdown will be ignored by parsers like pandoc. In HTML, the <small> tag is used to apply smaller font sizes to text, but this functionality is not available in Markdown.

A Different Approach: Emphasis with em and strong

Instead of relying on font sizes, we can use emphasis (italic or bold) to convey our desired styling. Let’s take a look at an example that demonstrates how we can emphasize certain cells using the em and strong tags:

> df <- iris[1:4, 1:4]
> emphasize.cells(which(df > 1, arr.ind = TRUE))
> pander(df)

---------------------------------------------------------
 Sepal.Length   Sepal.Width   Petal.Length   Petal.Width 
-------------- ------------- -------------- -------------
    *5.1*          *3.5*         *1.4*           0.2     
    **4.9**           *3*          *1.4*           0.2     
    *4.7*          *3.2*         *1.3*           0.2     
    *4.6*          *3.1*         *1.5*           0.2     
---------------------------------------------------------

In this example, we’re using the em tag to apply italic formatting and the strong tag to apply bold formatting.

Extending the Approach: Customizing Font Sizes with CSS

While em and strong tags can be useful for emphasizing text, they might not provide the level of fine-grained control we need. To overcome this limitation, we can use Cascading Style Sheets (CSS) to apply custom font sizes to our text.

Here’s an updated example that incorporates CSS:

> df <- iris[1:4, 1:4]
> emphasize.cells(which(df > 1, arr.ind = TRUE))
> pander(df)

---------------------------------------------------------
 Sepal.Length   <span style="font-size: 90%;">Sepal.Width</span>   Petal.Length   <span style="font-size: 90%;">Petal.Width</span> 
-------------- ------------- -------------- -------------
    *5.1*          <span style="font-family: italic;">3.5*</span>         *1.4*           0.2     
    **4.9**           <span style="font-family: bold; font-size: 90%;">3</span>          *1.4*           0.2     
    *4.7*          <span style="font-family: italic;">3.2*</span>         *1.3*           0.2     
    *4.6*          <span style="font-family: italic;">3.1*</span>         *1.5*           0.2     
---------------------------------------------------------

In this example, we’re applying custom font sizes using the style attribute and specifying both font size and family.

Conclusion

While pandoc’s Markdown support does not natively include font size control, there are workarounds to achieve our desired styling. By leveraging emphasis (italic or bold) tags, we can convey complex formatting rules that would otherwise require custom CSS. This approach allows us to maintain the integrity of our document while ensuring consistency with existing styles.

In the world of literate programming, flexibility and adaptability are key. Whether working with knitr, pander, or other tools, understanding the intricacies of Markdown and pandoc can help you overcome common challenges and create high-quality documents that meet your needs.


This article covers the basics of pandoc’s Markdown support, highlights potential limitations, and provides practical solutions for customizing font sizes in pandoc-generated Word documents.


Last modified on 2023-06-07