Markdown to HTML Converter Logo

2 May 2024 ~ 14 min read

Can you make a table of contents in markdown


Did you know that a paper that is well organized can help people remember it by as much as 40%? It’s real! This interesting fact shows how important it is to have a clear and easy-to-find Table of Contents (ToC) in your Markdown files. A good table of contents (ToC) not only makes the text easier to read, but it also acts as a plan that makes it easy for people to find their way through the rest of the text.

Markdown is a lightweight script language that is popular with both developers and writers. It has a number of ways to make a table of contents. Getting good at this skill can make your papers much better in terms of organization and ease of use, whether you’re writing instructions, a blog post, or a technical report.

Here’s what you’ll learn in this article:

  • Built-In Directives: Utilizing Markdown’s native features to create a ToC.

  • Plugins and Extensions: Enhancing your workflow with tools like Markdown All in One for VSCode.

  • External Tools: Leveraging powerful options such as Pandoc for automated ToC generation.

  • Custom Solutions: Crafting a visually appealing and functional ToC using personalized formatting tips.

Built-In Directives

While Markdown itself doesn’t include built-in support for automatic ToCs, certain Markdown processors like GitHub Flavored Markdown (GFM) provide straightforward syntax for this purpose. These built-in options are great for quick and simple documents, offering an easy way to keep your readers oriented.

Plugins and Extensions

For those using Markdown editors like Visual Studio Code or Atom, numerous plugins can automate the creation of a ToC. These extensions not only save time but also ensure your ToC stays updated as you modify your document. We’ll explore popular options and their features to help you pick the best one for your needs.

External Tools

Pandoc, a universal document converter, offers robust capabilities for Markdown users. With a simple command, you can generate a comprehensive ToC that integrates seamlessly with your document. We’ll walk you through the process, highlighting the advantages and potential pitfalls of this approach.

Custom Solutions

Sometimes, a one-size-fits-all solution doesn’t quite fit. That’s where custom formatting comes in. By tweaking the Markdown syntax and using CSS for styling, you can create a ToC that not only functions well but also looks great. We’ll share tips and examples to inspire your own unique creations.

Mastering these techniques will not only make your documents more professional but also enhance the overall reader experience. Ready to dive in? Let’s explore these methods in detail

Markdown headings​

The different levels of headings that can be used in Markdown are denoted by the number of # characters at the beginning of the line, ranging from one # for the highest level to six # for the lowest level. Here is a detailed breakdown of each heading level and its formatting:

Heading Level Markdown Syntax Example Output
Level 1 # Heading 1

Heading 1

Level 2 ## Heading 2

Heading 2

Level 3 ### Heading 3

Heading 3

Level 4 #### Heading 4

Heading 4

Level 5 ##### Heading 5
Heading 5
Level 6 ###### Heading 6
Heading 6

These headings help organise and structure content in Markdown documents. They make it easier for readers to navigate through the text and understand the hierarchy of information. Each heading level decreases in font size and emphasis, with Level 1 being the largest and most prominent, and Level 6 being the smallest and least prominent.

In Markdown, these headings are not only useful for creating visually structured documents but also play a crucial role when converting Markdown to other formats like HTML. This ensures that the structure and readability of the document are maintained across different platforms and applications.

For instance, using these headings effectively allows for the automatic generation of a table of contents (TOC), which can be crucial for longer documents. Various Markdown editors and tools, such as pandoc, support this functionality, providing plugins or extensions that can automatically create and update a TOC based on the headings in the document.

Table of contents heading level​

To format heading levels in a Markdown table of contents (TOC), use hashtags (#) to denote different heading levels. This hierarchical structure aids readers in navigating through the document with ease. Each level of heading is distinguished by the number of hashtags:

  1. Main Heading: Denoted by a single #

  2. Subheading Level 1: Denoted by ##

  3. Subheading Level 2: Denoted by ###

  4. Subheading Level 3: Denoted by ####

  5. Subheading Level 4: Denoted by #####

These headings automatically generate a TOC when supported by Markdown processors or plugins. Here’s an example of how the TOC and corresponding headings would be formatted in Markdown:


# Table of Contents

1. [Main Heading](#main-heading)

1.1 [Subheading Level 1](#subheading-level-1)

1.1.1 [Subheading Level 2](#subheading-level-2)

1.1.1.1 [Subheading Level 3](#subheading-level-3)

Example Document Structure


# Main Heading

## Subheading Level 1

### Subheading Level 2

#### Subheading Level 3

##### Subheading Level 4

Automatic Table of Contents Generation

You can automate the TOC creation using tools like Markdown TOC plugins, which dynamically generate the TOC based on the headings in your document. For instance, in GitHub-flavored Markdown, adding [TOC] at the beginning of your document will create a TOC automatically.

Customizing Your Table of Contents

Customizing the TOC can enhance user navigation by including clickable links to headings. For this, each heading should be given an ID, making the TOC more interactive:


# Table of Contents

1. [Main Heading](#main-heading)

1.1 [Subheading Level 1](#subheading-level-1)

1.1.1 [Subheading Level 2](#subheading-level-2)

1.1.1.1 [Subheading Level 3](#subheading-level-3)

# Main Heading {#main-heading}

## Subheading Level 1 {#subheading-level-1}

### Subheading Level 2 {#subheading-level-2}

#### Subheading Level 3 {#subheading-level-3}

##### Subheading Level 4 {#subheading-level-4}

Practical Example

Here’s how you can format the heading levels in Markdown and how it would look like when rendered:


# Overview

## Introduction

## Chapter 1

### Section 1.1

## Inline table of contents​

To create an inline table of contents (TOC) in Markdown, you follow specific steps to format and generate links to sections within your document. This allows for seamless navigation and better organisation of your content. Here's a detailed guide to help you achieve this.

**Creating an Inline Table of Contents in Markdown**

1. **Format Heading Levels**: Use hashtags to define headings in your Markdown document. The number of hashtags determines the heading level:

```markdown

# Main Title

## Section 1

### Subsection 1.1

## Section 2
  1. Generate the TOC Manually: Create a list with links to each section. The links should match the heading text and be formatted using square brackets [ ] and parentheses ( ) with the heading text converted to lowercase, spaces replaced by hyphens:

- [Main Title](#main-title)

- [Section 1](#section-1)

- [Subsection 1.1](#subsection-11)

- [Section 2](#section-2)
  1. Insert the TOC: Place the TOC at the desired location in your document:

# Table of Contents

- [Main Title](#main-title)

- [Section 1](#section-1)

- [Subsection 1.1](#subsection-11)

- [Section 2](#section-2)

# Main Title

## Section 1

### Subsection 1.1

## Section 2
  1. Automate the TOC Generation: Use tools or extensions like Markdown All in One for Visual Studio Code or online Markdown editors like Dillinger to automate TOC generation. These tools scan your document and create a TOC based on the headings automatically.

  2. Customising the TOC: Adjust the appearance and links within the TOC as needed. You can use HTML tags to add styling or more complex structures:


<h2>Table of Contents</h2>

<ul>

<li><a href="#main-title">Main Title</a></li>

<li><a href="#section-1">Section 1</a>

<ul>

<li><a href="#subsection-11">Subsection 1.1</a></li>

</ul>

</li>

<li><a href="#section-2">Section 2</a></li>

</ul>

Example Markdown Document with TOC


# Table of Contents

- [Introduction](#introduction)

- [Getting Started](#getting-started)

- [Installation](#installation)

## Customizing table of contents generation​

Customizing the appearance of your table of contents (TOC) in Markdown involves several steps and techniques. Markdown itself has limited support for customization, but you can achieve more advanced formatting and appearance control using a combination of Markdown and HTML.

### Steps to Customize TOC in Markdown

#### 1. Using Markdown with HTML

To enhance the TOC's appearance, integrate HTML tags within your Markdown file. This method allows for styling and formatting flexibility.

**Example:**

```markdown

# Table of Contents

<div id="toc">

<ul>

<li><a href="#section1">Section 1</a></li>

<li><a href="#section2">Section 2</a></li>

<li><a href="#section3">Section 3</a></li>

</ul>

</div>

## Section 1

Content...

## Section 2

Content...

## Section 3

Content...

2. Styling with CSS

For further customization, use CSS to style the TOC. You can add a style block within your Markdown file or link to an external CSS file.

Inline CSS Example:


<style>

#toc {

background-color: #f9f9f9;

border: 1px solid #ddd;

padding: 10px;

}

#toc ul {

list-style-type: none;

padding-left: 0;

}

#toc li {

margin: 5px 0;

}

#toc a {

text-decoration: none;

color: #007bff;

}

#toc a:hover {

text-decoration: underline;

}

</style>

3. Automated TOC Generation with Pandoc

If you’re open to using Pandoc, it provides options to automatically generate a TOC with various customization features.

Pandoc Command Example:


pandoc -s --toc -c style.css yourfile.md -o output.html

Here, --toc adds the TOC, and -c style.css links a CSS file for styling.

4. Using RStudio for R Markdown (Rmd)

RStudio integrates with R Markdown, making it easy to generate and customize TOCs. You can specify TOC options in the YAML header.

YAML Header Example:


---

title: "Your Document"

output:

html_document:

toc: true

toc_depth: 3

toc_float: true

---

This configuration enables a floating TOC with a depth of 3 levels.

Example Section 1​

To create a table of contents (TOC) in Markdown, you can use several methods depending on your specific needs and the tools you’re using. Here are some common approaches:

1. Using Markdown TOC Plugin

If you’re using a Markdown editor that supports plugins, the Markdown TOC plugin can be highly effective. This plugin automatically generates a TOC based on the headers in your document.


<!-- TOC -->

- [Introduction](#introduction)

- [Chapter 1](#chapter-1)

- [Section 1.1](#section-11)

- [Section 1.2](#section-12)

- [Chapter 2](#chapter-2)

- [Section 2.1](#section-21)

- [Section 2.2](#section-22)

<!-- /TOC -->

2. MultiMarkdown

MultiMarkdown offers more features compared to standard Markdown, including a built-in TOC macro. Here’s how you can use it:


<!-- Start TOC -->

[TOC]

<!-- End TOC -->

3. Using reStructuredText Directive

For those who prefer reStructuredText over Markdown, the built-in contents directive is straightforward:


.. contents:: Table of Contents

:depth: 2

:local:

4. Ruby Script for GFM TOC

If you need to generate a TOC for GitHub Flavored Markdown (GFM), you can use a Ruby script. Here’s a simple example script:


#!/usr/bin/env ruby

headers = []

ARGF.each do |line|

headers << line if line =~ /^#+\s/

end

headers.each do |header|

level = header.scan(/^#+/).first.size

text = header.gsub(/^#+\s/, '').strip

anchor = text.downcase.gsub(/[^a-z\s]/, '').gsub(/\s+/, '-')

puts "#{'  ' * (level - 1)}- [#{text}](##{anchor})"

end

5. Using Visual Editor Plugins

Many Markdown editors come with TOC generation plugins. For example, Visual Studio Code has extensions like Markdown All in One that can automatically generate and update a TOC.


<!-- TOC depthfrom:1 depthto:6 orderedlist:false -->

- [Title](#title)

- [Another Section](#another-section)

<!-- /TOC -->

6. Manual TOC Creation

For a manual approach, you can create a TOC by hand. This method gives you full control over the structure and appearance:


## Table of Contents

- [Introduction](#introduction)

- [Getting Started](#getting-started)

## Example Section 2​

Creating a table of contents (TOC) in Markdown involves a few straightforward steps. Below, I'll outline the process to help you craft a TOC efficiently.

1. **Implement the TOC as a Markdown list:**

Start by writing a list in Markdown format where each item represents a section or heading in your document. This step sets the structure for your TOC.

2. **Run the command "Create Table of Contents":**

Depending on the editor or tool you're using, execute the command that generates a TOC. For example, in some Markdown editors or extensions like "Markdown All in One" for Visual Studio Code, you can use the command `Markdown: Create Table of Contents`.

3. **Insert the new TOC:**

Once generated, insert the TOC at the beginning or the designated section of your document. This TOC will automatically link to the respective headings, providing easy navigation.

Here's a more detailed breakdown using a table format:

<table border="1">
<tbody>
<tr>
<td>Step</td>
<td>Description</td>
<td>Example</td>
</tr>
<tr>
<td>1. Implement the TOC as a Markdown list</td>
<td>Create a list structure that mirrors your document's headings.</td>
<td>
<pre>
```markdown
- [Introduction](#introduction)
- [Getting Started](#getting-started)
- [Features](#features)
- [Conclusion](#conclusion)
2. Run the command "Create Table of Contents" Use your Markdown editor's feature to generate a TOC. This step varies by editor.
- For Visual Studio Code:
1. Install "Markdown All in One" extension.
2. Press `Ctrl+Shift+P`.
3. Type "Markdown: Create Table of Contents".
3. Insert the new TOC Place the generated TOC at the desired location in your document.
```markdown

- [Introduction](#introduction)
- [Getting Started](#getting-started)
- [Features](#features)
- [Conclusion](#conclusion)

```

Additional Tips:

  • Plugins and Tools: Many editors like Visual Studio Code, Atom, and Sublime Text offer extensions or plugins to automate TOC creation. Explore plugins like “Markdown TOC” for added convenience.

  • Manual Updates: If you prefer a manual approach, ensure you update the TOC each time you modify your headings. This keeps your TOC accurate and useful.

Example Section 3​

Using Markdown for a table of contents (TOC) significantly enhances the readability and organisation of an article by providing a clear and structured overview. Here’s how it accomplishes this:

Improved Readability

  1. Clear Navigation:

A TOC allows readers to see the structure of the document at a glance. This makes it easier for them to locate specific sections without having to scroll through the entire article.

  1. User-Friendly Formatting:

Markdown’s simplicity ensures that the TOC is easy to read and understand. The headings and subheadings are neatly organised, providing a clear hierarchy of information.

  1. Consistent Formatting:

Using Markdown ensures that the TOC formatting remains consistent throughout the document, making it visually appealing and easy to follow.

Enhanced Organization

  1. Logical Structure:

A well-structured TOC helps in outlining the main points and subtopics of the article. This logical flow improves comprehension and retention of information.

  1. Quick Access:

Readers can quickly jump to the sections they are interested in by clicking on the links in the TOC. This is especially useful for long articles or technical documents.

  1. Efficient Updates:

While Markdown requires manual updates to the TOC, this encourages the author to regularly review and refine the document’s structure. This ongoing process ensures that the TOC remains accurate and reflective of the content.

Markdown Syntax for TOC

To create a TOC in Markdown, you manually list the sections with links. Here’s an example:


## Table of Contents

1. [Introduction](#introduction)

2. [Benefits of Using Markdown](#benefits-of-using-markdown)

- [Readability](#readability)

- [Organization](#organization)

3. [How to Create a TOC](#how-to-create-a-toc)

4. [Best Practices](#best-practices)

Conclusion

Creating a Table of Contents (ToC) in Markdown is an invaluable skill that enhances the readability and navigation of your documents. As we’ve explored, incorporating a well-organized ToC can significantly boost reader retention by providing a clear roadmap through your content. Markdown, though simple, offers multiple methods to achieve this, ensuring your documents are both professional and user-friendly.

We’ve delved into various techniques for generating a ToC in Markdown. Utilizing built-in directives in Markdown processors like GitHub Flavored Markdown offers a straightforward approach for quick and simple documents. For more dynamic needs, plugins and extensions in editors such as Visual Studio Code automate the creation and updating of a ToC, saving time and ensuring consistency. Tools like Pandoc provide robust solutions for more complex documents, allowing seamless integration of ToCs with extensive customization options.

Custom solutions also hold their charm, enabling you to tailor the appearance and functionality of your ToC using personalized formatting tips and CSS styling. These methods not only make your document visually appealing but also ensure it is functionally effective.

The power of a well-crafted ToC cannot be overstated. It transforms a potentially overwhelming document into an easily navigable and engaging experience for your readers. By mastering these techniques, you can elevate the structure and professionalism of your Markdown files, guiding your audience effortlessly through your content.


Jacqueline M. Leonard

Hi, my name is Jacqueline M. Leonard. I am the main developer and editor of markdowntohtmlgenius.com. Markdown to HTML Genius offers an amazing and easy-to-use tool for converting Markdown to HTML.