| 1 |
% (C) 2006-2026 Josselin Noirel |
| 2 |
% |
| 3 |
% Package cellspace: Ensuring a minimal spacing of table cells. |
| 4 |
% |
| 5 |
% This material is subject to the LaTeX Project Public License. See |
| 6 |
% https://www.latex-project.org/lppl/ . |
| 7 |
% |
| 8 |
\def \filedate {2026/01/24}% |
| 9 |
\def \fileversion {v1.9.1a}% |
| 10 |
|
| 11 |
\ProvidesFile{cellspace}% |
| 12 |
[\filedate \space \fileversion \space cellspace documentation (JN)] |
| 13 |
\pdfoutput = 1 |
| 14 |
\documentclass[12pt]{ltxdoc} |
| 15 |
|
| 16 |
\usepackage{ifpdf} |
| 17 |
\ifpdf |
| 18 |
\PassOptionsToPackage{pdftex}{geometry} |
| 19 |
\else |
| 20 |
\PassOptionsToPackage{dvips}{geometry} |
| 21 |
\fi |
| 22 |
\usepackage[ascii]{inputenc} |
| 23 |
\usepackage[T1]{fontenc} |
| 24 |
\usepackage{amsmath} |
| 25 |
\usepackage{textcomp} |
| 26 |
\usepackage{lmodern} |
| 27 |
\usepackage{microtype} |
| 28 |
\usepackage[a4paper, margin = 1.3in]{geometry} |
| 29 |
\usepackage[math]{cellspace} |
| 30 |
\usepackage{booktabs} |
| 31 |
\usepackage{tabularx} |
| 32 |
\usepackage{moreverb} |
| 33 |
\usepackage{floatrow} |
| 34 |
\floatsetup{margins = centering} |
| 35 |
\usepackage{algorithmic} |
| 36 |
\usepackage[british]{babel} |
| 37 |
|
| 38 |
% ----------------------------------------------------------------- |
| 39 |
|
| 40 |
\newcommand*{\compare}[2]{% |
| 41 |
\par \begingroup \trivlist \item \centering |
| 42 |
%% \parbox[t]{0.5\linewidth}{#1}% |
| 43 |
%% \parbox[t]{0.5\linewidth}{#2}% |
| 44 |
\vskip-\baselineskip |
| 45 |
\makebox[0pt]{% |
| 46 |
\mbox{#1}% |
| 47 |
\quad |
| 48 |
\mbox{#2}% |
| 49 |
} |
| 50 |
\endtrivlist \endgroup \par |
| 51 |
} |
| 52 |
\renewcommand*{\meta}[1]{\textnormal{\textlangle \emph{#1}\textrangle}} |
| 53 |
\newcommand*{\pack}{\textsf} |
| 54 |
\newcommand*{\tex}[1]{\texttt{#1}} |
| 55 |
\renewcommand*{\cmd}[1]{\tex{\textbackslash #1}} |
| 56 |
\makeatletter |
| 57 |
\newenvironment*{showexample}{% |
| 58 |
\par \vskip-\baselineskip |
| 59 |
\bgroup \obeylines |
| 60 |
\@showexample |
| 61 |
}{% |
| 62 |
\endverbatimwrite |
| 63 |
\endgroup |
| 64 |
\begingroup |
| 65 |
\footnotesize |
| 66 |
\samepage |
| 67 |
\verbatimtabinput{cellspace.tmp}% |
| 68 |
\endgroup |
| 69 |
\nobreak \@nobreaktrue \vskip-\baselineskip \vskip\z@skip |
| 70 |
\begingroup \@nameuse{\@showexampleenv}% |
| 71 |
\small |
| 72 |
\input{cellspace.tmp}% |
| 73 |
\@nameuse{end\@showexampleenv}\endgroup |
| 74 |
\par \bigbreak |
| 75 |
\@endparenv |
| 76 |
} |
| 77 |
\newcommand*{\@showexample}[1][center]{% |
| 78 |
\egroup |
| 79 |
\def \@showexampleenv {#1}% |
| 80 |
\begingroup |
| 81 |
\verbatimwrite{cellspace.tmp}% |
| 82 |
} |
| 83 |
\makeatother |
| 84 |
|
| 85 |
% ----------------------------------------------------------------- |
| 86 |
|
| 87 |
\setlength{\cellspacetoplimit}{2pt} |
| 88 |
\setlength{\cellspacebottomlimit}{2pt} |
| 89 |
\setcounter{secnumdepth}{0} |
| 90 |
\addparagraphcolumntypes{X} |
| 91 |
|
| 92 |
% ----------------------------------------------------------------- |
| 93 |
|
| 94 |
\GetFileInfo{cellspace.sty} |
| 95 |
|
| 96 |
\title {The \pack{cellspace} package} |
| 97 |
\author {Josselin Noirel\footnote{This document corresponds to version |
| 98 |
\fileversion\ (\filedate) of \pack{\filename}.}\\% |
| 99 |
\url{https://github.com/JosselinNoirel/cellspace}} |
| 100 |
\date {\filedate---\fileversion} |
| 101 |
|
| 102 |
\begin{document} |
| 103 |
|
| 104 |
\maketitle |
| 105 |
|
| 106 |
\begin{abstract} |
| 107 |
This package is intended to allow automatic spacing out of the lines of |
| 108 |
an array. People often complain about text touching the \cmd{hline} of |
| 109 |
a tabular when it is too high or too deep. For example (with the |
| 110 |
\pack{amsmath} package) with \tex{\textbackslash dfrac\{1\}\{2\}} |
| 111 |
surrounded by \cmd{hline}s. This package provides a modifier \tex{S} to |
| 112 |
usual column types (\tex{l}, \tex{c}, \tex{r}, \tex{p}, \tex{m}, |
| 113 |
and~\tex{b}) that ensures a minimal spacing between rules and cells of |
| 114 |
an array. |
| 115 |
\end{abstract} |
| 116 |
|
| 117 |
\section{Introduction} |
| 118 |
|
| 119 |
The mechanism used by \LaTeX{} to build tables---using struts---has an |
| 120 |
important consequence: the cells of a table that extend too much tend to |
| 121 |
touch horizontal rules. First example: |
| 122 |
% |
| 123 |
\begin{showexample} |
| 124 |
\begin{tabular}{cc} |
| 125 |
\hline |
| 126 |
\itshape Bond & |
| 127 |
\itshape Distance ($\mathrm{\mathring{A}})$ \\ |
| 128 |
\hline |
| 129 |
C--C & $1.53$ \\ |
| 130 |
C--H & $1.10$ \\ |
| 131 |
\hline |
| 132 |
\end{tabular} |
| 133 |
\end{showexample} |
| 134 |
% |
| 135 |
Second example: |
| 136 |
% |
| 137 |
\begin{showexample} |
| 138 |
\begin{tabular}{c} |
| 139 |
\hline |
| 140 |
\itshape Formula \\ |
| 141 |
\hline |
| 142 |
$\displaystyle |
| 143 |
e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots |
| 144 |
+ \frac{1}{k!} + \cdots $ \\ |
| 145 |
$\displaystyle |
| 146 |
\pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots |
| 147 |
+ \frac{(-1)^k}{2 k + 1} |
| 148 |
+ \cdots \right)$ \\ |
| 149 |
\hline |
| 150 |
\end{tabular} |
| 151 |
\end{showexample} |
| 152 |
% |
| 153 |
The classical workarounds \cmd{\textbackslash}\oarg{dimen} and |
| 154 |
\cmd{noalign}\tex{\{}\cmd{vspace}\marg{dimen}\tex{\}} are not very powerful |
| 155 |
and need fine tuning. |
| 156 |
|
| 157 |
\section{How to solve this?} |
| 158 |
|
| 159 |
The \pack{cellspace} loads several packages to carry out its job: |
| 160 |
\pack{array}, \pack{ifthen}, and~\pack{calc} (along \tex{amsmath} when |
| 161 |
the option \tex{math} is passed to \tex{cellspace}). It redefines several |
| 162 |
internals, still not too many (\cmd{@startpbox} and \cmd{@endpbox}). By |
| 163 |
default, the tables will behave as usual. To improve the spacing of your |
| 164 |
tables, you must change the table preamble and prepend \tex{S} to the |
| 165 |
column types \tex{l}, \tex{c}, \tex{r}. The same holds for the paragraph |
| 166 |
columns \tex{p}, \tex{m}, and~\tex{b}, except that they must be surrounded |
| 167 |
by an extra pair of braces. For instance, the default behaviour of a |
| 168 |
table beginning with |
| 169 |
% |
| 170 |
\begin{verbatim} |
| 171 |
\begin{tabular}{l l l p{3cm}} |
| 172 |
\end{verbatim} |
| 173 |
% |
| 174 |
should be changed into |
| 175 |
% |
| 176 |
\begin{verbatim} |
| 177 |
\begin{tabular}{Sl Sl Sl S{p{3cm}}} |
| 178 |
\end{verbatim} |
| 179 |
|
| 180 |
The \pack{cellspace} has two parameters governing the spacing of the |
| 181 |
cells. The dimension \cmd{cellspacetoplimit} is the minimal spacing |
| 182 |
required between the actual text the cell is made up of and the top of the |
| 183 |
cell (where \cmd{hline}s may appear), if the spacing is less than this |
| 184 |
threshold, a space of \cmd{cellspacetoplimit} is added, otherwise nothing |
| 185 |
is done. Conversely, at the bottom the dimension |
| 186 |
\cmd{cellspacebottomlimit} is the minimal space required between the |
| 187 |
bottom of the cell and the text itself. This parameters can be changed in |
| 188 |
the document preamble using \cmd{setlength}. Together with |
| 189 |
\pack{booktabs}, the results look rather good. \pack{cellspace} may work |
| 190 |
with other column types (defined through \cmd{newcolumntype} for |
| 191 |
instance); it will assume by default that an unknown column is a LR-mode |
| 192 |
column. Otherwise you have to tell \pack{cellspace} something like |
| 193 |
% |
| 194 |
\begin{verbatim} |
| 195 |
\addparagraphcolumntypes{X} |
| 196 |
\end{verbatim} |
| 197 |
% |
| 198 |
(The previous example is needed to make \pack{tabularx} work with |
| 199 |
\pack{cellspace}.) |
| 200 |
|
| 201 |
The following examples use a setting of $2\,\mathrm{pt}$ for both top and |
| 202 |
bottom space limits. |
| 203 |
% |
| 204 |
\compare{% |
| 205 |
\begin{tabular}[t]{cc} |
| 206 |
\hline |
| 207 |
\itshape Bond & |
| 208 |
\itshape Distance ($\mathrm{\mathring{A}})$ \\ |
| 209 |
\hline |
| 210 |
C--C & $1.53$ \\ |
| 211 |
C--H & $1.10$ \\ |
| 212 |
\hline |
| 213 |
\end{tabular} |
| 214 |
}{% |
| 215 |
\begin{tabular}[t]{Sc Sc} |
| 216 |
\hline |
| 217 |
\itshape Bond & |
| 218 |
\itshape Distance ($\mathrm{\mathring{A}})$ \\ |
| 219 |
\hline |
| 220 |
C--C & $1.53$ \\ |
| 221 |
C--H & $1.10$ \\ |
| 222 |
\hline |
| 223 |
\end{tabular} |
| 224 |
} |
| 225 |
% |
| 226 |
Some space is added at the top the first row because it doesn't fulfil |
| 227 |
the condition imposed by the package. Similarly some space is added at |
| 228 |
the bottom of the first row. It has to be noted, however, that no space |
| 229 |
is added in other rows because they are far enough from the cell |
| 230 |
separations. |
| 231 |
|
| 232 |
\compare{% |
| 233 |
\begin{tabular}[t]{c} |
| 234 |
\hline |
| 235 |
\itshape Formula \\ |
| 236 |
\hline |
| 237 |
$\displaystyle |
| 238 |
e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots |
| 239 |
+ \frac{1}{k!} + \cdots $ \\ |
| 240 |
$\displaystyle |
| 241 |
\pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots |
| 242 |
+ \frac{(-1)^k}{2 k + 1} |
| 243 |
+ \cdots \right)$ \\ |
| 244 |
\hline |
| 245 |
\end{tabular} |
| 246 |
}{% |
| 247 |
\begin{tabular}[t]{Sc} |
| 248 |
\hline |
| 249 |
\itshape Formula \\ |
| 250 |
\hline |
| 251 |
$\displaystyle |
| 252 |
e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots |
| 253 |
+ \frac{1}{k!} + \cdots $ \\ |
| 254 |
$\displaystyle |
| 255 |
\pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots |
| 256 |
+ \frac{(-1)^k}{2 k + 1} |
| 257 |
+ \cdots \right)$ \\ |
| 258 |
\hline |
| 259 |
\end{tabular} |
| 260 |
} |
| 261 |
% |
| 262 |
Here it can be immediately remarked that the `Formula' row has the same |
| 263 |
height in both examples. Only the formula lines are affected. The best |
| 264 |
layout is obtained with \pack{booktabs}: |
| 265 |
% |
| 266 |
\compare{% |
| 267 |
\begin{tabular}[t]{@{} c @{}} |
| 268 |
\toprule |
| 269 |
\itshape Formula \\ |
| 270 |
\midrule |
| 271 |
$\displaystyle |
| 272 |
e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots |
| 273 |
+ \frac{1}{k!} + \cdots $ \\ |
| 274 |
$\displaystyle |
| 275 |
\pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots |
| 276 |
+ \frac{(-1)^k}{2 k + 1} |
| 277 |
+ \cdots \right)$ \\ |
| 278 |
\bottomrule |
| 279 |
\end{tabular} |
| 280 |
}{% |
| 281 |
\begin{tabular}[t]{@{} Sc @{}} |
| 282 |
\toprule |
| 283 |
\itshape Formula \\ |
| 284 |
\midrule |
| 285 |
$\displaystyle |
| 286 |
e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots |
| 287 |
+ \frac{1}{k!} + \cdots $ \\ |
| 288 |
$\displaystyle |
| 289 |
\pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots |
| 290 |
+ \frac{(-1)^k}{2 k + 1} |
| 291 |
+ \cdots \right)$ \\ |
| 292 |
\bottomrule |
| 293 |
\end{tabular} |
| 294 |
} |
| 295 |
|
| 296 |
The following example shows that how it works with paragraph columns. |
| 297 |
% |
| 298 |
\compare{% |
| 299 |
\begin{tabular}[t]{p{5cm}} |
| 300 |
\hline |
| 301 |
$\displaystyle \frac{1}{2}$ |
| 302 |
A A A A A A A A A A A A A A |
| 303 |
A A A A A A A A A A A A A A |
| 304 |
A A A A A A A A A A A A A A |
| 305 |
A A A A A A A A A A A A A A |
| 306 |
A A A A A A A A A A A A A A |
| 307 |
$\displaystyle \frac{1}{2}$ \\ |
| 308 |
\hline |
| 309 |
\end{tabular} |
| 310 |
}{% |
| 311 |
\begin{tabular}[t]{S{p{5cm}}} |
| 312 |
\hline |
| 313 |
$\displaystyle \frac{1}{2}$ |
| 314 |
A A A A A A A A A A A A A A |
| 315 |
A A A A A A A A A A A A A A |
| 316 |
A A A A A A A A A A A A A A |
| 317 |
A A A A A A A A A A A A A A |
| 318 |
A A A A A A A A A A A A A A |
| 319 |
$\displaystyle \frac{1}{2}$ \\ |
| 320 |
\hline |
| 321 |
\end{tabular} |
| 322 |
} |
| 323 |
|
| 324 |
\section{Matrices} |
| 325 |
|
| 326 |
The package can be loaded with the options \tex{nomath} (default) and |
| 327 |
\tex{math}. The latter does two things: it first loads the |
| 328 |
\tex{amsmath} package and then redefines |
| 329 |
the command that is invoked when one typesets matrices |
| 330 |
(\tex{\textbackslash env@matrix} --- this improvement was suggested by |
| 331 |
Bastien Roucaries). The following |
| 332 |
examples illustrate what happens with (left) or without (right) |
| 333 |
\pack{cellspace}'s correction in a \tex{pmatrix} environment. |
| 334 |
% |
| 335 |
\[ |
| 336 |
\begingroup |
| 337 |
\makeatletter |
| 338 |
\def\env@matrix{\hskip -\arraycolsep |
| 339 |
\let\@ifnextchar\new@ifnextchar |
| 340 |
\array{*\c@MaxMatrixCols c}} |
| 341 |
\makeatother |
| 342 |
% |
| 343 |
\text{\tex{nomath} option\ }% |
| 344 |
\begin{pmatrix} |
| 345 |
+\frac{1}{2} & +\frac{1}{3} \\ |
| 346 |
-\frac{1}{3} & +\frac{1}{2} \\ |
| 347 |
\end{pmatrix} |
| 348 |
\endgroup |
| 349 |
\qquad |
| 350 |
\text{\tex{math} option\ }% |
| 351 |
\begin{pmatrix} |
| 352 |
+\frac{1}{2} & +\frac{1}{3} \\ |
| 353 |
-\frac{1}{3} & +\frac{1}{2} \\ |
| 354 |
\end{pmatrix} |
| 355 |
\] |
| 356 |
|
| 357 |
As a~consequence, the \tex{amsmath} package can be loaded beforehand |
| 358 |
with other packages (such as \tex{empheq} or \tex{mathtools}), were an |
| 359 |
incompatibility to arise from one's loading it later. |
| 360 |
|
| 361 |
\section{Using a different column name} |
| 362 |
|
| 363 |
An other column name than \tex{S} can be used if \tex{S} conflicts |
| 364 |
with the user's own definitions or a package's. A keyval option can |
| 365 |
be passed to the package to that effect; |
| 366 |
% |
| 367 |
\begin{verbatim} |
| 368 |
\usepackage[column=O]{cellspace} |
| 369 |
\end{verbatim} |
| 370 |
% |
| 371 |
can be used to provide cellspace's functionality through the \tex{O} |
| 372 |
column modifier instead of the \tex{S} column modifier. |
| 373 |
|
| 374 |
\section{Bugs and limitations} |
| 375 |
|
| 376 |
This package hasn't been heavily tested, so there may be plenty of bugs. |
| 377 |
As usual, bugs will certainly arise in complicated situations because |
| 378 |
tables can become very complicated (for instance, this package hasn't been |
| 379 |
designed with nested tables into mind). In simple cases however, it should |
| 380 |
work nicely. The package loads \pack{array} ensuring a peaceful |
| 381 |
collaboration with each other. Other packages of special interest haven't |
| 382 |
been tested: \pack{tabularx}\footnote{A one minute test seems to indicate |
| 383 |
that it works provided \pack{tabularx} is loaded after \pack{cellspace}. |
| 384 |
The example on this page illustrates this. A thirty second test |
| 385 |
indicates that \pack{cellspace} works correctly with \pack{longtable} as |
| 386 |
long as only LR-mode columns are concerned.} and \pack{longtable} in |
| 387 |
particular. |
| 388 |
% |
| 389 |
\begin{trivlist}\item |
| 390 |
\begin{tabularx}{\linewidth}{l SX} |
| 391 |
\hline |
| 392 |
A & B B B B B B B B \\ |
| 393 |
A A & B B B B B B B B |
| 394 |
B B B B B B B B \\ |
| 395 |
A A A A & $\dfrac{1}{2}$B B B B B B B B |
| 396 |
B B B B B B B B |
| 397 |
B B B B B B B B |
| 398 |
B B B B B B B B$\dfrac{1}{2}$ \\ |
| 399 |
\hline |
| 400 |
\end{tabularx} |
| 401 |
\end{trivlist} |
| 402 |
|
| 403 |
\pack{tabls}\footnote{Thanks to Jim Hefferon for pointing this out.} is a |
| 404 |
package that does a similar job as \pack{cellspace}. |
| 405 |
The differences are: \pack{tabls} affects a whole table while |
| 406 |
\pack{cellspace} affects only the columns on which applies the modifier |
| 407 |
\tex{S}. Each cell affected by \pack{cellspace} is affected in the same |
| 408 |
way: there must be enough space above ($>\cmd{cellspacetoplimit}$) and |
| 409 |
below the text ($>\cmd{cellspacebottomlimit}$), even if no \cmd{hline} is |
| 410 |
present. In the other hand, \pack{tabls} ensures a minimal distance using |
| 411 |
one threshold parameter \cmd{tablinesep} applying between rows not |
| 412 |
separated by a rule and using a systematic spacer \cmd{arraylinesep} when |
| 413 |
there is a rule (whereas \pack{cellspace} can leave a cell as is if the |
| 414 |
distance to the rule is big enough). As a last point, \pack{tabls}, as |
| 415 |
far as I can see, cannot work with \pack{array}. |
| 416 |
|
| 417 |
\section{What's new} |
| 418 |
|
| 419 |
\begin{description} |
| 420 |
\item [v1.9.1] The package now defines \cmd{@startpbox@action}. |
| 421 |
|
| 422 |
\item [v1.9.0] The new implementation solves a problem with empty |
| 423 |
cells for paragraph-type columns (\tex{m}, \tex{p}, \tex{b}) by |
| 424 |
testing whether \cmd{prevdepth} is greater than 1000~pt (see |
| 425 |
example below). The package has now been made aware of the new |
| 426 |
implementation of \cmd{@endpbox} of \tex{array} (again). |
| 427 |
|
| 428 |
% \begin{tabular}{S{p{4cm}}} \hline \\ \hline \end{tabular} |
| 429 |
|
| 430 |
\item [v1.8.1] Package becomes aware of the latest version of the |
| 431 |
array package. |
| 432 |
\end{description} |
| 433 |
|
| 434 |
\end{document} |