From 21be9d20c66f7da8f3a09794a811a0c69f836f62 Mon Sep 17 00:00:00 2001 From: grubert Date: Thu, 21 Aug 2008 10:27:17 +0000 Subject: [PATCH] Apply [ 2051599 ] multi-page tables in latex writer (from pabigot). git-svn-id: https://docutils.svn.sourceforge.net/svnroot/docutils/trunk/docutils@5628 929543f6-e4f2-0310-98a6-ba3bd3dd1d04 --- HISTORY.txt | 1 + docutils/writers/latex2e/__init__.py | 65 ++++++++++++++--------- test/functional/expected/standalone_rst_latex.tex | 33 ++++++++++++ 3 files changed, 74 insertions(+), 25 deletions(-) diff --git a/HISTORY.txt b/HISTORY.txt index 88de0141d..592710169 100644 --- a/HISTORY.txt +++ b/HISTORY.txt @@ -29,6 +29,7 @@ Changes Since 0.5 * docutils/writers/latex2e/__init__.py: + - Apply [ 2051599 ] multi-page tables in latex writer (from pabigot). - Fix: for spanish ``~n`` is ``n`` with tilde above, change to ``~{}n``. - Change: has_key for dictionaries (not Nodes) to in-operator. - Merge adjacent citations into one latex cite command. diff --git a/docutils/writers/latex2e/__init__.py b/docutils/writers/latex2e/__init__.py index 263d41ae9..08be43ce1 100644 --- a/docutils/writers/latex2e/__init__.py +++ b/docutils/writers/latex2e/__init__.py @@ -394,7 +394,8 @@ class Table: * booktabs (requires booktabs latex package): only horizontal lines * nolines, borderless : no lines """ - def __init__(self,latex_type,table_style): + def __init__(self,translator,latex_type,table_style): + self._translator = translator self._latex_type = latex_type self._table_style = table_style self._open = 0 @@ -403,6 +404,7 @@ class Table: self._col_width = [] self._rowspan = [] self.stubs = [] + self._in_thead = 0 def open(self): self._open = 1 @@ -502,8 +504,20 @@ class Table: """ return "%.2f\\locallinewidth" % self._col_width[self._cell_in_row-1] + def get_caption(self): + if not self.caption: + return "" + if 1 == self._translator.thead_depth(): + return '\\caption{%s}\\\\\n' % self.caption + return '\\caption[]{%s (... continued)}\\\\\n' % self.caption + + def need_recurse(self): + if self._latex_type == 'longtable': + return 1 == self._translator.thead_depth() + return 0 + def visit_thead(self): - self._in_thead = 1 + self._in_thead += 1 if self._table_style == 'standard': return ['\\hline\n'] elif self._table_style == 'booktabs': @@ -516,9 +530,14 @@ class Table: if self._table_style == 'booktabs': a.append('\\midrule\n') if self._latex_type == 'longtable': - a.append('\\endhead\n') + if 1 == self._translator.thead_depth(): + a.append('\\endfirsthead\n') + else: + a.append('\\endhead\n') + a.append('\\multicolumn{%d}{c}{\\hfill ... continued on next page} \\\\\n' % len(self._col_specs)) + a.append('\\endfoot\n\\endlastfoot\n') # for longtable one could add firsthead, foot and lastfoot - self._in_thead = 0 + self._in_thead -= 1 return a def visit_row(self): self._cell_in_row = 0 @@ -649,7 +668,7 @@ class LaTeXTranslator(nodes.NodeVisitor): settings.use_part_section) # object for a table while proccessing. self.table_stack = [] - self.active_table = Table('longtable',settings.table_style) + self.active_table = Table(self,'longtable',settings.table_style) # HACK. Should have more sophisticated typearea handling. if settings.documentclass.find('scr') == -1: @@ -2001,7 +2020,7 @@ class LaTeXTranslator(nodes.NodeVisitor): if self.active_table.is_open(): self.table_stack.append(self.active_table) # nesting longtable does not work (e.g. 2007-04-18) - self.active_table = Table('tabular',self.settings.table_style) + self.active_table = Table(self,'tabular',self.settings.table_style) self.active_table.open() for cl in node['classes']: self.active_table.set_table_style(cl) @@ -2036,7 +2055,7 @@ class LaTeXTranslator(nodes.NodeVisitor): # for tables without heads. if not self.active_table.get('preamble written'): self.visit_thead(None) - # self.depart_thead(None) + self.depart_thead(None) def depart_tbody(self, node): pass @@ -2057,28 +2076,24 @@ class LaTeXTranslator(nodes.NodeVisitor): def depart_tgroup(self, node): pass + _thead_depth = 0 + def thead_depth (self): + return self._thead_depth + def visit_thead(self, node): - self.body.append('{%s}\n' % self.active_table.get_colspecs()) - if self.active_table.caption: - self.body.append('\\caption{%s}\\\\\n' % self.active_table.caption) - self.active_table.set('preamble written',1) - # TODO longtable supports firsthead and lastfoot too. + self._thead_depth += 1 + if 1 == self.thead_depth(): + self.body.append('{%s}\n' % self.active_table.get_colspecs()) + self.active_table.set('preamble written',1) + self.body.append(self.active_table.get_caption()) self.body.extend(self.active_table.visit_thead()) def depart_thead(self, node): - # the table header written should be on every page - # => \endhead - self.body.extend(self.active_table.depart_thead()) - # and the firsthead => \endfirsthead - # BUG i want a "continued from previous page" on every not - # firsthead, but then we need the header twice. - # - # there is a \endfoot and \endlastfoot too. - # but we need the number of columns to - # self.body.append('\\multicolumn{%d}{c}{"..."}\n' % number_of_columns) - # self.body.append('\\hline\n\\endfoot\n') - # self.body.append('\\hline\n') - # self.body.append('\\endlastfoot\n') + if node is not None: + self.body.extend(self.active_table.depart_thead()) + if self.active_table.need_recurse(): + node.walkabout(self) + self._thead_depth -= 1 def visit_tip(self, node): self.visit_admonition(node, 'tip') diff --git a/test/functional/expected/standalone_rst_latex.tex b/test/functional/expected/standalone_rst_latex.tex index a4c9c9f6a..bf960f87e 100644 --- a/test/functional/expected/standalone_rst_latex.tex +++ b/test/functional/expected/standalone_rst_latex.tex @@ -1328,7 +1328,26 @@ B A or B } \\ \hline +\endfirsthead +\hline +\multicolumn{2}{|l|}{\textbf{ +Inputs +}} & \textbf{ +Output +} \\ +\hline +\textbf{ +A +} & \textbf{ +B +} & \textbf{ +A or B +} \\ +\hline \endhead +\multicolumn{3}{c}{\hfill ... continued on next page} \\ +\endfoot +\endlastfoot False & @@ -1385,7 +1404,21 @@ Header 2 Header 3 } \\ \hline +\endfirsthead +\hline +\textbf{ +Header row, column 1 +(header rows optional) +} & \textbf{ +Header 2 +} & \textbf{ +Header 3 +} \\ +\hline \endhead +\multicolumn{3}{c}{\hfill ... continued on next page} \\ +\endfoot +\endlastfoot body row 1, column 1 & -- 2.11.4.GIT