📄 __init__.py
字号:
return self._deepest_sectionclass Table: """ Manage a table while traversing. Maybe change to a mixin defining the visit/departs, but then class Table internal variables are in the Translator. """ def __init__(self,latex_type,table_style): self._latex_type = latex_type self._table_style = table_style self._open = 0 # miscellaneous attributes self._attrs = {} self._col_width = [] self._rowspan = [] def open(self): self._open = 1 self._col_specs = [] self.caption = None self._attrs = {} self._in_head = 0 # maybe context with search def close(self): self._open = 0 self._col_specs = None self.caption = None self._attrs = {} def is_open(self): return self._open def used_packages(self): if self._table_style == 'booktabs': return '\\usepackage{booktabs}\n' return '' def get_latex_type(self): return self._latex_type def set(self,attr,value): self._attrs[attr] = value def get(self,attr): if self._attrs.has_key(attr): return self._attrs[attr] return None def get_vertical_bar(self): if self._table_style == 'standard': return '|' return '' # horizontal lines are drawn below a row, because we. def get_opening(self): return '\\begin{%s}[c]' % self._latex_type def get_closing(self): line = "" if self._table_style == 'booktabs': line = '\\bottomrule\n' elif self._table_style == 'standard': lines = '\\hline\n' return '%s\\end{%s}' % (line,self._latex_type) def visit_colspec(self,node): self._col_specs.append(node) def get_colspecs(self): """ Return column specification for longtable. Assumes reST line length being 80 characters. Table width is hairy. === === ABC DEF === === usually gets to narrow, therefore we add 1 (fiddlefactor). """ width = 80 total_width = 0.0 # first see if we get too wide. for node in self._col_specs: colwidth = float(node['colwidth']+1) / width total_width += colwidth self._col_width = [] self._rowspan = [] # donot make it full linewidth factor = 0.93 if total_width > 1.0: factor /= total_width bar = self.get_vertical_bar() latex_table_spec = "" for node in self._col_specs: colwidth = factor * float(node['colwidth']+1) / width self._col_width.append(colwidth+0.005) self._rowspan.append(0) latex_table_spec += "%sp{%.2f\\locallinewidth}" % (bar,colwidth+0.005) return latex_table_spec+bar def get_column_width(self): """ return columnwidth for current cell (not multicell) """ return "%.2f\\locallinewidth" % self._col_width[self._cell_in_row-1] def visit_thead(self): self._in_thead = 1 if self._table_style == 'standard': return ['\\hline\n'] elif self._table_style == 'booktabs': return ['\\toprule\n'] return [] def depart_thead(self): a = [] #if self._table_style == 'standard': # a.append('\\hline\n') if self._table_style == 'booktabs': a.append('\\midrule\n') a.append('\\endhead\n') # for longtable one could add firsthead, foot and lastfoot self._in_thead = 0 return a def visit_row(self): self._cell_in_row = 0 def depart_row(self): res = [' \\\\\n'] self._cell_in_row = None # remove cell counter for i in range(len(self._rowspan)): if (self._rowspan[i]>0): self._rowspan[i] -= 1 if self._table_style == 'standard': rowspans = [] for i in range(len(self._rowspan)): if (self._rowspan[i]<=0): rowspans.append(i+1) if len(rowspans)==len(self._rowspan): res.append('\\hline\n') else: cline = '' rowspans.reverse() # TODO merge clines while 1: try: c_start = rowspans.pop() except: break cline += '\\cline{%d-%d}\n' % (c_start,c_start) res.append(cline) return res def set_rowspan(self,cell,value): try: self._rowspan[cell] = value except: pass def get_rowspan(self,cell): try: return self._rowspan[cell] except: return 0 def get_entry_number(self): return self._cell_in_row def visit_entry(self): self._cell_in_row += 1class LaTeXTranslator(nodes.NodeVisitor): # When options are given to the documentclass, latex will pass them # to other packages, as done with babel. # Dummy settings might be taken from document settings latex_head = '\\documentclass[%s]{%s}\n' encoding = '\\usepackage[%s]{inputenc}\n' linking = '\\usepackage[colorlinks=%s,linkcolor=%s,urlcolor=%s]{hyperref}\n' stylesheet = '\\input{%s}\n' # add a generated on day , machine by user using docutils version. generator = '%% generator Docutils: http://docutils.sourceforge.net/\n' # use latex tableofcontents or let docutils do it. use_latex_toc = 0 # TODO: use mixins for different implementations. # list environment for option-list. else tabularx use_optionlist_for_option_list = 1 # list environment for docinfo. else tabularx use_optionlist_for_docinfo = 0 # NOT YET IN USE # Use compound enumerations (1.A.1.) compound_enumerators = 0 # If using compound enumerations, include section information. section_prefix_for_enumerators = 0 # This is the character that separates the section ("." subsection ...) # prefix from the regular list enumerator. section_enumerator_separator = '-' # default link color hyperlink_color = "blue" def __init__(self, document): nodes.NodeVisitor.__init__(self, document) self.settings = settings = document.settings self.latex_encoding = self.to_latex_encoding(settings.output_encoding) self.use_latex_toc = settings.use_latex_toc self.use_latex_docinfo = settings.use_latex_docinfo self.use_latex_footnotes = settings.use_latex_footnotes self._use_latex_citations = settings.use_latex_citations self.hyperlink_color = settings.hyperlink_color self.compound_enumerators = settings.compound_enumerators self.font_encoding = settings.font_encoding self.section_prefix_for_enumerators = ( settings.section_prefix_for_enumerators) self.section_enumerator_separator = ( settings.section_enumerator_separator.replace('_', '\\_')) if self.hyperlink_color == '0': self.hyperlink_color = 'black' self.colorlinks = 'false' else: self.colorlinks = 'true' # language: labels, bibliographic_fields, and author_separators. # to allow writing labes for specific languages. self.language = languages.get_language(settings.language_code) self.babel = Babel(settings.language_code) self.author_separator = self.language.author_separators[0] self.d_options = self.settings.documentoptions if self.babel.get_language(): self.d_options += ',%s' % \ self.babel.get_language() self.d_class = DocumentClass(settings.documentclass) # object for a table while proccessing. self.active_table = Table('longtable',settings.table_style) # HACK. Should have more sophisticated typearea handling. if settings.documentclass.find('scr') == -1: self.typearea = '\\usepackage[DIV12]{typearea}\n' else: if self.d_options.find('DIV') == -1 and self.d_options.find('BCOR') == -1: self.typearea = '\\typearea{12}\n' else: self.typearea = '' if self.font_encoding == 'OT1': fontenc_header = '' elif self.font_encoding == '': fontenc_header = '\\usepackage{ae}\n\\usepackage{aeguill}\n' else: fontenc_header = '\\usepackage[%s]{fontenc}\n' % (self.font_encoding,) input_encoding = self.encoding % self.latex_encoding if self.settings.graphicx_option == '': self.graphicx_package = '\\usepackage{graphicx}\n' elif self.settings.graphicx_option.lower() == 'auto': self.graphicx_package = '\n'.join( ('%Check if we are compiling under latex or pdflatex', '\\ifx\\pdftexversion\\undefined', ' \\usepackage{graphicx}', '\\else', ' \\usepackage[pdftex]{graphicx}', '\\fi\n')) else: self.graphicx_package = ( '\\usepackage[%s]{graphicx}\n' % self.settings.graphicx_option) self.head_prefix = [ self.latex_head % (self.d_options,self.settings.documentclass), '\\usepackage{babel}\n', # language is in documents settings. fontenc_header, '\\usepackage{shortvrb}\n', # allows verb in footnotes. input_encoding, # * tabularx: for docinfo, automatic width of columns, always on one page. '\\usepackage{tabularx}\n', '\\usepackage{longtable}\n', self.active_table.used_packages(), # possible other packages. # * fancyhdr # * ltxtable is a combination of tabularx and longtable (pagebreaks). # but ?? # # extra space between text in tables and the line above them '\\setlength{\\extrarowheight}{2pt}\n', '\\usepackage{amsmath}\n', # what fore amsmath. self.graphicx_package, '\\usepackage{color}\n', '\\usepackage{multirow}\n', '\\usepackage{ifthen}\n', # before hyperref! self.linking % (self.colorlinks, self.hyperlink_color, self.hyperlink_color), self.typearea, self.generator, # latex lengths '\\newlength{\\admonitionwidth}\n', '\\setlength{\\admonitionwidth}{0.9\\textwidth}\n' # width for docinfo tablewidth '\\newlength{\\docinfowidth}\n', '\\setlength{\\docinfowidth}{0.9\\textwidth}\n' # linewidth of current environment, so tables are not wider # than the sidebar: using locallinewidth seems to defer evaluation # of linewidth, this is fixing it. '\\newlength{\\locallinewidth}\n', # will be set later. ] self.head_prefix.extend( latex_headings['optionlist_environment'] ) self.head_prefix.extend( latex_headings['lineblock_environment'] ) self.head_prefix.extend( latex_headings['footnote_floats'] ) self.head_prefix.extend( latex_headings['some_commands'] ) ## stylesheet is last: so it might be possible to overwrite defaults. stylesheet = utils.get_stylesheet_reference(settings) if stylesheet: settings.record_dependencies.add(stylesheet) self.head_prefix.append(self.stylesheet % (stylesheet)) if self.linking: # and maybe check for pdf self.pdfinfo = [ ] self.pdfauthor = None # pdftitle, pdfsubject, pdfauthor, pdfkeywords, pdfcreator, pdfproducer else: self.pdfinfo = None # NOTE: Latex wants a date and an author, rst puts this into # docinfo, so normally we donot want latex author/date handling. # latex article has its own handling of date and author, deactivate. # So we always emit \title{...} \author{...} \date{...}, even if the # "..." are empty strings. self.head = [ ] # separate title, so we can appen subtitle. self.title = '' # if use_latex_docinfo: collects lists of author/organization/contact/address lines self.author_stack = [] self.date = '' self.body_prefix = ['\\raggedbottom\n'] self.body = [] self.body_suffix = ['\n'] self.section_level = 0 self.context = [] self.topic_classes = [] # column specification for tables self.table_caption = None # Flags to encode # --------------- # verbatim: to tell encode not to encode. self.verbatim = 0 # insert_newline: to tell encode to replace blanks by "~". self.insert_none_breaking_blanks = 0 # insert_newline: to tell encode to add latex newline. self.insert_newline = 0 # mbox_newline: to tell encode to add mbox and newline. self.mbox_newline = 0 # enumeration is done by list environment. self._enum_cnt = 0
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -