Fenced code blocks: how to add caption and label?

1,382 views
Skip to first unread message

Giorgio Buffa

unread,
Sep 8, 2013, 8:39:08 AM9/8/13
to multim...@googlegroups.com
I’ve noticed that recently MMD has added support for fenced code blocks, that’s nice!

I’ve a couple of questions:

1. Can I add a caption to the listing?
2. Can I create a label for it, to cross-reference the listing within the text?

When exporting to Latex, I use the following scheme:

    A simple C example.

    ```c,caption=My first C program,label=src-hello-c
  #include <stdio.h>

  int main(int argc, char *argv[])
  {
      printf("Hello world!\n");
      return 0;
  }
  ```
    
where the code block is inserted in a `listings` environment (at least with `MultiMarkdown Composer`), and it has its own caption:

    \begin{lstlisting}[language=c,caption==My first C program,label=src-hello-c]
  #include <stdio.h>

  int main(int argc, char *argv[])
  {
      printf("Hello world!\n");
      return 0;
  }
  \end{lstlisting}
    
The problems are:

* I cannot figure out how to cross-reference the label within the text in MMD.
* `Listings` package is used only when exporting from `MMD Composer`. This is probably because I’ve installed MMD v4.2 on my system (and maybe `MMD Composer` uses a separate, newer MMD engine.

Any hints?

Thanks,
Giorgio

Fletcher T. Penney

unread,
Sep 9, 2013, 11:10:24 AM9/9/13
to Giorgio Buffa, multim...@googlegroups.com
On Sep 8, 2013, at 8:39 AM, Giorgio Buffa <buffag...@gmail.com> wrote:

I’ve noticed that recently MMD has added support for fenced code blocks, that’s nice!

I’ve a couple of questions:

1. Can I add a caption to the listing?

No.  HTML has no concept of a caption for code blocks, so it's not part of Markdown/MMD.

2. Can I create a label for it, to cross-reference the listing within the text?

Not in MMD.  You have to do it manually for your desired output format.

When exporting to Latex, I use the following scheme:

    A simple C example.

    ```c,caption=My first C program,label=src-hello-c

..

    where the code block is inserted in a `listings` environment (at least with `MultiMarkdown Composer`), and it has its own caption:

    \begin{lstlisting}[language=c,caption==My first C program,label=src-hello-c]

Clever use of the language specification.  ;)

The problems are:

* I cannot figure out how to cross-reference the label within the text in MMD.

It looks like you applied the label "src-hello-c", so that's what you would use just like any other manual internal anchor:

[source](#src-hello-c)

* `Listings` package is used only when exporting from `MMD Composer`. This is probably because I’ve installed MMD v4.2 on my system (and maybe `MMD Composer` uses a separate, newer MMD engine.

Just compile MMD from source any time if you want to ensure you have the latest modifications.

FTP

Reply all
Reply to author
Forward
0 new messages