Debian Codegraph @

texmf-dist/doc/latex/vcell/vcell.tex

raw
1 \documentclass{article}
2 \usepackage{vcell,url,booktabs,multirow,tabularx,listings,ragged2e,color,colortbl}
3
4 \lstloadlanguages{[LaTeX]TeX}
5 \lstset{
6 basicstyle=\ttfamily,
7 columns=fullflexible,
8 backgroundcolor=\color[rgb]{0.82,0.82,0.82},
9 language=[LaTeX]TeX}
10
11
12 \begin{document}
13 \title{The package \texttt{vcell} : Vertical alignment for cells}
14 \author{Xavier Dionne\thanks{The author wrote this package for its online table generator (\texttt{https://latex-tables.com}). You can contact him at info [at] latex-tables [dot] com.}}
15 \date{April 30, 2021}
16 \maketitle
17
18 \section{Introduction}
19
20 This package offers a serie of low-level macros to align vertically the content of a cell, whether it has a fixed height or a variable height.
21
22 \section{Using \texttt{vcell}}
23
24 The idea behind \texttt{vcell} is to measure the height of cells to calculate the height of the row and then to print the cells in another row. This second row will be printed over the first one\footnote{Which can be understand as ``cheating'' for some.}. This process is not automated.
25
26 In a row where cells are vertically-aligned, all cells must be vertically-aligned. Each cells can have a different alignment however.
27
28 The commands available are those :
29 \begin{center}
30 \noindent\begin{tabularx}{\textwidth}{rX}
31 \verb|\savecellbox{<content>}|&Measure and save a cell that is vertically aligned. It will have to be printed in a second row.\\
32 \verb|\vcell{<content>}|&Shortcut for \verb|\savecellbox|\\
33 \verb|\printcelltop|&Print a top-aligned cell\\
34 \verb|\printcellmiddle|&Print a middle-aligned cell\\
35 \verb|\printcellbottom|&Print a bottom-aligned cell\\
36 \verb|\resetcellcount|&Reset the measurement of cells. This will be done automatically once you print all saved cells\\
37 \end{tabularx}
38 \end{center}
39 Once your cells are measured, the following lengths will be available :
40 \begin{center}
41 \noindent\begin{tabular}{rl}
42 \verb|\rowht|&The height of the row\\
43 \verb|\rowdp|&The depth of the row\\
44 \verb|\rowheight|&The total height of the row\\
45 \end{tabular}
46 \end{center}
47
48 \section{Basic example}
49
50 The following table :
51
52
53 \begin{center}
54 \noindent\begin{tabular}{@{}c@{}lcr@{}}
55 \toprule
56 & Top cell & Middle cell & Bottom cell\\\midrule
57 \vcell{\rule{0pt}{40pt}}&\savecellbox{Top}&\savecellbox{Middle}&\savecellbox{Bottom}\\[-\rowheight]
58 \printcellmiddle&\printcelltop&\printcellmiddle&\printcellbottom\\\bottomrule
59 \end{tabular}
60 \end{center}
61
62
63
64 can be created with the following code :
65
66 \begin{lstlisting}[breaklines]
67 \begin{tabular}{@{}c@{}lcr@{}}
68 \toprule
69 & Top cell
70 & Middle cell
71 & Bottom cell
72 \\ \midrule
73 \savecellbox{\rule{0pt}{40pt}}
74 & \savecellbox{Top}
75 & \savecellbox{Middle}
76 & \savecellbox{Bottom}
77 \\[-\rowheight]
78 \printcellmiddle
79 & \printcelltop
80 & \printcellmiddle
81 & \printcellbottom
82 \\ \bottomrule
83 \end{tabular}
84 \end{lstlisting}
85
86 To ensure the height of the table, an invisible strut is used. However, any content could be used instead, even content with variable height. You can see the alignment of the table with this figure :
87
88 \begin{center}
89 \noindent\begin{tabular}{@{}clcr@{}}
90 \toprule
91 & Top cell & Middle cell & Bottom cell\\\midrule
92 \savecellbox{\rule{10pt}{40pt}}&\savecellbox{\rule{10pt}{20pt}}&\savecellbox{\rule{10pt}{20pt}}&\savecellbox{\rule{10pt}{20pt}}\\[-\rowheight]
93 \printcellmiddle&\printcelltop&\printcellmiddle&\printcellbottom\\\bottomrule
94 \end{tabular}
95 \end{center}
96
97 The commands works even if all the cells have the same height, as shown in the following table :
98
99 \begin{center}
100 \noindent\begin{tabular}{lcr@{}}
101 \toprule
102 Top cell & Middle cell & Bottom cell\\\midrule
103 \savecellbox{Top}&\savecellbox{Middle}&\savecellbox{Bottom}\\[-\rowheight]
104 \printcelltop&\printcellmiddle&\printcellbottom\\\bottomrule
105 \end{tabular}
106 \end{center}
107
108
109 \section{Special cases}
110
111
112
113 \subsection{Usage of \texttt{multicolumn}}
114
115 Whenever you want to use the command \verb|\multicolumn| because you want to change the alignment of your cell or because you want your cell to span several column, you have to repeat the command in the second row.
116
117 \begin{center}
118 \noindent\begin{tabular}{@{}c@{}lcr@{}}
119 \toprule
120 & Top cell & Top cell & Bottom cell\\\midrule
121 \savecellbox{\rule{0pt}{40pt}}&\multicolumn{2}{c}{\savecellbox{Top (2 cols)}}&\savecellbox{Bottom}\\[-\rowheight]
122 \printcellmiddle&\multicolumn{2}{c}{\printcelltop}&\printcellbottom\\\bottomrule
123 \end{tabular}
124 \end{center}
125
126 The previous table can be created with the following code :
127
128 \begin{lstlisting}[breaklines]
129 \begin{tabular}{@{}c@{}lcr@{}}
130 \toprule
131 & Top cell
132 & Top cell
133 & Bottom cell
134 \\ \midrule
135 \savecellbox{\rule{0pt}{40pt}}
136 & \multicolumn{2}{c}{\savecellbox{Top (2 cols)}}
137 & \savecellbox{Bottom}
138 \\ [-\rowheight]
139 \printcellmiddle
140 & \multicolumn{2}{c}{\printcelltop}
141 & \printcellbottom
142 \\ \bottomrule
143 \end{tabular}
144 \end{lstlisting}
145
146 \subsection{Paragraph columns}
147
148 A frequent case is the use of paragraph columns (i.e. \verb|p|, \verb|m| or \verb|b| columns). The only rule is to use \verb|p| columns (or \verb|X| columns from the \texttt{tabularx} package) and avoid \verb|m| columns, otherwise it will mess with the alignment.
149
150 \begin{center}
151 \newdimen\rrowheight
152 \noindent\begin{tabular}{@{}p{80pt}p{45pt}p{90pt}p{45pt}c@{}}
153 \toprule
154 Middle-p cell & Top-p cell & Bottom-p cell&Bottom-p cell & Middle cell\\\midrule
155 \savecellbox{This is a paragraph that starts at the center and keeps continuing}&\savecellbox{Paragraph from top}&\savecellbox{This is the biggest paragraph. It is aligned from the bottom but you can't notice that.}&\savecellbox{Paragraph from bottom}&\savecellbox{Middle}\\\noalign{\global\setlength{\rrowheight}{\rowheight}\vspace{-\rowheight}}
156 \printcellmiddle&\printcelltop&\printcellbottom&\printcellbottom&\printcellmiddle\\\bottomrule\noalign{\vspace{2pt}\verb|\rowheight=|\the\rrowheight}
157 \end{tabular}
158 \end{center}
159
160 It can be produced by this code :
161
162 \begin{lstlisting}[breaklines]
163 \begin{tabular}{@{}p{80pt}p{45pt}p{90pt}p{45pt}c@{}}
164 \toprule
165 Middle-p cell
166 & Top-p cell
167 & Bottom-p cell
168 & Bottom-p cell
169 & Middle cell
170 \\ \midrule
171 \savecellbox{This is a paragraph that starts at the center and keeps continuing}
172 & \savecellbox{Paragraph from top}
173 & \savecellbox{This is the biggest paragraph. It is aligned from the bottom but you can't notice that.}
174 & \savecellbox{Paragraph from bottom}
175 & \savecellbox{Middle}
176 \\ [-\rowheight]
177 \printcellmiddle
178 & \printcelltop
179 & \printcellbottom
180 & \printcellbottom
181 & \printcellmiddle
182 \\ \bottomrule
183 \end{tabular}
184 \end{lstlisting}
185
186 \subsection{Usage with \texttt{colortbl}}
187
188 You can use coloured cells with \texttt{colortbl}. However, the color declaration must be repeated before \verb|\printcellmiddle|, \verb|\printcelltop| or \verb|\printcellbottom| commands.
189
190 \begin{center}
191 \noindent\begin{tabular}{|>{\columncolor{red}}p{60pt}|c|}
192 \hline
193 Middle-p cell & \cellcolor{yellow}Bottom cell\\\hline
194 \savecellbox{This is a paragraph that starts at the center and keeps continuing}&\cellcolor{green}\savecellbox{Paragraph from bottom}\\[-\rowheight]
195 \printcellmiddle&\cellcolor{green}\printcellbottom\\\hline
196 \end{tabular}
197 \end{center}
198
199 \begin{lstlisting}[breaklines]
200 \begin{tabular}{|>{\columncolor{red}}p{60pt}|c|}
201 \hline
202 Middle-p cell
203 & \cellcolor{yellow} Bottom cell
204 \\ \hline
205 \savecellbox{This is a paragraph that starts at the center and keeps continuing}
206 & \cellcolor{green}\savecellbox{Paragraph from bottom}
207 \\ [-\rowheight]
208 \printcellmiddle
209 & \cellcolor{green}\printcellbottom
210 \\ \hline
211 \end{tabular}
212 \end{lstlisting}
213
214 \subsection{Usage with \texttt{multirow}}
215
216 \texttt{vcell} does not support cells that spans several rows. If you want to use \verb|\multirow| in the same row as vertically-aligned cells, just use \verb|multirow| as you would normally do and skip it in the second row. You don't have to adjust the first argument of \verb|multirow|. You might want to use the optional argument of \verb|multirow| to get the vertical alignment that you want.
217
218 \begin{center}
219 \noindent\begin{tabular}{|c|>{\centering}p{60pt}|c|}
220 \hline
221 Multirow cell & Middle-p cell & Bottom cell\\\hline
222 \multirow{2}{*}[-3.5em]{Two rows}&\savecellbox{ This is a paragraph that starts at the center and keeps continuing}&\savecellbox{Paragraph from bottom}\\[-\rowheight]
223 &\printcellmiddle&\printcellbottom\\\cline{2-3}
224 &\savecellbox{This is another paragraph that starts at the center and keeps continuing}&\savecellbox{Second paragraph from bottom}\\[-\rowheight]
225 &\printcellmiddle&\printcellbottom\\\hline
226 \end{tabular}
227 \end{center}
228 \begin{lstlisting}[breaklines]
229 \begin{tabular}{|c|>{\centering}p{60pt}|c|}
230 \hline
231 Multirow cell
232 & Middle-p cell
233 & Bottom cell
234 \\ \hline
235 \multirow{2}{*}[-3.5em]{Two rows}
236 & \savecellbox{This is a paragraph that starts at the center and keeps continuing}
237 & \savecellbox{Paragraph from bottom}
238 \\ [-\rowheight]
239 % We skip for the multirow
240 & \printcellmiddle
241 & \printcellbottom
242 \\ \cline{2-3}
243 & \savecellbox{This is another paragraph that starts at the center and keeps continuing}
244 & \savecellbox{Second paragraph from bottom}
245 \\ [-\rowheight]
246 % We skip for the multirow
247 & \printcellmiddle
248 & \printcellbottom
249 \\ \hline
250 \end{tabular}
251 \end{lstlisting}
252 \section{Issues and limitations}
253
254 This is a list of known issues and limitations from this package :
255
256 \begin{itemize}
257 \item Content generated by \verb|>{...}| or \verb|<{...}| in the preamble are not measured and may produce errors in some cases.
258 \item \verb!m! columns will mess with vertical alignment.
259 \item Glues such as \verb|\dotfill| or \verb|\hrulefill| are not supported in cells
260 \item \verb|\verb| command is not supported in cells
261 \end{itemize}
262
263 However, we took great care to ensure that the content of the cell is only evaluated once, that no driver-specific commands were used and that selection in PDFs was not broken.
264
265 \section{Licence}
266
267 The contents of this package are distributed under the \LaTeX{} Project
268 Public License,
269 version 1.3c or later.
270
271
272 \end{document}