Skip to content

Commit

Permalink
Merge pull request #6017 from tk0miya/4550_align_figures_to_center
Browse files Browse the repository at this point in the history
Close #4550: All tables and figures without ``align`` option are displayed to center
  • Loading branch information
tk0miya authored Feb 10, 2019
2 parents 7aaae77 + 2502794 commit 3a237f7
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 77 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ Incompatible changes
* LaTeX: graphics inclusion of oversized images rescales to not exceed
the text width and height, even if width and/or height option were used.
(refs: #5956)
* #4550: All tables and figures without ``align`` option are displayed to center

Deprecated
----------
Expand Down
4 changes: 2 additions & 2 deletions sphinx/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from sphinx.deprecation import RemovedInSphinx30Warning
from sphinx.transforms import (
ApplySourceWorkaround, ExtraTranslatableNodes, CitationReferences,
DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks, SortIds,
DefaultSubstitutions, MoveModuleTargets, HandleCodeBlocks, SortIds, FigureAligner,
AutoNumbering, AutoIndexUpgrader, FilterSystemMessages,
UnreferencedFootnotesDetector, SphinxSmartQuotes, DoctreeReadEvent, ManpageLink
)
Expand Down Expand Up @@ -96,7 +96,7 @@ class SphinxStandaloneReader(SphinxBaseReader):
"""
transforms = [ApplySourceWorkaround, ExtraTranslatableNodes, PreserveTranslatableMessages,
Locale, CitationReferences, DefaultSubstitutions, MoveModuleTargets,
HandleCodeBlocks, AutoNumbering, AutoIndexUpgrader, SortIds,
HandleCodeBlocks, AutoNumbering, AutoIndexUpgrader, SortIds, FigureAligner,
RemoveTranslatableInline, FilterSystemMessages, RefOnlyBulletListTransform,
UnreferencedFootnotesDetector, SphinxSmartQuotes, ManpageLink,
SphinxDomains, SubstitutionDefinitionsRemover, DoctreeReadEvent,
Expand Down
15 changes: 14 additions & 1 deletion sphinx/transforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from sphinx.util import logging
from sphinx.util.docutils import new_document
from sphinx.util.i18n import format_date
from sphinx.util.nodes import apply_source_workaround, is_smartquotable
from sphinx.util.nodes import NodeMatcher, apply_source_workaround, is_smartquotable

if False:
# For type annotation
Expand Down Expand Up @@ -309,6 +309,19 @@ def apply(self, **kwargs):
location=node)


class FigureAligner(SphinxTransform):
"""
Align figures to center by default.
"""
default_priority = 700

def apply(self, **kwargs):
# type: (Any) -> None
matcher = NodeMatcher(nodes.table, nodes.figure)
for node in self.document.traverse(matcher): # type: nodes.Element
node.setdefault('align', 'center')


class FilterSystemMessages(SphinxTransform):
"""Filter system messages from a doctree."""
default_priority = 999
Expand Down
2 changes: 1 addition & 1 deletion tests/roots/test-latex-table/expects/longtable.tex
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\label{\detokenize{longtable:longtable}}

\begin{savenotes}\sphinxatlongtablestart\begin{longtable}{|l|l|}
\begin{savenotes}\sphinxatlongtablestart\begin{longtable}[c]{|l|l|}
\hline
\sphinxstyletheadfamily
header1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\label{\detokenize{longtable:longtable-having-caption}}

\begin{savenotes}\sphinxatlongtablestart\begin{longtable}{|l|l|}
\begin{savenotes}\sphinxatlongtablestart\begin{longtable}[c]{|l|l|}
\sphinxthelongtablecaptionisattop
\caption{caption for longtable\strut}\label{\detokenize{longtable:id1}}\\*[\sphinxlongtablecapskipadjust]
\hline
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\label{\detokenize{longtable:longtable-having-problematic-cell}}

\begin{savenotes}\sphinxatlongtablestart\begin{longtable}{|*{2}{\X{1}{2}|}}
\begin{savenotes}\sphinxatlongtablestart\begin{longtable}[c]{|*{2}{\X{1}{2}|}}
\hline
\sphinxstyletheadfamily
header1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\label{\detokenize{longtable:longtable-having-both-stub-columns-and-problematic-cell}}

\begin{savenotes}\sphinxatlongtablestart\begin{longtable}{|*{3}{\X{1}{3}|}}
\begin{savenotes}\sphinxatlongtablestart\begin{longtable}[c]{|*{3}{\X{1}{3}|}}
\hline
\sphinxstyletheadfamily
header1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\label{\detokenize{longtable:longtable-having-verbatim}}

\begin{savenotes}\sphinxatlongtablestart\begin{longtable}{|*{2}{\X{1}{2}|}}
\begin{savenotes}\sphinxatlongtablestart\begin{longtable}[c]{|*{2}{\X{1}{2}|}}
\hline
\sphinxstyletheadfamily
header1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\label{\detokenize{longtable:longtable-having-widths-option}}

\begin{savenotes}\sphinxatlongtablestart\begin{longtable}{|\X{30}{100}|\X{70}{100}|}
\begin{savenotes}\sphinxatlongtablestart\begin{longtable}[c]{|\X{30}{100}|\X{70}{100}|}
\hline\noalign{\phantomsection\label{\detokenize{longtable:namedlongtable}}\label{\detokenize{longtable:mylongtable}}}%
\sphinxstyletheadfamily
header1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\label{\detokenize{longtable:longtable-having-both-widths-and-problematic-cell}}

\begin{savenotes}\sphinxatlongtablestart\begin{longtable}{|\X{30}{100}|\X{70}{100}|}
\begin{savenotes}\sphinxatlongtablestart\begin{longtable}[c]{|\X{30}{100}|\X{70}{100}|}
\hline
\sphinxstyletheadfamily
header1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
\label{\detokenize{longtable:longtable-with-tabularcolumn}}

\begin{savenotes}\sphinxatlongtablestart\begin{longtable}{|c|c|}
\begin{savenotes}\sphinxatlongtablestart\begin{longtable}[c]{|c|c|}
\hline
\sphinxstyletheadfamily
header1
Expand Down
Loading

0 comments on commit 3a237f7

Please sign in to comment.