Debian Codegraph @

texmf-dist/source/latex/listliketab/listliketab.dtx

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2005 by Scott Pakin <[email protected]>
4 % -------------------------------------------------------
5 %
6 % This file may be distributed and/or modified under the
7 % conditions of the LaTeX Project Public License, either version 1.2
8 % of this license or (at your option) any later version.
9 % The latest version of this license is in:
10 %
11 % http://www.latex-project.org/lppl.txt
12 %
13 % and version 1.2 or later is part of all distributions of LaTeX
14 % version 1999/12/01 or later.
15 %
16 % \fi
17 %
18 % \iffalse
19 %<*driver>
20 \ProvidesFile{listliketab.dtx}
21 %</driver>
22 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
23 %<package>\ProvidesPackage{listliketab}
24 %<*package>
25 [2005/01/09 v1.0a Create list-like tabulars]
26 %</package>
27 %
28 %<*driver>
29 \documentclass{ltxdoc}
30 \usepackage{listliketab}
31 \usepackage{tabularx}
32 \usepackage{amstext}
33 \usepackage{hyperref}
34 \EnableCrossrefs
35 \CodelineIndex
36 \RecordChanges
37 \begin{document}
38 \DocInput{listliketab.dtx}
39 \phantomsection
40 \addcontentsline{toc}{section}{Change History}
41 \PrintChanges
42 \phantomsection
43 \addcontentsline{toc}{section}{Index}
44 \PrintIndex
45 \end{document}
46 %</driver>
47 % \fi
48 %
49 % \CheckSum{100}
50 %
51 % \CharacterTable
52 % {Upper-case \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
53 % Lower-case \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
54 % Digits \0\1\2\3\4\5\6\7\8\9
55 % Exclamation \! Double quote \" Hash (number) \#
56 % Dollar \$ Percent \% Ampersand \&
57 % Acute accent \' Left paren \( Right paren \)
58 % Asterisk \* Plus \+ Comma \,
59 % Minus \- Point \. Solidus \/
60 % Colon \: Semicolon \; Less than \<
61 % Equals \= Greater than \> Question mark \?
62 % Commercial at \@ Left bracket \[ Backslash \\
63 % Right bracket \] Circumflex \^ Underscore \_
64 % Grave accent \` Left brace \{ Vertical bar \|
65 % Right brace \} Tilde \~}
66 %
67 %
68 % \changes{v1.0}{2000/09/08}{Initial version}
69 % \changes{v1.0a}{2005/01/09}{Cleaned up the \string\texttt{.dtx} file}
70 %
71 % \GetFileInfo{listliketab.dtx}
72 %
73 % \DoNotIndex{\begin,\catcode,\DeclareRobustCommand,\def,\dp,\end}
74 % \DoNotIndex{\expandafter,\gdef,\global,\hfill,\hspace,\ht}
75 % \DoNotIndex{\ignorespaces,\ignorespacesafterend,\item}
76 % \DoNotIndex{\newenvironment,\newlength,\newsavebox,\noindent}
77 % \DoNotIndex{\ratio,\renewcommand,\setlength,\strip@pt,\strutbox}
78 % \DoNotIndex{\vspace,\xdef}
79 %
80 %
81 % \title{The \textsf{listliketab} package\thanks{This document
82 % corresponds to \textsf{listliketab}~\fileversion, dated \filedate.}}
83 % \author{Scott Pakin \\ \texttt{[email protected]}}
84 %
85 % \hypersetup{^^A
86 % pdftitle={The listliketab package},
87 % pdfauthor={Scott Pakin <[email protected]>},
88 % pdfsubject={LaTeX package to make tabulars look like lists},
89 % pdfkeywords={tabulars, tables, lists, itemize, enumerate, columns}
90 % }
91 %
92 % \maketitle
93 % \sloppy
94 %
95 % ^^A Define an environment for command/environment delcarations.
96 % \newsavebox{\declbox}
97 % \newenvironment{decl}{%
98 % \begin{lrbox}{\declbox}\begin{tabular}{l}}{%
99 % \end{tabular}\end{lrbox}%
100 % \vspace{3ex}\noindent\hspace*{-3em}\fbox{\usebox{\declbox}}\vspace*{2ex}}
101 %
102 % \begin{abstract}
103 % The \texttt{listliketab} package helps the user make list-like
104 % \texttt{tabular}s, i.e., a \texttt{tabular} that is indistinguishable
105 % from an \texttt{itemize} or \texttt{enumerate} environment.
106 % The advantage of using a \texttt{tabular} is that the user can add
107 % additional columns to each entry in the list.
108 % \end{abstract}
109 %
110 % \section{Introduction}
111 %
112 % Here's an itemized list:
113 %
114 % \begin{itemize}
115 % \item Fee
116 % \item Fi
117 % \item Fo
118 % \item Fum
119 % \end{itemize}
120 %
121 % \noindent
122 % Here's another itemized list:
123 %
124 % \storestyleof{itemize}
125 % \begin{listliketab}
126 % \begin{tabular}{LlR}
127 % \textbullet & Fee \\
128 % \textbullet & Fi \\
129 % \textbullet & Fo \\
130 % \textbullet & Fum
131 % \end{tabular}
132 % \end{listliketab}
133 %
134 % What's the difference? The two look identical, but the first was typeset
135 % in the ordinary way, with an \texttt{itemize} environment. The second
136 % was typeset within a \texttt{tabular} environment, using the
137 % \texttt{listliketab} package. Because the second is a \texttt{tabular},
138 % it can contain additional columns on each line:
139 %
140 % \begin{listliketab}
141 % \begin{tabular}{Ll@{\hspace*{2cm}}|lR}
142 % \textbullet & Fee &
143 % We can have additional text (and rules) with each item. \\
144 % \textbullet & Fi &
145 % Try doing \emph{that} in an \texttt{itemize} environment! \\
146 % \textbullet & Fo \\
147 % \textbullet & Fum &
148 % Not so easy, is it?
149 % \end{tabular}
150 % \end{listliketab}
151 %
152 % \texttt{listliketab} works with enumerated lists, too:
153 %
154 % \storestyleof{enumerate}
155 % \begin{listliketab}
156 % \newcounter{lltenum}\setcounter{lltenum}{0}
157 % \newcommand{\nextnum}{\addtocounter{lltenum}{1}\thelltenum.}
158 % \begin{tabularx}{\linewidth}{LX@{\qquad}lR}
159 % & & \multicolumn{1}{c}{Due date} \\ \cline{3-3}
160 % \nextnum & Clean desk. & 2005/01/10 \\
161 % \nextnum & Sort ``in'' pile. & 2005/01/11 \\
162 % \nextnum & Discard random applications from ``in'' pile. & 2005/01/11 \\
163 % \nextnum & Move applications from ``in'' pile to ``out'' pile, stamping
164 % each one with a different official-looking stamp.
165 % & 2005/01/15 \\
166 % \nextnum & Write and mail meaningless memos. & 2005/01/16 \\
167 % \nextnum & Update r\'esum\'e. & 2005/01/20 \\
168 % \end{tabularx}
169 % \end{listliketab}
170 %
171 % \section{Usage}
172 %
173 % There are two steps involved in making list-like \texttt{tabulars}:
174 % First, you store a list environment's parameters. And second, you
175 % create a \texttt{tabular} using the stored parameters. The following
176 % are the commands and environments needed to perform these operations.
177 %
178 % \begin{decl}
179 % |\storestyleof| \marg{environment}
180 % \end{decl}
181 %
182 % |\storestyleof| is the easier to use of the two commands that store a list's
183 % formatting style. Merely pass this command the name of an existing list
184 % environment---generally either \texttt{itemize} or \texttt{enumerate}---as
185 % its \meta{environment} parameter. |\storestyleof| will then remember the
186 % formatting of that list environment for later use in a \texttt{tabular}.
187 %
188 % \begin{decl}
189 % |\storeliststyle|
190 % \end{decl}
191 %
192 % Sometimes, you have a list environment that takes parameters.
193 % |\storestyleof| has no mechanism for passing parameters to such an
194 % environment. In this situation, you can manually create a list of the
195 % appropriate type and call |\storeliststyle| from within that list.
196 % For example:
197 %
198 % \begin{verbatim}
199 % \begin{mylistenvironment}{something}{something else}
200 % \item[] \storeliststyle{}
201 % \end{mylistenvironment}
202 % \end{verbatim}
203 % \vspace*{-\baselineskip}
204 %
205 % Note that the above will probably leave some blank space in your document.
206 % |\storestyleof|---which uses |\storeliststyle|, incidentally---gets around
207 % that problem by building the list within a \texttt{minipage} within an
208 % \texttt{lrbox}. As you can tell, |\storestyleof| is a lot more convenient
209 % to use, when applicable.
210 %
211 % \begin{decl}
212 % |\begin{listliketab}| \\
213 % \meta{tabular} \\
214 % |\end{listliketab}|
215 % \end{decl}
216 %
217 % Once you've stored a list environment's style with |\storestyleof| or
218 % |\storeliststyle|, you're ready to typeset list-like \texttt{tablular}s.
219 % The \texttt{listliketab} environment adjusts the internal and external
220 % spacing of a \texttt{tablular} to match that of the previously given list.
221 % It also defines two new field types: |L| and |R|\@. |L| inserts spacing
222 % corresponding to the list's left margin, a right-justified parbox of the
223 % same size as the list's label field, and spacing to separate the label
224 % from the remaining fields. |R| inserts spacing corresponding to the list's
225 % right margin, and is more useful in \texttt{tabularx} environments than
226 % in ordinary \texttt{tabular}s.
227 %
228 % Speaking of which, the \meta{tabular} you put inside \texttt{listliketab}
229 % can be any environment that's compatible with the \texttt{array} package.
230 % This includes \texttt{tabular}, \texttt{tabularx}, \texttt{longtable}, and
231 % probably others, as well. Basically, \texttt{listliketab} needs to call
232 % |\newcolumntype| to define the |L| and |R| fields.
233 %
234 % The styles stored by |\storestyleof| and |\storeliststyle| are valid
235 % until the next call to one of those commands. Hence, any number of
236 % \texttt{listliketab} environments can follow a single |\storestyleof|
237 % or |\storeliststyle|.
238 %
239 % \section{Examples}
240 %
241 % Here's a simple bullet list:
242 %
243 % \begin{verbatim}
244 % \storestyleof{itemize}
245 % \begin{listliketab}
246 % \begin{tabular}{Ll}
247 % \textbullet & One \\
248 % \textbullet & Two \\
249 % \textbullet & Three \\
250 % \end{tabular}
251 % \end{listliketab}
252 % \end{verbatim}
253 % \vspace*{-\baselineskip}
254 %
255 % \noindent
256 % and its output:
257 %
258 % \storestyleof{itemize}
259 % \begin{listliketab}
260 % \begin{tabular}{Ll}
261 % \textbullet & One \\
262 % \textbullet & Two \\
263 % \textbullet & Three \\
264 % \end{tabular}
265 % \end{listliketab}
266 %
267 % \bigskip\noindent
268 % Here's an enumerated list that contains multiple columns after the label:
269 %
270 % \begin{verbatim}
271 % \storestyleof{enumerate}
272 % \begin{listliketab}
273 % \newcounter{tabenum}\setcounter{tabenum}{0}
274 % \newcommand{\nextnum}{\addtocounter{tabenum}{1}\thetabenum.}
275 % \begin{tabular}{L>{\bf}l@{~or~}>{\bf}l@{~or~}>{\bf}l}
276 % \nextnum & Red & green & blue \\
277 % \nextnum & Short & stout & tall \\
278 % \nextnum & Happy & sad & confused \\
279 % \end{tabular}
280 % \end{listliketab}
281 % \end{verbatim}
282 % \vspace*{-\baselineskip}
283 %
284 % \noindent
285 % and what it produces:
286 %
287 % \storestyleof{enumerate}
288 % \begin{listliketab}
289 % \newcounter{tabenum}\setcounter{tabenum}{0}
290 % \newcommand{\nextnum}{\addtocounter{tabenum}{1}\thetabenum.}
291 % \begin{tabular}{L>{\bf}l@{~or~}>{\bf}l@{~or~}>{\bf}l}
292 % \nextnum & Red & green & blue \\
293 % \nextnum & Short & stout & tall \\
294 % \nextnum & Happy & sad & confused \\
295 % \end{tabular}
296 % \end{listliketab}
297 %
298 % \bigskip\noindent
299 % And finally, here's an example using \texttt{tabularx}:
300 %
301 % \begin{verbatim}
302 % \storestyleof{itemize}
303 % \begin{listliketab}
304 % \begin{tabularx}{0.5\linewidth}{%
305 % LX@{\raisebox{-2pt}{\framebox(12,12){}}}R}
306 % \textbullet & Milk \\
307 % \textbullet & Flour \\
308 % \textbullet & Sugar \\
309 % \textbullet & Butter \\
310 % \textbullet & Eggs \\
311 % \end{tabularx}
312 % \end{listliketab}
313 % \end{verbatim}
314 % \vspace*{-\baselineskip}
315 %
316 % \noindent
317 % and the generated list:
318 %
319 % \storestyleof{itemize}
320 % \begin{listliketab}
321 % \begin{tabularx}{0.5\linewidth}{%
322 % LX@{\raisebox{-2pt}{\framebox(12,12){}}}R}
323 % \textbullet & Milk \\
324 % \textbullet & Flour \\
325 % \textbullet & Sugar \\
326 % \textbullet & Butter \\
327 % \textbullet & Eggs \\
328 % \end{tabularx}
329 % \end{listliketab}
330 %
331 % \StopEventually{^^A
332 % \section{Future work}
333 %
334 % The \texttt{listliketab} environment is too inflexible in terms of
335 % defining the |L| and |R| column types for the user's \texttt{tabular}
336 % environments. First, the user should be able to choose what letters
337 % to use, in case he has already assigned a meaning to |L| or |R|\@.
338 % Second, |L| always formats the label as a right-justified parbox,
339 % while there may be a case in which the user wants the label to be
340 % formatted differently.
341 %
342 % The next limitation that should be addressed in a later version of
343 % \texttt{listliketab} is that the user must manually insert
344 % |\textbullet|s (when mimicking \texttt{itemize}) or numbers (when
345 % mimicking \texttt{enumerate}) into the ``label'' field of his
346 % \texttt{tabular}. It would be nice if the \texttt{listliketab}
347 % environment could do this automatically.
348 %
349 % Finally, there is no support for nested lists. Those would probably
350 % be tricky to mimic properly in a \texttt{tabular}, but could
351 % occasionally be useful to have.
352 % }
353 %
354 %
355 % \section{Implementation}
356 %
357 % This section contains the complete source code for \texttt{listliketab}.
358 % Most users will not get much out of it, but it should be of use to
359 % those who need more precise documentation and those who want to extend
360 % the \texttt{listliketab} package.
361 %
362 % \subsection{Required packages}
363 % \label{sec:packages}
364 %
365 % \texttt{listliketab} requires that the following packages be loaded:
366 %
367 % \begin{macrocode}
368 \RequirePackage{calc}
369 \RequirePackage{array}
370 % \end{macrocode}
371 %
372 % \subsection{List parameter storage}
373 %
374 % Once we're inside a list environment, we'll need some (global) locations
375 % in which to store the local values of various list parameters.
376 %
377 % \begin{macro}{\llt@labelwidth}
378 % \begin{macro}{\llt@labelsep}
379 % \begin{macro}{\llt@topsep}
380 % \begin{macro}{\llt@rightmargin}
381 % These are the global equivalents of |\labelwidth|, |\labelsep|, |\topsep|,
382 % and |\rightmargin|, respectively. They're stored by the |\storeliststyle|
383 % command from within a list environment.
384 % \begin{macrocode}
385 \newlength{\llt@labelwidth}
386 \newlength{\llt@labelsep}
387 \newlength{\llt@topsep}
388 \newlength{\llt@rightmargin}
389 % \end{macrocode}
390 % \end{macro}
391 % \end{macro}
392 % \end{macro}
393 % \end{macro}
394 %
395 % \begin{macro}{\llt@tab@indent}
396 % |\llt@tab@indent| is the indentation of the entire list. It
397 % corresponds to the space to the left of the label or, more precisely,
398 % |\leftmargin|-|\labelsep|-|\labelwidth|.
399 % \begin{macrocode}
400 \newlength{\llt@tab@indent}
401 % \end{macrocode}
402 % \end{macro}
403 %
404 % \begin{macro}{\llt@bot@sep}
405 % |\llt@bot@sep| is the amount of space to add at the end of a list.
406 % It is set to |\itemsep|+|\parsep| by |\storeliststyle|.
407 % (I'm not actually positive this is the right amount of space to add,
408 % but it looks okay to me.)
409 % \begin{macrocode}
410 \newlength{\llt@bot@sep}
411 % \end{macrocode}
412 % \end{macro}
413 %
414 % \subsection{Other variables}
415 %
416 % \begin{macro}{\llt@arraystretch}
417 % \begin{macro}{\llt@arraystretch@clean}
418 % |\llt@arraystretch| is the value we need to assign to |\arraystretch| to
419 % make \texttt{tabular} spacing mimic the spacing used in the given list.
420 % |\llt@arraystretch@clean| is the same as |\llt@arraystretch|, except it
421 % is ordinary text instead of a length and does not end in ``|pt|''.
422 % \begin{macrocode}
423 \newlength{\llt@arraystretch}
424 \def\llt@arraystretch@clean{}
425 % \end{macrocode}
426 % \end{macro}
427 % \end{macro}
428 %
429 % \begin{macro}{\llt@list@box}
430 % This box is used by |\storestyleof| to hold a throwaway list.
431 % \begin{macrocode}
432 \newsavebox{\llt@list@box}
433 % \end{macrocode}
434 % \end{macro}
435 %
436 % \subsection{Author commands and environments}
437 %
438 % \begin{macro}{\storeliststyle}
439 % When |\storeliststyle| is invoked within a list environment, it does
440 % two things. First, it copies the current settings of various list
441 % parameters into global variables, so they can be used outside the list.
442 % And second, it calculates a value for |\arraystretch| to match the list's
443 % inter-item spacing.
444 % \begin{macrocode}
445 \DeclareRobustCommand{\storeliststyle}{
446 % \end{macrocode}
447 % Storing list parameters is fairly straightforward.
448 % \begin{macrocode}
449 \setlength{\llt@tab@indent}{\leftmargin-\labelsep-\labelwidth}
450 \global\llt@tab@indent=\llt@tab@indent
451 \setlength{\llt@bot@sep}{\itemsep+\parsep}
452 \global\llt@bot@sep=\llt@bot@sep
453
454 \global\llt@labelwidth=\labelwidth
455 \global\llt@labelsep=\labelsep
456 \global\llt@rightmargin=\rightmargin
457 \global\llt@topsep=\topsep
458 % \end{macrocode}
459 % Determining an appropriate value for |\arraystretch| takes a bit of
460 % explanation. Rows of a \texttt{tabular} environment normally have the
461 % same height and depth as a strut. Entries in a list are also one strut
462 % high/deep, but are separated by |\itemsep|+|\parsep|'s worth of glue.
463 % Hence, to get the new value of |\arraystretch|, we have to take:
464 % \newcommand{\strutfunc}[1]{\text{#1}(\text{strut})}
465 % \begin{eqnarray*}
466 % |\arraystretch| & = & \frac{\text{total space between baselines in a
467 % list}}%
468 % {\text{total space between baselines in a
469 % \texttt{tabular}}} \\[2ex]
470 % & = & \frac{\text{item height + item depth +
471 % inter-item spacing}}%
472 % {\text{row height + row depth}} \\[2ex]
473 % & = & \frac{\strutfunc{height} + \strutfunc{depth} +
474 % \texttt{\bslash itemsep} +
475 % \texttt{\bslash parsep}}%
476 % {\strutfunc{height} + \strutfunc{depth}}
477 % \end{eqnarray*}
478 % \begin{macrocode}
479 \setlength{\llt@arraystretch}{%
480 1.0pt*\ratio{\ht\strutbox+\dp\strutbox+\itemsep+\parsep}
481 {\ht\strutbox+\dp\strutbox}}
482 % \end{macrocode}
483 % |\arraystretch| takes a unitless fixed-point number as an argument.
484 % Unfortunately, \TeX\ doesn't support such a thing. So we use
485 % \LaTeXe's |\strip@pt| macro to convert from a length to the equivalent
486 % text, dropping the units in the process.
487 % ^^A
488 % \makeatletter
489 % \changes{v1.0a}{2005/01/09}{Modified to use \string\LaTeXe's
490 % \string\texttt\string{\string\string\string\strip@pt\string} macro
491 % instead of defining the equivalent ourselves.}
492 % \makeatother
493 % \begin{macrocode}
494 \xdef\llt@arraystretch@clean{\strip@pt\llt@arraystretch}%
495 }
496 % \end{macrocode}
497 % \end{macro}
498 %
499 % \begin{macro}{\storestyleof}
500 % The problem with |\storeliststyle| is that it can be called only from
501 % within a list. What if you don't have a list to use as a template? Well,
502 % you have to make one. Unfortunately, that list then winds up in your
503 % document. |\storestyleof| to the rescue! This convenience function creates
504 % a list of type |#1| (probably either \texttt{itemize} or \texttt{enumerate})
505 % containing a call to |\storeliststyle|, but then discards the list
506 % environment, so you never see it. (More accurately, |\storelist|
507 % constructs the list within an \texttt{lrbox} that it never typesets.)
508 % \begin{macrocode}
509 \DeclareRobustCommand{\storestyleof}[1]{%
510 \begin{lrbox}{\llt@list@box}
511 \noindent
512 \begin{minipage}{\linewidth}
513 \begin{#1}
514 \item[] \storeliststyle{}
515 \end{#1}
516 \end{minipage}
517 \end{lrbox}\ignorespacesafterend
518 }
519 % \end{macrocode}
520 % \end{macro}
521 %
522 % \begin{environment}{listliketab}
523 % The \texttt{listliketab} environment defines a new \texttt{tabular}
524 % column type, |L|, which corresponds to the list's indentation, the label
525 % (a right justified parbox), and the separation between the label and the
526 % list body. |L| should be the first field in the user's \texttt{tabular}
527 % environment. Similarly, \text{listliketab} defines |R|, which is the
528 % spacing on the right side of the list. |R| is useful when the user is
529 % using \texttt{tabularx} instead of \texttt{tabular}. In that case, a good
530 % \texttt{tabularx} format string is ``|LXR|'', possibly with other fields
531 % between the |X| and the |R|.
532 %
533 % \texttt{listliketab} also stretches the array appropriately and suppresses
534 % paragraph indentation. (The |L| field will ensure the \texttt{tabular} is
535 % properly indented.)
536 % \begin{macrocode}
537 \newenvironment{listliketab}{%
538 \newcolumntype{L}{%
539 @{\hspace*{\llt@tab@indent}}%
540 >{\hfill}p{\llt@labelwidth}%
541 @{\hspace*{\llt@labelsep}}}%
542 \newcolumntype{R}{%
543 @{\hspace*{\llt@rightmargin}}}%
544 \renewcommand{\arraystretch}{\llt@arraystretch@clean}%
545 \vspace{\llt@topsep}%
546 \noindent\ignorespaces
547 }{%
548 \vspace{\llt@bot@sep}%
549 }
550 % \end{macrocode}
551 % \end{environment}
552 %
553 %
554 % \Finale
555 \endinput