pandoc: customize tabular conversion in rst

pandoc: customize tabular conversion in rst

Consider the following mwe

mwe.tex

\documentclass{article}

\begin{document}

\begin{table}
    \centering
    \caption{my caption}
    \label{mylabel}
    \begin{tabular}{lc}
        1 & 2\\
        3 & 4
    \end{tabular}
\end{table}
\end{document}

When converting the file to rst using the following command

pandoc -o mwe.rst mwe.tex

then I receive the following

mwe.rst

+-----+-----+
| 1   | 2   |
+-----+-----+
| 3   | 4   |
+-----+-----+

Table: my caption

However, I would like the output to be

.. _mylabel:

.. table:: my caption

   +-----+-----+
   | 1   | 2   |
   +-----+-----+
   | 3   | 4   |
   +-----+-----+

which does three things: it gives the table a number,it gives the table a caption, and it allows the table to be referenced.

How can I tweak pandoc to output the tabular in my desired format?

답변1

Firstly, you are not providing the output format to pandoc, and so it is outputting markdown rather than RST:

$ pandoc -o mwe.rst mwe.tex -f latex -t rst

should give:

.. table:: my caption

   +-----+-----+
   | 1   | 2   |
   +-----+-----+
   | 3   | 4   |
   +-----+-----+

Secondly, unfotunately pandoc does not inherently have a concept of table labels. So when it reads in the table, it ignores the label.

The best route would be to create a pandoc filter. Using panflute is a nice approach.

>> import panflute as pf
>> content = pf.convert_text(tex, input_format="latex")
>> content
[Table(TableRow(TableCell(Plain(Str(1))) TableCell(Plain(Str(2)))) TableRow(TableCell(Plain(Str(3))) TableCell(Plain(Str(4)))); alignment=['AlignLeft', 'AlignCenter'], width=[0, 0], rows=2, cols=2)]
>> content.insert(0, pf.RawBlock(".. _mylabel:", format="rst"))
...

관련 정보