开发者

Convert tables in rst to LaTeX

开发者 https://www.devze.com 2022-12-26 04:52 出处:网络
I have some .rst files and I convert them to .tex file using standard sphinx converter. In some .rst I have tables with special width like:

I have some .rst files and I convert them to .tex file using standard sphinx converter.

In some .rst I have tables with special width like:

.. list-table::  
   :widths: 50 50 

The resulting .tex always contains tables like:

\begin{tabulary}{\textwidth}{|L|L|}

So, the column width is lost.开发者_C百科

How can I preserve column width when converting rst to latex?


I used comma separator too,

.. list-table::  
   :widths: 50 , 50 
   :header-rows: 1 

* - SETTING 
  - DESCRIPTION
* - Enable 
  - Enables or disables internal tracing.
* - Verbose 
  - Enables or disables extended internal tracing. 

but it doesn't work.. maybe I used a bad converter? What converter do you recommend?


actually the command

.. tabularcolumns:: |p{4.5cm}|p{8.5cm}| 

is needed just before .. list-table::

https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-tabularcolumns


try:

:widths: 50, 50

with a comma separator.

The output also depends on how your table is written in rst.

I assumed that you were using the standard rst table syntax, not making tables from bulleted lists (as is possible). For more help, try http://docutils.sourceforge.net/docs/ref/rst/directives.html#tables

Also, if the 50, 50 is the column width, your latex code should look like this:

\begin{tabulary}{  1\textwidth}{ | p{0.5} | p{0.5} | }

and:

\begin{tabulary}{total width of table}{| column width| column width|}


The docutils rst2latex writer has some issues with tables: http://docutils.sourceforge.net/docs/dev/todo.html#tables , so maybe your problem is related to that? I think the Sphinx writer is based on rst2latex and might thus have the same issues.


I can confirm that this:

.. list-table::
:widths: 10 40 50

* - Module
  - Link
  - Description 

Works with rst2latex

\setlength{\DUtablewidth}{\linewidth}
\begin{longtable*}[c]{|p{0.104\DUtablewidth}|p{0.375\DUtablewidth}|p{0.465\DUtablewidth}|}
\hline

Module
 & 
Link
 & 
Description
 \\
\hline

But with sphinx, I get what the OP put. So not an rst2latex issue I would gather.

The "Auto" width stuff the docs talk about is also not very functional for me, links tend to bleed over.


Since I have a huge documentation, I tried to fix the latex generation. Also, I consider Latex notation in rst files a disadvantage, because it's inconsistent and requires editors to partly learn a touchy markup language.

I replaced LaTeXTranslator.depart_table with my own version. I copied the original depart_table and added this code (shortened):

def my_depart_table (self, node):
    totalColwidth = 0
    givenColwidth = []
    hasColwidth = False

    for tgroup in node:
        for tableColspec in tgroup:
            try:
                if tableColspec.has_key('colwidth'):
                    totalColwidth += tableColspec['colwidth']    
                    givenColwidth.append(tableColspec['colwidth'])
                    hasColwidth = True
             except: 
                    print "colspec missing. \n" 
     # original code

     if hasColwidth:
         colspec = ""
         for thisColwidth in givenColwidth:
             colspec += ('>{\RaggedRight}p{%.3f\\linewidth}' % (0.95 * thisColwidth / totalColwidth))                    
             #print "using widths: %.3f %s %s" % ((0.95 * thisColwidth / totalColwidth), thisColwidth, totalColwidth)
             self.body.append('{' + colspec + '}\n')

     # more original code

LaTeXTranslator.depart_table = my_depart_table

Im neither fluent in Python nor Sphinx, so use at own risk. I hope you get the idea or can even give advice.

If you use Python < 3.0 and want to remove the factor 0.95 completely, remember to either cast one of the integers to float or import division from __ future __.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号