1

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?

  • I'm using the following, in the mean time: perl -p0i -e 's|(\.\. _tab.*?$)(.*?)^Table:(.*?)$|my $label=$1; my $body=$2; my $caption=$3; $body=~s/^/\t/mg; $label."\n\n.. table:: ".$caption."\n\n".$body;|msge' mwe.rst – cmhughes Feb 18 '18 at 8:32
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"))
...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.