From e0c6872cf40896c7be36b11dcc744620f10adf1d Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Mon, 2 Sep 2019 13:46:59 +0900 Subject: Initial commit --- macros/latex/contrib/cellspace/README | 25 ++ macros/latex/contrib/cellspace/cellspace.pdf | Bin 0 -> 336907 bytes macros/latex/contrib/cellspace/cellspace.sty | 201 +++++++++++++ macros/latex/contrib/cellspace/cellspace.tex | 425 +++++++++++++++++++++++++++ 4 files changed, 651 insertions(+) create mode 100644 macros/latex/contrib/cellspace/README create mode 100644 macros/latex/contrib/cellspace/cellspace.pdf create mode 100644 macros/latex/contrib/cellspace/cellspace.sty create mode 100644 macros/latex/contrib/cellspace/cellspace.tex (limited to 'macros/latex/contrib/cellspace') diff --git a/macros/latex/contrib/cellspace/README b/macros/latex/contrib/cellspace/README new file mode 100644 index 0000000000..7adfa2eac6 --- /dev/null +++ b/macros/latex/contrib/cellspace/README @@ -0,0 +1,25 @@ +% (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 . + +This package is intended to allow automatic spacing out of +the lines of an array. People often complain about text touching the +\hline of a tabular when it is too high or too deep. For example (with +the amsmath package) a \dfrac{1}{2} surrounded by \hlines touches the +horizontal rules. This package provides a modifier S to usual column +types (l, c, r, p, m, and b) that ensures a minimal spacing between rules +and cells of an array. + +v1.6: New feature as suggested by Bastien Roucaries: an option [math] + has been introduced, which allows to use cellspace's features into + matrix environments. + +v1.7: New features as suggested by Bastien Roucaries: (1) Possibility + to change the name of the column used for spacing; (2) Make the + math column independent of the colum name. + +v1.8.1: Package becomes aware of the latest modifications of the array + package. Also the GitHub project is referenced in the documentation. \ No newline at end of file diff --git a/macros/latex/contrib/cellspace/cellspace.pdf b/macros/latex/contrib/cellspace/cellspace.pdf new file mode 100644 index 0000000000..5755b527f5 Binary files /dev/null and b/macros/latex/contrib/cellspace/cellspace.pdf differ diff --git a/macros/latex/contrib/cellspace/cellspace.sty b/macros/latex/contrib/cellspace/cellspace.sty new file mode 100644 index 0000000000..8a7735490b --- /dev/null +++ b/macros/latex/contrib/cellspace/cellspace.sty @@ -0,0 +1,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{}{} +\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 diff --git a/macros/latex/contrib/cellspace/cellspace.tex b/macros/latex/contrib/cellspace/cellspace.tex new file mode 100644 index 0000000000..5c4da1937e --- /dev/null +++ b/macros/latex/contrib/cellspace/cellspace.tex @@ -0,0 +1,425 @@ +% (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}% + +\ProvidesFile{cellspace}% + [\filedate \space \fileversion \space cellspace documentation (JN)] +\pdfoutput = 1 +\documentclass[12pt]{ltxdoc} + +\usepackage{ifpdf} +\ifpdf + \PassOptionsToPackage{pdftex}{geometry} +\else + \PassOptionsToPackage{dvips}{geometry} +\fi +\usepackage[ascii]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{amsmath} +\usepackage{textcomp} +\usepackage{lmodern} +\usepackage{microtype} +\usepackage[a4paper, margin = 1.3in]{geometry} +\usepackage[math]{cellspace} +\usepackage{booktabs} +\usepackage{tabularx} +\usepackage{moreverb} +\usepackage{floatrow} + \floatsetup{margins = centering} +\usepackage{algorithmic} +\usepackage[british]{babel} + +% ----------------------------------------------------------------- + +\newcommand*{\compare}[2]{% + \par \begingroup \trivlist \item \centering +%% \parbox[t]{0.5\linewidth}{#1}% +%% \parbox[t]{0.5\linewidth}{#2}% + \vskip-\baselineskip + \makebox[0pt]{% + \mbox{#1}% + \quad + \mbox{#2}% + } + \endtrivlist \endgroup \par +} +\renewcommand*{\meta}[1]{\textnormal{\textlangle \emph{#1}\textrangle}} +\newcommand*{\pack}{\textsf} +\newcommand*{\tex}[1]{\texttt{#1}} +\renewcommand*{\cmd}[1]{\tex{\textbackslash #1}} +\makeatletter +\newenvironment*{showexample}{% + \par \vskip-\baselineskip + \bgroup \obeylines + \@showexample +}{% + \endverbatimwrite + \endgroup + \begingroup + \footnotesize + \samepage + \verbatimtabinput{cellspace.tmp}% + \endgroup + \nobreak \@nobreaktrue \vskip-\baselineskip \vskip\z@skip + \begingroup \@nameuse{\@showexampleenv}% + \small + \input{cellspace.tmp}% + \@nameuse{end\@showexampleenv}\endgroup + \par \bigbreak + \@endparenv +} +\newcommand*{\@showexample}[1][center]{% + \egroup + \def \@showexampleenv {#1}% + \begingroup + \verbatimwrite{cellspace.tmp}% +} +\makeatother + +% ----------------------------------------------------------------- + +\setlength{\cellspacetoplimit}{2pt} +\setlength{\cellspacebottomlimit}{2pt} +\setcounter{secnumdepth}{0} +\addparagraphcolumntypes{X} + +% ----------------------------------------------------------------- + +\GetFileInfo{cellspace.sty} + +\title {The \pack{cellspace} package} +\author {Josselin Noirel\footnote{This document corresponds to version + \fileversion\ (\filedate) of \pack{\filename}.}\\% +\url{https://github.com/JosselinNoirel/cellspace}} +\date {\filedate---\fileversion} + +\begin{document} + +\maketitle + +\begin{abstract} + This package is intended to allow automatic spacing out of the lines of + an array. People often complain about text touching the \cmd{hline} of + a tabular when it is too high or too deep. For example (with the + \pack{amsmath} package) with \tex{\textbackslash dfrac\{1\}\{2\}} + surrounded by \cmd{hline}s. This package provides a modifier \tex{S} to + usual column types (\tex{l}, \tex{c}, \tex{r}, \tex{p}, \tex{m}, + and~\tex{b}) that ensures a minimal spacing between rules and cells of + an array. +\end{abstract} + +\section{Introduction} + +The mechanism used by \LaTeX{} to build tables---using struts---has an +important consequence: the cells of a table that extend too much tend to +touch horizontal rules. First example: +% +\begin{showexample} +\begin{tabular}{cc} + \hline + \itshape Bond & + \itshape Distance ($\mathrm{\mathring{A}})$ \\ + \hline + C--C & $1.53$ \\ + C--H & $1.10$ \\ + \hline +\end{tabular} +\end{showexample} +% +Second example: +% +\begin{showexample} +\begin{tabular}{c} + \hline + \itshape Formula \\ + \hline + $\displaystyle + e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots + + \frac{1}{k!} + \cdots $ \\ + $\displaystyle + \pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots + + \frac{(-1)^k}{2 k + 1} + + \cdots \right)$ \\ + \hline +\end{tabular} +\end{showexample} +% +The classical workarounds \cmd{\textbackslash}\oarg{dimen} and +\cmd{noalign}\tex{\{}\cmd{vspace}\marg{dimen}\tex{\}} are not very powerful +and need fine tuning. + +\section{How to solve this?} + +The \pack{cellspace} loads several packages to carry out its job: +\pack{array}, \pack{ifthen}, and~\pack{calc} (along \tex{amsmath} when +the option \tex{math} is passed to \tex{cellspace}). It redefines several +internals, still not too many (\cmd{@startpbox} and \cmd{@endpbox}). By +default, the tables will behave as usual. To improve the spacing of your +tables, you must change the table preamble and prepend \tex{S} to the +column types \tex{l}, \tex{c}, \tex{r}. The same holds for the paragraph +columns \tex{p}, \tex{m}, and~\tex{b}, except that they must be surrounded +by an extra pair of braces. For instance, the default behaviour of a +table beginning with +% +\begin{verbatim} +\begin{tabular}{l l l p{3cm}} +\end{verbatim} +% +should be changed into +% +\begin{verbatim} +\begin{tabular}{Sl Sl Sl S{p{3cm}}} +\end{verbatim} + +The \pack{cellspace} has two parameters governing the spacing of the +cells. The dimension \cmd{cellspacetoplimit} is the minimal spacing +required between the actual text the cell is made up of and the top of the +cell (where \cmd{hline}s may appear), if the spacing is less than this +threshold, a space of \cmd{cellspacetoplimit} is added, otherwise nothing +is done. Conversely, at the bottom the dimension +\cmd{cellspacebottomlimit} is the minimal space required between the +bottom of the cell and the text itself. This parameters can be changed in +the document preamble using \cmd{setlength}. Together with +\pack{booktabs}, the results look rather good. \pack{cellspace} may work +with other column types (defined through \cmd{newcolumntype} for +instance); it will assume by default that an unknown column is a LR-mode +column. Otherwise you have to tell \pack{cellspace} something like +% +\begin{verbatim} +\addparagraphcolumntypes{X} +\end{verbatim} +% +(The previous example is needed to make \pack{tabularx} work with +\pack{cellspace}.) + +The following examples use a setting of $2\,\mathrm{pt}$ for both top and +bottom space limits. +% +\compare{% + \begin{tabular}[t]{cc} + \hline + \itshape Bond & + \itshape Distance ($\mathrm{\mathring{A}})$ \\ + \hline + C--C & $1.53$ \\ + C--H & $1.10$ \\ + \hline + \end{tabular} +}{% + \begin{tabular}[t]{Sc Sc} + \hline + \itshape Bond & + \itshape Distance ($\mathrm{\mathring{A}})$ \\ + \hline + C--C & $1.53$ \\ + C--H & $1.10$ \\ + \hline + \end{tabular} +} +% +Some space is added at the top the first row because it doesn't fulfil +the condition imposed by the package. Similarly some space is added at +the bottom of the first row. It has to be noted, however, that no space +is added in other rows because they are far enough from the cell +separations. + +\compare{% + \begin{tabular}[t]{c} + \hline + \itshape Formula \\ + \hline + $\displaystyle + e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots + + \frac{1}{k!} + \cdots $ \\ + $\displaystyle + \pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots + + \frac{(-1)^k}{2 k + 1} + + \cdots \right)$ \\ + \hline + \end{tabular} +}{% + \begin{tabular}[t]{Sc} + \hline + \itshape Formula \\ + \hline + $\displaystyle + e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots + + \frac{1}{k!} + \cdots $ \\ + $\displaystyle + \pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots + + \frac{(-1)^k}{2 k + 1} + + \cdots \right)$ \\ + \hline + \end{tabular} +} +% +Here it can be immediately remarked that the `Formula' row has the same +height in both examples. Only the formula lines are affected. The best +layout is obtained with \pack{booktabs}: +% +\compare{% + \begin{tabular}[t]{@{} c @{}} + \toprule + \itshape Formula \\ + \midrule + $\displaystyle + e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots + + \frac{1}{k!} + \cdots $ \\ + $\displaystyle + \pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots + + \frac{(-1)^k}{2 k + 1} + + \cdots \right)$ \\ + \bottomrule + \end{tabular} +}{% + \begin{tabular}[t]{@{} Sc @{}} + \toprule + \itshape Formula \\ + \midrule + $\displaystyle + e = 1 + \frac{1}{2} + \frac{1}{6} + \cdots + + \frac{1}{k!} + \cdots $ \\ + $\displaystyle + \pi = 4 \left( 1 - \frac{1}{3} + \frac{1}{5} + \cdots + + \frac{(-1)^k}{2 k + 1} + + \cdots \right)$ \\ + \bottomrule + \end{tabular} +} + +The following example shows that how it works with paragraph columns. +% +\compare{% + \begin{tabular}[t]{p{5cm}} + \hline + $\displaystyle \frac{1}{2}$ + A A A A A A A A A A A A A A + A A A A A A A A A A A A A A + A A A A A A A A A A A A A A + A A A A A A A A A A A A A A + A A A A A A A A A A A A A A + $\displaystyle \frac{1}{2}$ \\ + \hline + \end{tabular} +}{% + \begin{tabular}[t]{S{p{5cm}}} + \hline + $\displaystyle \frac{1}{2}$ + A A A A A A A A A A A A A A + A A A A A A A A A A A A A A + A A A A A A A A A A A A A A + A A A A A A A A A A A A A A + A A A A A A A A A A A A A A + $\displaystyle \frac{1}{2}$ \\ + \hline + \end{tabular} +} + +\section{Matrices} + +The package can be loaded with the options \tex{nomath} (default) and +\tex{math}. The latter does two things: it first loads the +\tex{amsmath} package and then redefines +the command that is invoked when one typesets matrices +(\tex{\textbackslash env@matrix} --- this improvement was suggested by +Bastien Roucaries). The following +examples illustrate what happens with (left) or without (right) +\pack{cellspace}'s correction in a \tex{pmatrix} environment. +% +\[ +\begingroup +\makeatletter + \def\env@matrix{\hskip -\arraycolsep + \let\@ifnextchar\new@ifnextchar + \array{*\c@MaxMatrixCols c}} +\makeatother +% +\text{\tex{nomath} option\ }% +\begin{pmatrix} ++\frac{1}{2} & +\frac{1}{3} \\ +-\frac{1}{3} & +\frac{1}{2} \\ +\end{pmatrix} +\endgroup +\qquad +\text{\tex{math} option\ }% +\begin{pmatrix} ++\frac{1}{2} & +\frac{1}{3} \\ +-\frac{1}{3} & +\frac{1}{2} \\ +\end{pmatrix} +\] + +As a~consequence, the \tex{amsmath} package can be loaded beforehand +with other packages (such as \tex{empheq} or \tex{mathtools}), were an +incompatibility to arise from one's loading it later. + +\section{Using a different column name} + +An other column name than \tex{S} can be used if \tex{S} conflicts +with the user's own definitions or a package's. A keyval option can +be passed to the package to that effect; +% +\begin{verbatim} +\usepackage[column=O]{cellspace} +\end{verbatim} +% +can be used to provide cellspace's functionality through the \tex{O} +column modifier instead of the \tex{S} column modifier. + + +\section{Bugs and limitations} + +This package hasn't been heavily tested, so there may be plenty of bugs. +As usual, bugs will certainly arise in complicated situations because +tables can become very complicated (for instance, this package hasn't been +designed with nested tables into mind). In simple cases however, it should +work nicely. The package loads \pack{array} ensuring a peaceful +collaboration with each other. Other packages of special interest haven't +been tested: \pack{tabularx}\footnote{A one minute test seems to indicate + that it works provided \pack{tabularx} is loaded after \pack{cellspace}. + The example on this page illustrates this. A thirty second test +indicates that \pack{cellspace} works correctly with \pack{longtable} as +long as only LR-mode columns are concerned.} and \pack{longtable} in +particular. +% +\begin{trivlist}\item +\begin{tabularx}{\linewidth}{l SX} + \hline + A & B B B B B B B B \\ + A A & B B B B B B B B + B B B B B B B B \\ + A A A A & $\dfrac{1}{2}$B B B B B B B B + B B B B B B B B + B B B B B B B B + B B B B B B B B$\dfrac{1}{2}$ \\ + \hline +\end{tabularx} +\end{trivlist} + +\pack{tabls}\footnote{Thanks to Jim Hefferon for pointing this out.} is a +package that does a similar job as \pack{cellspace}. +The differences are: \pack{tabls} affects a whole table while +\pack{cellspace} affects only the columns on which applies the modifier +\tex{S}. Each cell affected by \pack{cellspace} is affected in the same +way: there must be enough space above ($>\cmd{cellspacetoplimit}$) and +below the text ($>\cmd{cellspacebottomlimit}$), even if no \cmd{hline} is +present. In the other hand, \pack{tabls} ensures a minimal distance using +one threshold parameter \cmd{tablinesep} applying between rows not +separated by a rule and using a systematic spacer \cmd{arraylinesep} when +there is a rule (whereas \pack{cellspace} can leave a cell as is if the +distance to the rule is big enough). As a last point, \pack{tabls}, as +far as I can see, cannot work with \pack{array}. + +\section{What's new} + +\begin{description} +\item [v1.8.1] Package becomes aware of the latest version of the + array package +\end{description} + +\end{document} -- cgit v1.2.3