Search code examples
latexmarkdownpandoccross-reference

Markdown and references inside the listing captions


Is it possible to process the text markdown for the listing captions in pandoc?

Pandoc works well for figure captions and translates the emphasis and inline code, but apparently fails to do so in listing captions. For example, in the code below, I have some formatting and reference in the captions for the second listing and the figure. However, only the formatting in the figure is correctly transformed into latex. The listing caption is left as it is, which breaks the tex processer later in the pipeline.

Is it possible to process the listing captions just like the figure captions?


    ```{#lst:first .C caption="Hi"}
    int hi() {
        return ((int)'h'<<8) | 'i';
    }
    ```

    ```{#lst:second .C caption="Code *using* the function `hi` from [@lst:first]"}
    x = hi();
    ```

    ![Picture *with* `inline code` and reference [@lst:first]](picture.png)

pandoc example.so.md -o example.so.tex --listings --filter=pandoc-crossref

Produces:

\begin{codelisting}

\caption{Hi}

\begin{lstlisting}[language=C, caption=Hi, label=lst:first]
int hi() {
    return ((int)'h'<<8) | 'i';
}
\end{lstlisting}

\end{codelisting}

\begin{codelisting}

\caption{Code *using* the function `hi` from {[}@lst:first{]}}

\begin{lstlisting}[language=C, caption={Code *using* the function `hi` from [@lst:first]}, label=lst:second]
x = hi();
\end{lstlisting}

\end{codelisting}

\begin{figure}
\centering
\includegraphics{picture.png}
\caption{Picture \emph{with} \passthrough{\lstinline!inline code!} and
reference lst.~\ref{lst:first}}
\end{figure}

I use pandoc 2.7.3 and pandoc-crossref v0.3.4.1

P.S. as https://github.com/jgm/pandoc/issues/673 suggests, there may still be no native support for this. Is there a workaround?


Solution

  • K4zuki's response on the pandoc google group worked for me.(https://groups.google.com/forum/#!msg/pandoc-discuss/DItTuL5S1EM/L1Ou25gTCAAJ)

    Use the option for pandoc-crossref explained here: http://lierdakil.github.io/pandoc-crossref/#table-style-captions add codeBlockCaptions: true in your metadata block or run pandoc with -M codeBlockCaptions=true

    This worked:

    Listing: Code *using* the function `hi` from [@lst:first]
    
    ```{#lst:second .C}
    x = hi();
    ```