summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/cellspace/cellspace.sty
blob: 8a7735490b7294bc0fe49401432b5004c188036f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
% (C) 2006-2019 Josselin Noirel
% 
% Package cellspace: Ensuring a minimal spacing of table cells.
%
% This material is subject to the LaTeX Project Public License. See
% http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html .
%
\def \filedate    {2019/03/11}%
\def \fileversion {v1.8.1}%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{cellspace}%
  [\filedate \space \fileversion \space Spacing table cells (JN)]

\RequirePackage{ifthen}  % \newboolean, \ifthenelse
\RequirePackage{array}   % \newcolumntype, \@{start,end}pbox
\RequirePackage{calc}    % \setlength{<dimen>}{<expression>}
\RequirePackage{xkeyval} % key=val options

% 2. Check the definitions of \@startpbox and \@endpbox
%    from array.sty

\@ifpackagelater{array}{2018/12/30}{%
      % New versions of \@startpbox / \@endpbox
      % Includes a \color@...group
      \CheckCommand*{\@startpbox}[1]{%
        \bgroup
          \color@begingroup
            \setlength\hsize{#1}%
            \@arrayparboxrestore
            \everypar{%
                  \vrule \@height \ht\@arstrutbox \@width \z@
                  \everypar{}}%
      }%
      %
      \CheckCommand*{\@endpbox}{\@finalstrut \@arstrutbox \color@endgroup \egroup \hfil}%
}{%
      % Older versions of \@startpbox / \@endpbox
      \CheckCommand*{\@startpbox}[1]{%
         \bgroup
            \setlength\hsize{#1}%
            \@arrayparboxrestore
            \everypar{%
                  \vrule \@height \ht\@arstrutbox \@width \z@
                  \everypar{}%
            }%
      }%
      %
      \CheckCommand*{\@endpbox}{\@finalstrut \@arstrutbox \egroup \hfil}%
}

  % New version of array.sty

% 3. Declare registers and other variables

\newboolean{cellspace@lr} % Is it a LR column type ?
\newboolean{cellspace@}   % Is it a paragraph column that needs to be
                          % spaced out?
\newboolean{cellspace@m}  % [math] option: matrices

\@ifdefinable \cellspacetoplimit    {\newdimen \cellspacetoplimit}
\@ifdefinable \cellspacebottomlimit {\newdimen \cellspacebottomlimit}
\@ifdefinable \cellspace@firstht {\newdimen \cellspace@firstht} % Global
\@ifdefinable \cellspace@lastdp {\newdimen \cellspace@lastdp}   % Global

\setlength{\cellspacetoplimit}{1pt}
\setlength{\cellspacebottomlimit}{1pt}

% Options

\DeclareOptionX{math}{\setboolean{cellspace@m}{true}}
\DeclareOptionX{nomath}{\setboolean{cellspace@m}{false}}
\define@key{cellspace.sty}{column}{\def\cellspace@column{#1}}

\ExecuteOptionsX{nomath, column=S}
\ProcessOptionsX

% 4. Redefinitions

\renewcommand*{\@startpbox}[1]{%
  \bgroup
    \setbox \@tempboxa = \vtop \bgroup
      \color@begingroup
      \setlength{\hsize}{#1}%
      \@arrayparboxrestore
      % We need to test \ifcellspace@ within the \everypar
      % because it is still not set correctly
      \everypar{%
        \unless \ifcellspace@
          \vrule \@height \ht\@arstrutbox \@width \z@
        \fi
        \everypar{}%
      }%
}

\renewcommand*{\@endpbox}{%
      \unless \ifcellspace@
        \@finalstrut \@arstrutbox
      \fi
      \par
      % Save the depth of the last line
      \global \cellspace@lastdp = \prevdepth
      \expandafter % v1.7 This expandafter is now important
      \color@endgroup
      % \ifcellspace@ is only locally true, so we need to expand it before
      % \egroup stops it action
      \expandafter
    \egroup
    \ifcellspace@
      % Save the height of the first line
      \global \cellspace@firstht = \ht\@tempboxa
      \setbox \@tempboxa = \vbox {%
        \setlength{\@tempdima}{\ht\@arstrutbox - \cellspace@firstht}%
        \ifthenelse{\lengthtest{\@tempdima<\cellspacetoplimit}}{%
          \setlength{\@tempdima}{\cellspace@firstht + \cellspacetoplimit}%
        }{%
          \setlength{\@tempdima}{\ht\@arstrutbox}%
        }%
        \hbox{\vrule height \@tempdima width 0pt depth 0pt }%
        \nointerlineskip
        \vskip-\cellspace@firstht
        \unvbox \@tempboxa
        \setlength{\@tempdima}{\dp\@arstrutbox - \cellspace@lastdp}%
        \ifthenelse{\lengthtest{\@tempdima<\cellspacebottomlimit}}{%
          \setlength{\@tempdima}{\cellspace@lastdp + \cellspacebottomlimit}%
        }{%
          \setlength{\@tempdima}{\dp\@arstrutbox}%
        }%
        \vskip-\cellspace@lastdp
        \nointerlineskip
        \hbox{\vrule height 0pt depth \@tempdima width 0pt }%
      }%
    \fi
    \unvbox \@tempboxa
  \egroup
  \hfil
}

\let \@@startpbox = \@startpbox
\let \@@endpbox = \@endpbox

% Make it work with matrices
% 31 Jul 2009: suggested by Bastien Roucaries (for a long time)

\ifcellspace@m
  \RequirePackage{amsmath}%
  \def \env@matrix {%
    \hskip -\arraycolsep
    \let \@ifnextchar \new@ifnextchar
    \array{%
          *{\c@MaxMatrixCols}%
           {>{\bcolumn c\@nil$}c<{$\ecolumn}}@{}}}%
\fi

% 5. Defining a prefix

\expandafter
\newcolumntype
\expandafter{\cellspace@column}[1]%
    {>{\bcolumn #1\@nil}#1<{\ecolumn}}

\newcommand*{\bcolumn}{}
\def \bcolumn #1#2\@nil {%
  \cellspace@lrtrue
  \@expandtwoargs \in@{#1}{\cellspace@parcoltypes}%
  \ifin@
    \cellspace@lrfalse
  \fi
  \ifcellspace@lr
    \begingroup \lrbox {\@tempboxa}%
  \else
    \cellspace@true
  \fi
}

\newcommand*{\ecolumn}{%
  \ifcellspace@lr
    \endlrbox \endgroup
    \setlength{\@tempdima}{\ht\@arstrutbox - \ht\@tempboxa}%
    \ifthenelse{\lengthtest{\@tempdima<\cellspacetoplimit}}{%
      \setlength{\@tempdima}{\ht\@tempboxa + \cellspacetoplimit}%
      \ht\@tempboxa = \@tempdima
    }{}%
    \setlength{\@tempdima}{\dp\@arstrutbox - \dp\@tempboxa}%
    \ifthenelse{\lengthtest{\@tempdima<\cellspacebottomlimit}}{%
      \setlength{\@tempdima}{\dp\@tempboxa + \cellspacebottomlimit}%
      \dp\@tempboxa = \@tempdima
    }{}%
    \usebox{\@tempboxa}%
  \fi
}

\newcommand*{\cellspace@parcoltypes}{pmb}

\newcommand*{\addparagraphcolumntypes}[1]{%
  \edef \cellspace@parcoltypes {\cellspace@parcoltypes #1}%
}

\@onlypreamble \addparagraphcolumntypes

\endinput