Skip to content

Creating tables

E. F. Haghish edited this page Dec 3, 2018 · 9 revisions

MarkDoc has a specific command for generating tables called tbl. However, there are other possibilities for generating a table in MarkDoc which are covered in this document. Most of the users will find the tbl command much handier and more versatile! Furthermore, the tbl command is also supported in MarkDoc mini mode, and thus is the recommended method for creating dynamic tables. This page is just providing an overview of alternative procedures for writing dynamic tables in MarkDoc.


In general, the tables that you include in the dynamic document can be divided in two groups:

  1. static tables, where the content of the table does not include scalars or macros
  2. dynamic tables, where the table includes scalar or macro values.

Static tables

Any of the supported markup languages (Markdown, HTML, LaTeX) can be used to create a table. Here are simple examples of Markdown, HTML, and LaTeX tables that generate a 3 by 3 table.

Table 1. preview of the example table written in Markdown

Animals Sports Fruits
Cat Soccer Apple
Dog Basketball Orange

All of these examples will render a table in the dynamic document which looks like the table above. However, it would be a good idea to compare these markup languages to one another.

Example 1. Markdown table

/***
| __Animals__ | __Sports__ | __Fruits__ |
|-------------|------------|------------|
| Cat         | Soccer     | Apple      |
| Dog         | Basketball | Orange     |
***/

Example 2. LaTeX table

/***
\begin{table}[]
\centering
\caption{My caption}
\label{my-label}
\begin{tabular}{|l|l|l|}
\hline
\textbf{Animals} & \textbf{Sports}  & \textbf{Fruits} \\ \hline
Cat     & Soccer     & Apple  \\ \hline
Dog     & Basketball & Orange \\ \hline
\end{tabular}
\end{table}
***/

Example 3. HTML table

/***
<table class="tg">
  <tr>
    <th class="tg-yw4l"><b>Animals</b></th>
    <th class="tg-yw4l"><b>Sports</b></th>
    <th class="tg-yw4l"><b>Fruits</b></th>
  </tr>
  <tr>
    <td class="tg-yw4l">Cat</td>
    <td class="tg-yw4l">Soccer</td>
    <td class="tg-yw4l">Apple</td>
  </tr>
  <tr>
    <td class="tg-yw4l">Dog</td>
    <td class="tg-yw4l">Basketball</td>
    <td class="tg-yw4l">Orange</td>
  </tr>
</table>
***/

As evident from the examples, the Markdown code is much more human readable compared to HTML and LaTeX. Using an easy-to-read markup language such as Markdown for writing documentation improves the readability of your script files.

Dynamic tables

The problem with the examples above is that the content of the tables is static, i.e. it does not change. Often, when you are presenting the results of your data analysis and interpreting them, you need to create a table using the results returned from the analysis.

One solution to this problem is to use the txt command for adding the code to the smcl log file. For example, let's assume that you are writing your documentation using Markdown, and you want to create a table similar to the previous 3 by 3 table, however, you want to obtain the values from local macros.

Example 4. Dynamic table problem

local l1 Cat
local l2 Dog
local l3 Soccer
local l4 Basketball
local l5 Apple 
local l6 Orange

txt                                             ///
"| __Animals__ | __Sports__ | __Fruits__ |"  _n ///
"|-------------|------------|------------|"  _n ///
"| `l1'        | `l3'       | `l5'       |"  _n ///
"| `l2'        | `l4'       | `l6'       |"  _n

This example will be rendered similar to the Table 1. But creating such a table requires a lot of work. MarkDoc provides a simpler method for creating a dynamic table, which is using the tbl command. This command is documented in a separate section.