@@ -196,6 +196,7 @@ def render_verbatim(self, value, render_context, *,
196196 is_block_level = False , annotations = None , target_id = None ):
197197 if 'verbatimcode' in annotations :
198198 return r'\begin{verbatim}' + '\n ' + value + r'\end{verbatim}'
199+ # TODO: Maybe provide an interface to use \EscVerb here?? (cf fvextra latex package)
199200 # what to do with annotations / target_id ??
200201 if self .latex_wrap_verbatim_macro :
201202 return "\\ " + self .latex_wrap_verbatim_macro + "{" + self .latexescape (value ) + "}"
@@ -615,6 +616,9 @@ def collect_graphics_resource(self, graphics_resource, render_context):
615616 return graphics_resource .src_url , whoptc
616617
617618
619+ cells_max_width_latexdim = r'0.96\linewidth'
620+ cells_render_method = 'fit_width_test'
621+
618622 def render_cells (self , cells_model , render_context , target_id = None ,
619623 render_cell_nodelist_contents_fn = None ):
620624
@@ -717,44 +721,94 @@ def render_cells(self, cells_model, render_context, target_id=None,
717721
718722 stab_contents += '&' .join (stab_rowitems ) + '\\ \\ ' + '\n '
719723
720- s = (
721- r'\flmCellsBeginCenter' + '\n '
724+ def _typeset_table_contents (raw_args = None , colspec_one = None ):
725+ if raw_args is not None :
726+ tblrargs = raw_args
727+ else :
728+ tblrargs = r'colspec={' + (colspec_one * tabwidth ) + r'}'
729+ return (
730+ r'\begin{tblr}{' + tblrargs + r',' + '\n '
731+ + r' hspan=minimal'
732+ + cell_spans_styles
733+ + "" .join ([ ",\n hline{" + rownrng + "}={" + colnrng + "}{" + lsty + "}"
734+ for (rownrng , colnrng , lsty ) in cell_hlines ])
735+ + "" .join ([ ",\n vline{" + colnrng + "}={" + rownrng + "}{" + lsty + "}"
736+ for (rownrng , colnrng , lsty ) in cell_vlines ])
737+ + r'}%' + '\n '
738+ + r'\toprule'
739+ + '\n '
740+ + stab_contents
741+ + r'\bottomrule' + '\n '
742+ + r'\end{tblr}%' + '\n '
743+ )
744+
745+ if self .cells_render_method == 'simple' :
746+ s = (
747+ r'\flmCellsBeginCenter' + '\n '
748+ + _typeset_table_contents (colspec_one = 'c' )
749+ + r'\flmCellsEndCenter '
750+ )
751+ return s
752+
753+ if self .cells_render_method == 'fit_width_test' :
754+ # Hack for automatic width detection -- typeset table once with 'c'
755+ # column types; if the width exceeds a maximum set width
756+ # (0.96\linewidth), then re-typeset the table with 'X[-1]' column
757+ # types. Store the typesetting code in a macro to be more compact.
758+ s = (
759+ r'\flmCellsBeginCenter' + '\n '
760+ + r'\long\def\flmTempTypesetThisTable#1{%' + '\n '
761+ + _typeset_table_contents (raw_args = '#1' )
762+ + r'}%' + '\n '
763+ )
764+ # now, the code to automatically detect the correct width
765+ s += (
766+ r'\def\flmTmpMaxW{\dimexpr ' + self .cells_max_width_latexdim
767+ + r'\relax}%' + '\n '
768+ + r'\setbox0=\hbox{\flmTempTypesetThisTable{colspec={'
769+ + ('c' * tabwidth ) + r'}}}%' + '\n '
770+ + r'\ifdim\wd0<\flmTmpMaxW\relax' + '\n '
771+ + r' \leavevmode\box0 ' + '\n '
772+ + r'\else' + '\n '
773+ + r' \flmTempTypesetThisTable{width=\flmTmpMaxW,colspec={'
774+ + ('X[-1]' * tabwidth ) + r'}}' + '\n '
775+ + r'\fi' + '\n '
776+ )
777+ s += r'\flmCellsEndCenter '
778+ return s
779+
780+ if self .cells_render_method == 'fit_width_test_dupl' :
722781 # Hack for automatic width detection -- typeset table once with 'c'
723782 # column types; if the width exceeds a maximum set width
724783 # (0.96\linewidth), then re-typeset the table with 'X[-1]' column
725784 # types.
726- r'\long\def\flmTempTypesetThisTable#1{%' + '\n '
727- r'\begin{tblr}{#1,' + '\n ' + r' hspan=minimal'
728- + cell_spans_styles
729- + "" .join ([ ",\n hline{" + rownrng + "}={" + colnrng + "}{" + lsty + "}"
730- for (rownrng , colnrng , lsty ) in cell_hlines ])
731- + "" .join ([ ",\n vline{" + colnrng + "}={" + rownrng + "}{" + lsty + "}"
732- for (rownrng , colnrng , lsty ) in cell_vlines ])
733- + r'}' + '\n '
734- + r'\toprule'
735- + '\n '
736- )
737- s += stab_contents
738- s += r'\bottomrule' + '\n '
739- s += r'\end{tblr}%' + '\n '
740- s += r'}%' + '\n '
741- # now, the code to automatically detect the correct width
742- s += (
743- r'\def\flmTmpMaxW{\dimexpr ' + self .max_table_width_latexdim + r'\relax}%' + '\n '
744- + r'\setbox0=\hbox{\flmTempTypesetThisTable{colspec={'
745- + ('c' * tabwidth ) + r'}}}%' + '\n '
746- + r'\ifdim\wd0<\flmTmpMaxW\relax' + '\n '
747- + r' \leavevmode\box0 ' + '\n '
748- + r'\else' + '\n '
749- + r' \flmTempTypesetThisTable{width=\flmTmpMaxW,colspec={'
750- + ('X[-1]' * tabwidth ) + r'}}' + '\n '
751- + r'\fi' + '\n '
752- )
753- s += r'\flmCellsEndCenter '
785+ #
786+ # But paste the code to typeset the table *TWICE* so that we do not
787+ # have to use this ugly \def hack. (Originally I thought that this
788+ # would enable us to use fragile commands like \verba{...} (like
789+ # \verb+...+) inside the table contents. But it doesn't work,
790+ # probably because of tblr itself.)
791+ s = (
792+ r'\flmCellsBeginCenter' + '\n '
793+ + r'\def\flmTmpMaxW{\dimexpr ' + self .cells_max_width_latexdim
794+ + r'\relax}%' + '\n '
795+ + r'\setbox0=\hbox\bgroup'
796+ + _typeset_table_contents (colspec_one = 'c' )
797+ + r'\egroup' + '\n '
798+ + r'\ifdim\wd0<\flmTmpMaxW\relax' + '\n '
799+ + r' \leavevmode\box0 ' + '\n '
800+ + r'\else' + '\n '
801+ # Failed to typeset the table in the available width, try
802+ # variable sizing instead.
803+ + _typeset_table_contents (colspec_one = 'X[-1]' )
804+ + r'\fi' + '\n '
805+ + r'\flmCellsEndCenter '
806+ )
807+ return s
808+
809+ raise ValueError ("Invalid cells_render_method: " + repr (self .cells_render_method ))
754810
755- return s
756811
757- max_table_width_latexdim = r'0.96\linewidth'
758812
759813# ------------------
760814
0 commit comments