How to right-align columns content in reStructuredText simple tables?

后端 未结 3 666
感动是毒
感动是毒 2021-02-05 03:03

I\'m editing the documentation for a project of mine using Sphinx, which in turn uses reStructuredText as markup language.

I have a simple table (as opposed to

3条回答
  •  孤城傲影
    2021-02-05 03:51

    My approach is a bit of sed on the TeX file generated by Docutils. The idea is to replace the table declaration with something that fits your needs.

    Docutils produce something like that :

    \begin{longtable*}[c]{p{0.086\DUtablewidth}p{0.290\DUtablewidth}}
    

    Imagine you want to right-align the second column.You may want to replace this with :

    \begin{longtable*}[c]{lr}
    

    But you lose the ability to control the width of the cells. What we need here is to declare 2 \newcolumntype, one for the right-align (x) and one for the left-align (y):

    \newcolumntype{x}[1]{% 
    >{\raggedleft\hspace{0pt}}p{#1}}% 
    \newcolumntype{y}[1]{% 
    >{\raggedright\hspace{0pt}}p{#1}}% 
    

    And use them in the table declaration:

    \begin{longtable*}[c]{y{7.5cm}x{2cm}}
    

    The \\ newline must also be replaced with a \tabularnewline.

    I put everything in a script file because I am on OSX and the version of sed shipped does not support newline substitution with \n (that sucks when you are in a Makefile).

    The bottom-line

    On OSX/BSD:

    sed -E -f fix_table.sed < source.tex > destination.tex
    

    with fix_table.sed:

    s/\\begin{longtable\*}.*/\\newcolumntype{x}[1]{% \
    >{\\raggedleft\\hspace{0pt}}p{#1}}% \
    \\newcolumntype{y}[1]{% \
    >{\\raggedright\\hspace{0pt}}p{#1}}% \
    \\begin{longtable*}[c]{y{7.5cm}x{2cm}}/
    s/\\\\/\\tabularnewline/
    

    This is a bit harsh but there is no workaround that really works at the RestructuredText level.

    http://en.wikibooks.org/wiki/LaTeX/Tables

    http://texblog.org/2008/05/07/fwd-equal-cell-width-right-and-centre-aligned-content/

提交回复
热议问题