Julia Markdown - Chunk Output as Markdown

Viewed 132

I would like to print a table in Julia Markdown. To the best of my knowledge there is no cool package, yet, that is doing this. Hence, I would like to create a nice looking table through code, but I can't figure out how.

This is my table code...

---
title: Just a test
author: Me
date: 2022-01-03
output: pdf_document
---

```julia 
"""
| Column One | Column Two | Column Three |
|:---------- | ---------- |:------------:|
| Row `1`    | Column `2` |              |
| *Row* 2    | **Row** 2  | Column ``3`` |
"""
``` 

...and I want it to produce this...

enter image description here

...instead of this:

enter image description here

1 Answers

The Markdown standard library can parse tables too:

julia> tbl = """
       | Column One | Column Two | Column Three |
       |:---------- | ---------- |:------------:|
       | Row `1`    | Column `2` |              |
       | *Row* 2    | **Row** 2  | Column ``3`` |
       """
julia> md = Markdown.parse(tbl);

julia> # text formatting like emphasis and bold are lost in pasting
       # to StackOverflow, but shown in the original output
       md

  Column One Column Two Column Three
  –––––––––– –––––––––– ––––––––––––
  Row 1        Column 2             
  Row 2           Row 2   Column 3  

The parse output is a Markdown.MD object that is rendered appropriately depending on your output display (i.e. terminal, Jupyter, etc).

Table as rendered in Pluto

If you want to produce a markdown table directly from data (without parsing it from a string), you can also construct a Markdown.Table directly; check the varinfo() function from the InteractiveUtils standard library for an example of that.

Related