Blame doc/pdf/sphinxmulticell.sty

Packit fd8b60
\NeedsTeXFormat{LaTeX2e}
Packit fd8b60
\ProvidesPackage{sphinxmulticell}%
Packit fd8b60
    [2017/02/23 v1.6 better span rows and columns of a table (Sphinx team)]%
Packit fd8b60
\DeclareOption*{\PackageWarning{sphinxmulticell}{Option `\CurrentOption' is unknown}}%
Packit fd8b60
\ProcessOptions\relax
Packit fd8b60
%
Packit fd8b60
% --- MULTICOLUMN ---
Packit fd8b60
% standard LaTeX's \multicolumn
Packit fd8b60
% 1. does not allow verbatim contents,
Packit fd8b60
% 2. interacts very poorly with tabulary.
Packit fd8b60
%
Packit fd8b60
% It is needed to write own macros for Sphinx: to allow code-blocks in merged
Packit fd8b60
% cells rendered by tabular/longtable, and to allow multi-column cells with
Packit fd8b60
% paragraphs to be taken into account sanely by tabulary algorithm for column
Packit fd8b60
% widths.
Packit fd8b60
%
Packit fd8b60
% This requires quite a bit of hacking. First, in Sphinx, the multi-column
Packit fd8b60
% contents will *always* be wrapped in a varwidth environment. The issue
Packit fd8b60
% becomes to pass it the correct target width. We must trick tabulary into
Packit fd8b60
% believing the multicolumn is simply separate columns, else tabulary does not
Packit fd8b60
% incorporate the contents in its algorithm. But then we must clear the
Packit fd8b60
% vertical rules...
Packit fd8b60
%
Packit fd8b60
% configuration of tabulary
Packit fd8b60
\setlength{\tymin}{3\fontcharwd\font`0 }% minimal width of "squeezed" columns
Packit fd8b60
\setlength{\tymax}{10000pt}% allow enough room for paragraphs to "compete"
Packit fd8b60
% we need access to tabulary's final computed width. \@tempdima is too volatile
Packit fd8b60
% to hope it has kept tabulary's value when \sphinxcolwidth needs it.
Packit fd8b60
\newdimen\sphinx@TY@tablewidth
Packit fd8b60
\def\tabulary{%
Packit fd8b60
  \def\TY@final{\sphinx@TY@tablewidth\@tempdima\tabular}%
Packit fd8b60
  \let\endTY@final\endtabular
Packit fd8b60
  \TY@tabular}%
Packit fd8b60
% next hack is needed only if user has set latex_use_latex_multicolumn to True:
Packit fd8b60
% it fixes tabulary's bug with \multicolumn defined "short" in first pass. (if
Packit fd8b60
% upstream tabulary adds a \long, our extra one causes no harm)
Packit fd8b60
\def\sphinx@tempa #1\def\multicolumn#2#3#4#5#6#7#8#9\sphinx@tempa
Packit fd8b60
   {\def\TY@tab{#1\long\def\multicolumn####1####2####3{\multispan####1\relax}#9}}%
Packit fd8b60
\expandafter\sphinx@tempa\TY@tab\sphinx@tempa
Packit fd8b60
%
Packit fd8b60
% TN. 1: as \omit is never executed, Sphinx multicolumn does not need to worry
Packit fd8b60
% like standard multicolumn about |l| vs l|. On the other hand it assumes
Packit fd8b60
% columns are separated by a | ... (if not it will add extraneous
Packit fd8b60
% \arrayrulewidth space for each column separation in its estimate of available
Packit fd8b60
% width).
Packit fd8b60
%
Packit fd8b60
% TN. 1b: as Sphinx multicolumn uses neither \omit nor \span, it can not
Packit fd8b60
% (easily) get rid of extra macros from >{...} or <{...} between columns. At
Packit fd8b60
% least, it has been made compatible with colortbl's \columncolor.
Packit fd8b60
%
Packit fd8b60
% TN. 2: tabulary's second pass is handled like tabular/longtable's single
Packit fd8b60
% pass, with the difference that we hacked \TY@final to set in
Packit fd8b60
% \sphinx@TY@tablewidth the final target width as computed by tabulary. This is
Packit fd8b60
% needed only to handle columns with a "horizontal" specifier: "p" type columns
Packit fd8b60
% (inclusive of tabulary's LJRC) holds the target column width in the
Packit fd8b60
% \linewidth dimension.
Packit fd8b60
%
Packit fd8b60
% TN. 3: use of \begin{sphinxmulticolumn}...\end{sphinxmulticolumn} mark-up
Packit fd8b60
% would need some hacking around the fact that groups can not span across table
Packit fd8b60
% cells (the code does inserts & tokens, see TN1b). It was decided to keep it
Packit fd8b60
% simple with \sphinxstartmulticolumn...\sphinxstopmulticolumn.
Packit fd8b60
%
Packit fd8b60
% MEMO about nesting: if sphinxmulticolumn is encountered in a nested tabular
Packit fd8b60
% inside a tabulary it will think to be at top level in the tabulary. But
Packit fd8b60
% Sphinx generates no nested tables, and if some LaTeX macro uses internally a
Packit fd8b60
% tabular this will not have a \sphinxstartmulticolumn within it!
Packit fd8b60
%
Packit fd8b60
\def\sphinxstartmulticolumn{%
Packit fd8b60
    \ifx\equation$% $ tabulary's first pass
Packit fd8b60
        \expandafter\sphinx@TYI@start@multicolumn
Packit fd8b60
    \else % either not tabulary or tabulary's second pass
Packit fd8b60
        \expandafter\sphinx@start@multicolumn
Packit fd8b60
    \fi
Packit fd8b60
}%
Packit fd8b60
\def\sphinxstopmulticolumn{%
Packit fd8b60
    \ifx\equation$% $ tabulary's first pass
Packit fd8b60
        \expandafter\sphinx@TYI@stop@multicolumn
Packit fd8b60
    \else % either not tabulary or tabulary's second pass
Packit fd8b60
        \ignorespaces
Packit fd8b60
    \fi
Packit fd8b60
}%
Packit fd8b60
\def\sphinx@TYI@start@multicolumn#1{%
Packit fd8b60
    % use \gdef always to avoid stack space build up
Packit fd8b60
    \gdef\sphinx@tempa{#1}\begingroup\setbox\z@\hbox\bgroup
Packit fd8b60
}%
Packit fd8b60
\def\sphinx@TYI@stop@multicolumn{\egroup % varwidth was used with \tymax
Packit fd8b60
    \xdef\sphinx@tempb{\the\dimexpr\wd\z@/\sphinx@tempa}% per column width
Packit fd8b60
    \endgroup
Packit fd8b60
    \expandafter\sphinx@TYI@multispan\expandafter{\sphinx@tempa}%
Packit fd8b60
}%
Packit fd8b60
\def\sphinx@TYI@multispan #1{%
Packit fd8b60
    \kern\sphinx@tempb\ignorespaces % the per column occupied width
Packit fd8b60
    \ifnum#1>\@ne % repeat, taking into account subtleties of TeX's & ...
Packit fd8b60
      \expandafter\sphinx@TYI@multispan@next\expandafter{\the\numexpr#1-\@ne\expandafter}%
Packit fd8b60
    \fi
Packit fd8b60
}%
Packit fd8b60
\def\sphinx@TYI@multispan@next{&\relax\sphinx@TYI@multispan}%
Packit fd8b60
%
Packit fd8b60
% Now the branch handling either the second pass of tabulary or the single pass
Packit fd8b60
% of tabular/longtable. This is the delicate part where we gather the
Packit fd8b60
% dimensions from the p columns either set-up by tabulary or by user p column
Packit fd8b60
% or Sphinx \X, \Y columns. The difficulty is that to get the said width, the
Packit fd8b60
% template must be inserted (other hacks would be horribly complicated except
Packit fd8b60
% if we rewrote crucial parts of LaTeX's \@array !) and we can not do
Packit fd8b60
% \omit\span like standard \multicolumn's easy approach. Thus we must cancel
Packit fd8b60
% the \vrule separators. Also, perhaps the column specifier is of the l, c, r
Packit fd8b60
% type, then we attempt an ad hoc rescue to give varwidth a reasonable target
Packit fd8b60
% width.
Packit fd8b60
\def\sphinx@start@multicolumn#1{%
Packit fd8b60
    \gdef\sphinx@multiwidth{0pt}\gdef\sphinx@tempa{#1}\sphinx@multispan{#1}%
Packit fd8b60
}%
Packit fd8b60
\def\sphinx@multispan #1{%
Packit fd8b60
    \ifnum#1=\@ne\expandafter\sphinx@multispan@end
Packit fd8b60
            \else\expandafter\sphinx@multispan@next
Packit fd8b60
    \fi {#1}%
Packit fd8b60
}%
Packit fd8b60
\def\sphinx@multispan@next #1{%
Packit fd8b60
    % trick to recognize L, C, R, J or p, m, b type columns
Packit fd8b60
    \ifdim\baselineskip>\z@
Packit fd8b60
       \gdef\sphinx@tempb{\linewidth}%
Packit fd8b60
    \else
Packit fd8b60
    % if in an l, r, c type column, try and hope for the best
Packit fd8b60
       \xdef\sphinx@tempb{\the\dimexpr(\ifx\TY@final\@undefined\linewidth\else
Packit fd8b60
            \sphinx@TY@tablewidth\fi-\arrayrulewidth)/\sphinx@tempa
Packit fd8b60
            -\tw@\tabcolsep-\arrayrulewidth\relax}%
Packit fd8b60
    \fi
Packit fd8b60
    \noindent\kern\sphinx@tempb\relax
Packit fd8b60
    \xdef\sphinx@multiwidth
Packit fd8b60
           {\the\dimexpr\sphinx@multiwidth+\sphinx@tempb+\tw@\tabcolsep+\arrayrulewidth}%
Packit fd8b60
    % hack the \vline and the colortbl macros
Packit fd8b60
    \sphinx@hack@vline\sphinx@hack@CT&\relax
Packit fd8b60
    % repeat
Packit fd8b60
    \expandafter\sphinx@multispan\expandafter{\the\numexpr#1-\@ne}%
Packit fd8b60
}%
Packit fd8b60
% packages like colortbl add group levels, we need to "climb back up" to be
Packit fd8b60
% able to hack the \vline and also the colortbl inserted tokens. This creates
Packit fd8b60
% empty space whether or not the columns were | separated:
Packit fd8b60
\def\sphinx@hack@vline{\ifnum\currentgrouptype=6\relax
Packit fd8b60
    \kern\arrayrulewidth\arrayrulewidth\z@\else\aftergroup\sphinx@hack@vline\fi}%
Packit fd8b60
\def\sphinx@hack@CT{\ifnum\currentgrouptype=6\relax
Packit fd8b60
    \let\CT@setup\sphinx@CT@setup\else\aftergroup\sphinx@hack@CT\fi}%
Packit fd8b60
% It turns out \CT@row@color is not expanded contrarily to \CT@column@color
Packit fd8b60
% during LaTeX+colortbl preamble preparation, hence it would be possible for
Packit fd8b60
% \sphinx@CT@setup to discard only the column color and choose to obey or not
Packit fd8b60
% row color and cell color. It would even be possible to propagate cell color
Packit fd8b60
% to row color for the duration of the Sphinx multicolumn... the (provisional?)
Packit fd8b60
% choice has been made to cancel the colortbl colours for the multicolumn
Packit fd8b60
% duration.
Packit fd8b60
\def\sphinx@CT@setup #1\endgroup{\endgroup}% hack to remove colour commands
Packit fd8b60
\def\sphinx@multispan@end#1{%
Packit fd8b60
    % first, trace back our steps horizontally
Packit fd8b60
    \noindent\kern-\dimexpr\sphinx@multiwidth\relax
Packit fd8b60
    % and now we set the final computed width for the varwidth environment
Packit fd8b60
    \ifdim\baselineskip>\z@
Packit fd8b60
       \xdef\sphinx@multiwidth{\the\dimexpr\sphinx@multiwidth+\linewidth}%
Packit fd8b60
    \else
Packit fd8b60
       \xdef\sphinx@multiwidth{\the\dimexpr\sphinx@multiwidth+
Packit fd8b60
         (\ifx\TY@final\@undefined\linewidth\else
Packit fd8b60
          \sphinx@TY@tablewidth\fi-\arrayrulewidth)/\sphinx@tempa
Packit fd8b60
          -\tw@\tabcolsep-\arrayrulewidth\relax}%
Packit fd8b60
    \fi
Packit fd8b60
    % we need to remove colour set-up also for last cell of the multi-column
Packit fd8b60
    \aftergroup\sphinx@hack@CT
Packit fd8b60
}%
Packit fd8b60
\newcommand*\sphinxcolwidth[2]{%
Packit fd8b60
  % this dimension will always be used for varwidth, and serves as maximum
Packit fd8b60
  % width when cells are merged either via multirow or multicolumn or both,
Packit fd8b60
  % as always their contents is wrapped in varwidth environment.
Packit fd8b60
  \ifnum#1>\@ne % multi-column (and possibly also multi-row)
Packit fd8b60
  % we wrote our own multicolumn code especially to handle that (and allow
Packit fd8b60
  % verbatim contents)
Packit fd8b60
   \ifx\equation$%$
Packit fd8b60
       \tymax % first pass of tabulary (cf MEMO above regarding nesting)
Packit fd8b60
   \else % the \@gobble thing is for compatibility with standard \multicolumn
Packit fd8b60
       \sphinx@multiwidth\@gobble{#1/#2}%
Packit fd8b60
   \fi
Packit fd8b60
  \else % single column multirow
Packit fd8b60
   \ifx\TY@final\@undefined % not a tabulary.
Packit fd8b60
    \ifdim\baselineskip>\z@
Packit fd8b60
        % in a p{..} type column, \linewidth is the target box width
Packit fd8b60
        \linewidth
Packit fd8b60
    \else
Packit fd8b60
        % l, c, r columns. Do our best.
Packit fd8b60
        \dimexpr(\linewidth-\arrayrulewidth)/#2-
Packit fd8b60
                \tw@\tabcolsep-\arrayrulewidth\relax
Packit fd8b60
    \fi
Packit fd8b60
   \else % in tabulary
Packit fd8b60
    \ifx\equation$%$% first pass
Packit fd8b60
        \tymax % it is set to a big value so that paragraphs can express themselves
Packit fd8b60
    \else
Packit fd8b60
     % second pass.
Packit fd8b60
     \ifdim\baselineskip>\z@
Packit fd8b60
         \linewidth % in a L, R, C, J column or a p, \X, \Y ...
Packit fd8b60
     \else
Packit fd8b60
         % we have hacked \TY@final to put in \sphinx@TY@tablewidth the table width
Packit fd8b60
         \dimexpr(\sphinx@TY@tablewidth-\arrayrulewidth)/#2-
Packit fd8b60
                 \tw@\tabcolsep-\arrayrulewidth\relax
Packit fd8b60
     \fi
Packit fd8b60
    \fi
Packit fd8b60
   \fi
Packit fd8b60
  \fi
Packit fd8b60
}%
Packit fd8b60
% fallback default in case user has set latex_use_latex_multicolumn to True:
Packit fd8b60
% \sphinxcolwidth will use this only inside LaTeX's standard \multicolumn
Packit fd8b60
\def\sphinx@multiwidth #1#2{\dimexpr % #1 to gobble the \@gobble (!)
Packit fd8b60
    (\ifx\TY@final\@undefined\linewidth\else\sphinx@TY@tablewidth\fi
Packit fd8b60
     -\arrayrulewidth)*#2-\tw@\tabcolsep-\arrayrulewidth\relax}%
Packit fd8b60
%
Packit fd8b60
% --- MULTIROW ---
Packit fd8b60
% standard \multirow
Packit fd8b60
% 1. does not allow verbatim contents,
Packit fd8b60
% 2. does not allow blank lines in its argument,
Packit fd8b60
% 3. its * specifier means to typeset "horizontally" which is very
Packit fd8b60
%    bad for paragraph content. 2016 version has = specifier but it
Packit fd8b60
%    must be used with p type columns only, else results are bad,
Packit fd8b60
% 4. it requires manual intervention if the contents is too long to fit
Packit fd8b60
%    in the asked-for number of rows.
Packit fd8b60
% 5. colour panels (either from \rowcolor or \columncolor) will hide
Packit fd8b60
%    the bottom part of multirow text, hence manual tuning is needed
Packit fd8b60
%    to put the multirow insertion at the _bottom_.
Packit fd8b60
%
Packit fd8b60
% The Sphinx solution consists in always having contents wrapped
Packit fd8b60
% in a varwidth environment so that it makes sense to estimate how many
Packit fd8b60
% lines it will occupy, and then ensure by insertion of suitable struts
Packit fd8b60
% that the table rows have the needed height. The needed mark-up is done
Packit fd8b60
% by LaTeX writer, which has its own id for the merged cells.
Packit fd8b60
%
Packit fd8b60
% The colour issue is solved by clearing colour panels in all cells,
Packit fd8b60
% whether or not the multirow is single-column or multi-column.
Packit fd8b60
%
Packit fd8b60
% In passing we obtain baseline alignements across rows (only if
Packit fd8b60
% \arraylinestretch is 1, as LaTeX's does not obey \arraylinestretch in "p"
Packit fd8b60
% multi-line contents, only first and last line...)
Packit fd8b60
%
Packit fd8b60
% TODO: examine the situation with \arraylinestretch > 1. The \extrarowheight
Packit fd8b60
% is hopeless for multirow anyhow, it makes baseline alignment strictly
Packit fd8b60
% impossible.
Packit fd8b60
\newcommand\sphinxmultirow[2]{\begingroup
Packit fd8b60
  % #1 = nb of spanned rows, #2 = Sphinx id of "cell", #3 = contents
Packit fd8b60
  % but let's fetch #3 in a way allowing verbatim contents !
Packit fd8b60
  \def\sphinx@nbofrows{#1}\def\sphinx@cellid{#2}%
Packit fd8b60
  \afterassignment\sphinx@multirow\let\next=
Packit fd8b60
}%
Packit fd8b60
\def\sphinx@multirow {%
Packit fd8b60
  \setbox\z@\hbox\bgroup\aftergroup\sphinx@@multirow\strut
Packit fd8b60
}%
Packit fd8b60
\def\sphinx@@multirow {%
Packit fd8b60
  % The contents, which is a varwidth environment, has been captured in
Packit fd8b60
  % \box0 (a \hbox).
Packit fd8b60
  % We have with \sphinx@cellid an assigned unique id. The goal is to give
Packit fd8b60
  % about the same height to all the involved rows.
Packit fd8b60
  % For this Sphinx will insert a \sphinxtablestrut{cell_id} mark-up
Packit fd8b60
  % in LaTeX file and the expansion of the latter will do the suitable thing.
Packit fd8b60
  \dimen@\dp\z@
Packit fd8b60
  \dimen\tw@\ht\@arstrutbox
Packit fd8b60
  \advance\dimen@\dimen\tw@
Packit fd8b60
  \advance\dimen\tw@\dp\@arstrutbox
Packit fd8b60
  \count@=\dimen@       % type conversion dim -> int
Packit fd8b60
  \count\tw@=\dimen\tw@
Packit fd8b60
  \divide\count@\count\tw@ % TeX division truncates
Packit fd8b60
  \advance\dimen@-\count@\dimen\tw@
Packit fd8b60
  % 1300sp is about 0.02pt. For comparison a rule default width is 0.4pt.
Packit fd8b60
  % (note that if \count@ holds 0, surely \dimen@>1300sp)
Packit fd8b60
  \ifdim\dimen@>1300sp \advance\count@\@ne \fi
Packit fd8b60
  % now \count@ holds the count L of needed "lines"
Packit fd8b60
  % and \sphinx@nbofrows holds the number N of rows
Packit fd8b60
  % we have L >= 1 and N >= 1
Packit fd8b60
  % if L is a multiple of N, ... clear what to do !
Packit fd8b60
  % else write L = qN + r, 1 <= r < N and we will
Packit fd8b60
  % arrange for each row to have enough space for:
Packit fd8b60
  %     q+1 "lines" in each of the first r rows
Packit fd8b60
  %       q "lines" in each of the (N-r) bottom rows
Packit fd8b60
  % for a total of (q+1) * r + q * (N-r) = q * N + r = L
Packit fd8b60
  % It is possible that q == 0.
Packit fd8b60
  \count\tw@\count@
Packit fd8b60
  % the TeX division truncates
Packit fd8b60
  \divide\count\tw@\sphinx@nbofrows\relax
Packit fd8b60
  \count4\count\tw@ % q
Packit fd8b60
  \multiply\count\tw@\sphinx@nbofrows\relax
Packit fd8b60
  \advance\count@-\count\tw@ % r
Packit fd8b60
  \expandafter\xdef\csname sphinx@tablestrut_\sphinx@cellid\endcsname
Packit fd8b60
     {\noexpand\sphinx@tablestrut{\the\count4}{\the\count@}{\sphinx@cellid}}%
Packit fd8b60
  \dp\z@\z@
Packit fd8b60
  % this will use the real height if it is >\ht\@arstrutbox
Packit fd8b60
  \sphinxtablestrut{\sphinx@cellid}\box\z@
Packit fd8b60
  \endgroup % group was opened in \sphinxmultirow
Packit fd8b60
}%
Packit fd8b60
\newcommand*\sphinxtablestrut[1]{%
Packit fd8b60
  % #1 is a "cell_id", i.e. the id of a merged group of table cells
Packit fd8b60
  \csname sphinx@tablestrut_#1\endcsname
Packit fd8b60
}%
Packit fd8b60
% LaTeX typesets the table row by row, hence each execution can do
Packit fd8b60
% an update for the next row.
Packit fd8b60
\newcommand*\sphinx@tablestrut[3]{\begingroup
Packit fd8b60
  % #1 = q, #2 = (initially) r, #3 = cell_id, q+1 lines in first r rows
Packit fd8b60
  % if #2 = 0, create space for max(q,1) table lines
Packit fd8b60
  % if #2 > 0, create space for q+1 lines and decrement #2
Packit fd8b60
  \leavevmode
Packit fd8b60
  \count@#1\relax
Packit fd8b60
  \ifnum#2=\z@
Packit fd8b60
    \ifnum\count@=\z@\count@\@ne\fi
Packit fd8b60
  \else
Packit fd8b60
    % next row will be with a #2 decremented by one
Packit fd8b60
    \expandafter\xdef\csname sphinx@tablestrut_#3\endcsname
Packit fd8b60
      {\noexpand\sphinx@tablestrut{#1}{\the\numexpr#2-\@ne}{#3}}%
Packit fd8b60
   \advance\count@\@ne
Packit fd8b60
  \fi
Packit fd8b60
  \vrule\@height\ht\@arstrutbox
Packit fd8b60
        \@depth\dimexpr\count@\ht\@arstrutbox+\count@\dp\@arstrutbox-\ht\@arstrutbox\relax
Packit fd8b60
        \@width\z@
Packit fd8b60
  \endgroup
Packit fd8b60
  % we need this to avoid colour panels hiding bottom parts of multirow text
Packit fd8b60
  \sphinx@hack@CT
Packit fd8b60
}%
Packit fd8b60
\endinput
Packit fd8b60
%%
Packit fd8b60
%% End of file `sphinxmulticell.sty'.