How to align LaTeX PDF tables left again

3,671 views
Skip to first unread message

Erin Kelly

unread,
Jan 10, 2018, 5:17:59 PM1/10/18
to sphinx-users
Apparently in 1.6.1 the default table alignment changed from left to center. I want all my tables to align left. 

The change documentation says I can use Docutils :align: option but that only works if I'm using a .. table:: tag
I'm using this style of tables: 
+------------+------------+-----------+
| Header 1   | Header 2   | Header 3  |
+============+============+===========+
| body row 1 | column 2   | column 3  |
+------------+------------+-----------+

I tried throwing the table tag above this kind of table but it didn't understand that. 

Is there something I can put in the latex preamble to reset the default to align left? 

Peter Burdine

unread,
Jan 10, 2018, 7:30:53 PM1/10/18
to sphinx-users
You have to use the tabularcolumns directive, which can take a ton of latex commands for formatting of columns, such as:

.. tabularcolumns:: |>{\RaggedRight}p{\dimexpr 0.3\linewidth-2\tabcolsep}
                   
|>{\RaggedRight}p{\dimexpr 0.45\linewidth-2\tabcolsep}
                   
|>{\RaggedRight}p{\dimexpr 0.25\linewidth-2\tabcolsep}|


This will create columns that are 30%, 45%, 25%, all left aligned with vertical lines between cells.

--Peter

jfbu

unread,
Jan 11, 2018, 5:57:06 AM1/11/18
to sphinx...@googlegroups.com
Hi,

You can override the templates found at

https://github.com/sphinx-doc/sphinx/tree/stable/sphinx/templates/latex

by your own, according to instructions at bottom of

http://www.sphinx-doc.org/en/stable/latex.html

In that case just replace the

<%- if table.align == 'center' -%>
[c]
<%- elif table.align == 'left' -%>
[l]
<%- elif table.align == 'right' -%>
[r]
<%- endif -%>

block by a [l] for the longtable template
and

<% if table.align -%>
<%- if table.align == 'center' -%>
\centering
<%- elif table.align == 'left' -%>
\raggedright
<%- else -%>
\raggedleft
<%- endif %>
<%- else -%>
\centering
<%- endif %>

by \raggedright for tabular and tabulary

Jean-François


Komiya Takeshi

unread,
Jan 11, 2018, 11:52:26 AM1/11/18
to sphinx...@googlegroups.com
Hi,

Personally, I prefer to use an extension for this case.
I believe the following code help you. You can use this code with appending
them to your conf.py.

```
from docutils import nodes
from sphinx.transforms import SphinxTransform


class AdjustTableAlign(SphinxTransform):
default_priority = 500

def apply(self):
if self.app.builder.name != 'latex':
return

for node in self.document.traverse(nodes.table):
node.attributes.setdefault('align', 'left')


def setup(app):
app.add_post_transform(AdjustTableAlign)
```

I know the python code is a bit harder to understand and to maintain it.
But this does not touch the LaTeX macros directly, so this way is a
bit robuster than
writing LaTeX macros.

Thanks,
Takeshi KOMIYA

2018-01-11 4:59 GMT+09:00 'Erin Kelly' via sphinx-users
<sphinx...@googlegroups.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "sphinx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sphinx-users...@googlegroups.com.
> To post to this group, send email to sphinx...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sphinx-users.
> For more options, visit https://groups.google.com/d/optout.

Sebastian Huber

unread,
May 28, 2019, 9:30:26 AM5/28/19
to sphinx-users


Am Donnerstag, 11. Januar 2018 17:52:26 UTC+1 schrieb Komiya Takeshi:
Hi,

Personally, I prefer to use an extension for this case.
I believe the following code help you. You can use this code with appending
them to your conf.py.

```
from docutils import nodes
from sphinx.transforms import SphinxTransform


class AdjustTableAlign(SphinxTransform):
    default_priority = 500

    def apply(self):
        if self.app.builder.name != 'latex':
            return

        for node in self.document.traverse(nodes.table):
            node.attributes.setdefault('align', 'left')


def setup(app):
    app.add_post_transform(AdjustTableAlign)
```

I know the python code is a bit harder to understand and to maintain it.
But this does not touch the LaTeX macros directly, so this way is a
bit robuster than
writing LaTeX macros.

I tried this extension code to align all table cell content to the left by default. It seems to have no effect, the table cells are still center aligned. I added a "print(node)" before the "node.attributes.setdefault('align', 'left') " and see lots of output, so the extension seems to do something.

```

.. list-table:: Traceability from Item to Item in ECSS-E-ST-40C
    :widths: 16, 20, 18, 18, 8, 20
    :header-rows: 1
    :class: longtable

    * - Reference
      - Subject
      - Source
      - Destination
      - Y/N
      - Solution
    * - 5.7.3.5.a
      - Evaluation of acceptance testing
      - acceptance tests
      - requirements baseline
      - N
      - ?
```

Komiya Takeshi

unread,
May 28, 2019, 10:01:59 AM5/28/19
to sphinx...@googlegroups.com
Hi,

This code skips LaTeX builder via this if-block. Please remove this to
align tables on LaTaX builder.
```
if self.app.builder.name != 'latex':
return
```

Thanks,
Takeshi KOMIYA

2019年5月28日(火) 22:30 Sebastian Huber <seb...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups "sphinx-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sphinx-users...@googlegroups.com.
> To post to this group, send email to sphinx...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sphinx-users.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sphinx-users/80367e75-3ef2-48f0-81cc-69d65290a84f%40googlegroups.com.

Sebastian Huber

unread,
May 29, 2019, 2:10:37 AM5/29/19
to sphinx-users
I found a much better solution for my problem with the table cell alignment in this issue:


I added this to my custom Latex style file:

```
% Example ".. tabularcolumns:: |\Yl{0.1}|\Yr{0.9}|"
\newcolumntype{\Yl}[1]{>{\raggedright\arraybackslash}\Y{#1}}
\newcolumntype{\Yr}[1]{>{\raggedleft\arraybackslash}\Y{#1}}
\newcolumntype{\Yj}[1]{>{\centering\arraybackslash}\Y{#1}}
```

It works very nice. Maybe it should be added to the documentation:

https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#directive-tabularcolumns
Reply all
Reply to author
Forward
0 new messages