summaryrefslogtreecommitdiff
path: root/macros/latex/contrib/collcell/collcell.dtx
blob: 6a0846c3219c52f6319f7cf2afaeddebb159daa0 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
% \iffalse meta-comment
%
% Copyright (C) 2009-2011 by Martin Scharrer <martin@scharrer-online.de>
% ----------------------------------------------------------------------
%
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%
%   http://www.latex-project.org/lppl.txt
%
% and version 1.3c or later is part of all distributions of LaTeX
% version 2008/05/04 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Martin Scharrer.
%
% This work consists of the files collcell.dtx, collcell.ins
% and the derived file collcell.sty.
%
% \fi
%%^^A $Id: collcell.dtx 2188 2011-02-27 14:22:06Z martin $
%
% \iffalse
%<package>\ProvidesPackage{collcell}
%<*driver>
\ProvidesFile{collcell.dtx}
%</driver>
  [2011/02/27 v0.5 Collect the content of a tabular cell]
%<*driver>
\documentclass{ydoc}
\GetFileInfo{\jobname.dtx}
\usepackage[robustcr]{collcell}[\filedate]
\usepackage{tikz-timing}
\lstset{language=[latex]tex,basicstyle=\ttfamily}

\EnableCrossrefs
%\CodelineIndex
\RecordChanges
%\OnlyDescription
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintChanges
  %\newpage\PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{334}
%
% \CharacterTable
%  {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
%   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
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v0.0}{2009/08/13}{Created package}
% \changes{v0.1}{2011/02/04}{First released version}
% \changes{v0.1a}{2011/02/05}{Fixed unwanted spaces. Fixed misspelled macros in example.}
% \changes{v0.5}{2011/02/27}{Fixed several bugs and limitations. (Complete list to be inserted)}
%
%
% \DoNotIndex{\newcommand,\newenvironment,\def,\edef,\xdef,\DeclareRobustCommand}
%
% \GetFileInfo{\jobname.dtx}
% \ifpdf
% \hypersetup{%
%   pdfauthor   = {Martin Scharrer <martin@scharrer-online.de>},
%   pdftitle    = {The collcell package},
%   pdfsubject  = {Documentation of LaTeX package collcell},
%   pdfkeywords = {collcell, LaTeX, TeX}
% }%
% \fi
% \clearpage
% \null
% \vspace*{-2em}
% \begin{center}
%   {\LARGE The \textsf{collcell} Package\\[\medskipamount]}
%   {\large Martin Scharrer \\[\medskipamount]\normalsize 
%   \url{martin@scharrer-online.de}\\[.8ex]
%   \url{http://www.ctan.org/pkg/collcell/}\\[\medskipamount]}
%   {\large Version \fileversion\ -- \filedate}\\
% \end{center}
% \vspace{1.2em}%
%
% \begin{abstract}
% This package provides macros which collect the cell content of a tabular
% and provide it to a macro as argument. It was inspired by the |\collect@body|
% macro defined by the \pkg{amsmath} or the \pkg{environ} package, which can be used
% to collect the body of an environment. Special care is taken to remove all aligning
% macros inserted by tabular from the cell content. The macros also work in the last
% column of a tabular. They do not support verbatim material inside the cells, except of a
% special almost-verbatim version of \verb+\verb+.
%
% This package is relatively new and might still not work in all possible situations which can arise
% in a tabular. The implementation might change in future versions. Please do not hesitate to contact the
% author about any issue and suggestions.
% \end{abstract}
%
% \section{Usage}
% This package provides the macros \Macro\collectcell and \Macro\endcollectcell
% which are supposed to be used with the |>{ }| and |<{ }| tabular column declarations of
% the \pkg{array} package. This can be done either in the argument of tabular or using
% \Macro\newcolumntype.
%
% The following code defines a `|E|' column which passes the contents of its cell
% to \Macro\usermacro as an argument. The macro can the process the content as usual.
%
% \par\bigskip
% \noindent
% |% Preamble:|\\
% |\usepackage{array}|\\
% |\usepackage{collcell}|\\
% |% Preamble or document:|\\
% |\newcolumntype{E}{>{\collectcell\usermacro}c<{\endcollectcell}}|\\
% ||\\
% |% Document:|\\
% |\begin{tabular}{lE}|\\
% |   A & Example \\ % Same as \usermacro{Example} |\\ 
% |   B & Text    \\ % Same as \usermacro{Text}    |\\
% |\end{tabular}|\\
% \par\medskip
%
% For example \Macro\usermacro could be \Macro\fbox and wrap the cell content in a frame box.
% More complicated macros are also supported as long they take one argument. This package
% was originally programmed to be used with the \Macro\tikztiming macro of the \pkg{tikz-timing} package.
% This macro takes some complex user input and draws a timing diagram from it
%
% Note that if such a cell contains a tabular environment by itself, the environment must be wrapped
% in braces `|{ }|' to ensure proper operation.
%
% \subsection{Options}
% The following options are supported:
% \begin{description}
%   \item[verb] \null
%   \item[noverb] (Default |noverb|) Enables or disables the definition of a special almost-verbatim version of \verb+\verb+.
%     At the moment the one defined by the \pkg{tabularx} package is used, which is therefore loaded when this feature is enabled.
%     Future versions of \pkg{collcell} might provide this macro in a different way, so the visual result might be different.
%     The \pkg{tabularx} should be loaded explicitly if it is used.
%     This version of \verb+\verb+ will read the content first normally, i.e. non-verbatim, and then print the included tokens in a verbatim format.
%     The content must include a balanced number of |{ }| and must not be end with |\|. Macros inside the content will be followed by a space.
%     See the manual of \pkg{tabularx} (page 8 in the version from 1999/01/07) for a more detailed description.
%   \item[robustcr]
%   \item[norobustcr] (Default |robustcr|) This options enable or disable the redefinition of |\\| to a robust version, i.e. this macro
%     will be prefixed with eTeX's |\protected| to ensure that it isn't expanded by the underlying |\halign|. If this feature disabled the
%     last cell of a tabular must not be empty or only hold empty macros (like |\empty|).
% \end{description}
%
% \subsection{Limitations}
%
% \DescribeMacro\ccunskip
% The macro |\unskip| should not be included inside the cell directly, but only inside a |{ }| group or a macro.
% Otherwise it will be taken as part of the internal cell code and ignored. Leading spaces will however be removed.
% This macro can be used as a replacement of |\unskip| inside the cells.
%
% \DescribeMacro\cci
% The content of every cell is expanded by TeX itself until the first non-expandable token (macro, character, \ldots) is found. This happens to check if a |\noalign| follows
% with e.g.\ is used inside |\hrule| and other rule drawing macros. There is nothing what \pkg{collcell} could do about this.
% If this expansion is unwanted the non-expandable token \Macro\cci should be placed at the beginning of the cell. This macro will be ignored (discarded) by \pkg{collcell} and
% will not be provided to the user macro (|cci| = collect cell; ignore).
%
% \section{Tests and Examples}
%
% \begin{example}
% \caption{Framebox, texttiming, expanded tokens, sub-tabular}
% \begin{examplecode}
%     \makeatletter
%     \newcommand*\Meaning[1]
%        {\def\CODE{#1}\texttt{\expandafter\strip@prefix\meaning\CODE}}%
%     \newcolumntype{F}{>{\collectcell\fbox}l<{\endcollectcell}}%
%     \newcolumntype{M}{>{\collectcell\Meaning}l<{\endcollectcell}}%
%     \newcolumntype{T}{>{\collectcell\texttiming}l<{\endcollectcell}}%
%     \begin{tabular}{@{}F@{}|@{}M@{}|@{}T@{}}
%        A & B                       & HLDZ 2{HZLZ} \\
%        A & \empty\relax Z5D{TEST}Z & Z5D{TEST}Z  \\
%        A & \cci\empty Z5D{TEST}Z & Z5D{TEST}Z  \\
%        {\begin{tabular}{cFc} a & b & c \end{tabular}} &
%         \relax\begin{quote}AA\end{quote}   & $5+5${C} \\
%        A & B   \ccunskip B                 & 3{ttz} \\
%     \end{tabular}%
% \end{examplecode}
% \end{example}
%
% \makeatletter
% \newcommand*\Meaning[1]{\def\CODE{#1}\texttt{\expandafter\strip@prefix\meaning\CODE}}%
% \newcolumntype{M}{>{\collectcell\Meaning}l<{\endcollectcell}}%
% \newcolumntype{F}{>{\collectcell\fbox}l<{\endcollectcell}}%
% \makeatother
%
% \begin{example}
% \caption{Multicolumn, expanded row macro}
% \begin{examplecode}
%     \def\abc{ \empty A & \empty B & \empty C }
%     \begin{tabular}{MMM}
%       \multicolumn{2}{M}{\empty Multi} & \empty single \\
%       \abc \\
%     \end{tabular}
% \end{examplecode}
% \end{example}
%
% \begin{example}
% \caption{Empty cells, missing `\texttt{\textbackslash\textbackslash}' at end}
% \begin{examplecode}
%     \begin{tabular}{|F|F|F|}
%        \\
%        A & \\
%        A & B \\
%        A & B & \\
%        A & B & C \\
%        & \\
%        & B \\
%        & B & \\
%        & B & C \\
%        & & \\
%        & & C \\
%        A & B & C
%     \end{tabular}
% \end{examplecode}
% \end{example}
%
% \StopEventually{}
% \clearpage
% \iffalse
%<*package>
% \fi
% \section{Implementation}
%
%    \begin{macrocode}
\RequirePackage{array}
\def\collcell@beforeuser{\ignorespaces}
\def\collcell@afteruser{\unskip}

\newif\if@collcell@verb
\newif\if@collcell@robustcr
\@collcell@robustcrtrue
%    \end{macrocode}
%
% \subsection{Options}
%    \begin{macrocode}
\DeclareOption{verb}{\@collcell@verbtrue}
\DeclareOption{noverb}{\@collcell@verbfalse}
\DeclareOption{robustcr}{\@collcell@robustcrtrue}%
\DeclareOption{norobustcr}{\@collcell@robustcrfalse}%
\ProcessOptions\relax
\if@collcell@verb
  \RequirePackage{tabularx}
  \def\collcell@beforeuser{%
    \let\collcell@savedverb\verb
    \let\verb\TX@verb
    \let\TX@vwarn\collcell@vwarn
    \ignorespaces
  }%
  \def\collcell@afteruser{\unskip\let\verb\collcell@savedverb}%
  \def\collcell@vwarn{%
    \PackageWarning{collcell}{\noexpand\verb may be unreliable inside a collected cell}%
  }%
\fi
\if@collcell@robustcr
  \RequirePackage{etoolbox}
  \robustify\@arraycr
\fi
%    \end{macrocode}
%
% \subsection{Collect cell content}
%    \begin{macrocode}
\let\collect@cell@toks\@temptokena
\newcount\collect@cell@count
%    \end{macrocode}
%
% \begin{macro}{\collectcell}[2]{user macro(s)}{ignored tokens, possible empty}
%    \begin{macrocode}
\newenvironment{collectcell}{}{}
\def\collectcell#1#2\ignorespaces{%
  \begingroup
  \collect@cell@count\z@
  \collect@cell@toks{}%
  \let\collect@cell@spaces\empty
  \def\collect@cell@end{%
    \expandafter\endgroup
    \expandafter\collcell@beforeuser
    \expandafter\ccell@swap\expandafter{\the\collect@cell@toks}{#1}%
    \collcell@afteruser
  }%
  \collect@cell@look#2%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\ccell@swap}
% Swaps the two arguments. The second one (user macro(s)) is added without braces.
%    \begin{macrocode}
\def\ccell@swap#1#2{#2{#1}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\endcollectcell}
% Holds unique signature which will expand to nothing.
%    \begin{macrocode}
\def\endcollectcell{\@gobble{endcollectcell}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\collect@cell@look}
% Looks ahead to the next token and call the next macro to handle it.
%    \begin{macrocode}
\def\collect@cell@look{%
  \futurelet\collect@cell@lettoken\collect@cell@look@
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collect@cell@eatspace}
% Eats a following space and call the 'look' macro again.
%    \begin{macrocode}
\@firstofone{\def\collect@cell@eatspace} {\collect@cell@look}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collect@cell@look@}
% Handles special tokens which should not be read as argument.
% All other are handled by \Macro\collect@cell@arg.
%    \begin{macrocode}
\def\collect@cell@look@{%
  \cc@case
    \@sptoken{%
      \edef\collect@cell@spaces{\collect@cell@spaces\space}%
      \collect@cell@eatspace
    }%
    \bgroup{\collect@cell@group}%
    \default{\collect@cell@arg}%
  \endcc@case
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collect@cell@group}
% Tests if the previous discovered |begin-group character {| token was a |\bgroup| or a |{|.
% In the first case the command sequence is simply added but in the second case the surrounding 
% braces must be added again.
% The use of |\unexpanded| allows |#| in the cells, e.g. for in-cell macro definitions.
%    \begin{macrocode}
\def\collect@cell@group#1{%
  \begingroup
  \edef\@tempa{\unexpanded{#1}}%
  \def\@tempb{\bgroup}%
  \ifx\@tempa\@tempb
    \endgroup
    \collect@cell@addarg{#1}%
  \else
    \endgroup
    \collect@cell@addarg{{#1}}%
  \fi
  \collect@cell@look
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collect@cell@addarg}
% Adds the given argument to the token list.
%    \begin{macrocode}
\def\collect@cell@addarg#1{%
  \expandafter\expandafter\expandafter\collect@cell@toks
  \expandafter\expandafter\expandafter
  {\expandafter\the\expandafter\collect@cell@toks\collect@cell@spaces#1}%
  \let\collect@cell@spaces\empty
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collect@cell@addcc}
% This macro is called when another |\collectcell| is found in the preamble
% (at the moment also inside the cell). The argument of it is placed into the
% token register and all following tokens are placed in an own token list which content
% is then added with surrounding braces in the outer token list once the |\endcollectcell|
% is found. TeX scoping mechanism is used for this so only one token register is required.
%    \begin{macrocode}
\def\collect@cell@addcc#1{%
  \collect@cell@addarg{#1}%
  \begingroup
  \collect@cell@toks{}%
  \collect@cell@look
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collect@cell@checkcsname}
% For support of |\end{tabularx}| without trailing |\\|.
%    \begin{macrocode}
\def\collect@cell@checkcsname#1\endcsname{%
  \begingroup
  \expandafter\ccell@swap\expandafter
    {\expandafter,\@currenvir,endtabular,endtabular*,array,tabularx,}%
    {\in@{,#1,}}%
  \ifin@
    \endgroup
    \expandafter\@firstoftwo
  \else
    \endgroup
    \expandafter\@secondoftwo
  \fi
    {\collect@cell@cr\\\csname#1\endcsname}%
    {\collect@cell@addarg{\csname#1\endcsname}\collect@cell@look}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collect@cell@checkend}[1]{The argument of an \string\end~macro}
% Reads the argument of |\end| and checks if it is identical to the current environment (|tabular|, |array|, |tabularx|, ...).
% If so the collecting of token is ended, otherwise the |\end| and its argument are added to the 
%    \begin{macrocode}
\def\collect@cell@checkend#1{%
  \begingroup
  \def\@tempa{#1}%
  \ifx\@tempa\@currenvir
    \endgroup
    \expandafter\@firstoftwo
  \else
    \endgroup
    \expandafter\@secondoftwo
  \fi
    {\collect@cell@cr\\\end{#1}}%
    {\collect@cell@addarg{\end{#1}}\collect@cell@look}%
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cc@iftoken}
% Compares the |\collect@cell@lettoken| with the token given as argument.
%    \begin{macrocode}
\def\cc@iftoken#1{%
  \ifx#1\collect@cell@lettoken
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cc@case}
% Case statement over |\collect@cell@lettoken|.
%    \begin{macrocode}
\def\cc@case{%
  \begingroup
  \let\default= \collect@cell@lettoken
  \cc@@case
}
\def\cc@@case#1{%
  \ifx#1\collect@cell@lettoken
    \expandafter\cc@@truecase
  \else
    \expandafter\cc@@falsecase
  \fi
}
\def\cc@@truecase#1#2\endcc@case{\endgroup#1}
\def\cc@@falsecase#1{\cc@@case}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collcell@unskip}
% Wrapper around |\unskip| to protect it from the eyes of the token scanner.
% It is protected to avoid trouble if the user wrongly uses it at the beginning of the cell.
% The macro is first defined using |\newcommand| to warn the user about name collisions.
%    \begin{macrocode}
\newcommand*\ccunskip{}
\protected\def\ccunskip{\unskip}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\cci}
% Protected empty macro usable to stop the expansion of tokens at the beginning of the
% cell. It is ignored (gobbled) by the token scanner.
% The macro is first defined using |\newcommand| to warn the user about name collisions.
%    \begin{macrocode}
\newcommand*\cci{}
\protected\def\cci{}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collect@cell@cr}
% Redefines the table line/row end macro |\cr| so that 
% token collection is restarted after the real |\cr| is expanded
% and the end material defined by `<{}` is inserted.
%
% This redefinition must be done around some \emph{dirty tricks}
% otherwise the |\cr| will be wrongly taken as end of the
% row.
%
% Because the redefinition is done just at the end of a cell inside the group
% opened by |collcell| it will only be locally.
%    \begin{macrocode}
\def\collect@cell@cr{%
  \iffalse{\fi
  \let\collcell@realcr\cr
  \def\cr{%
    \expandafter
    \collect@cell@look
    \collcell@realcr
  }%
  \iffalse}\fi
}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\collect@cell@arg}
% Handles the arguments.
% The first token of the argument is still in the |lettoken| macro which is compared
% against a list of possible end tokens.
% Then either the cell end is handled or the argument is added to the token register
% and the rest of the cell is processed.
%    \begin{macrocode}
\def\collect@cell@arg#1{%
  \cc@case
    \\{\collect@cell@cr#1}%
    \end{\collect@cell@checkend}%
    \csname{\collect@cell@checkcsname}%
    \unskip{%
      \let\collect@cell@spaces\empty
      %\collect@cell@addarg{#1}% do not include the \unskip
      \collect@cell@look%
    }%
    \@sharp{%
      \expandafter\collect@cell@addarg\expandafter{#1}%
      \collect@cell@look
    }%
    \collectcell{%
      \advance\collect@cell@count by \@ne
      \collect@cell@addcc%
    }%
    \endcollectcell{%
      \ifnum\collect@cell@count=\z@
        \expandafter\collect@cell@end
      \else
        \expandafter\endgroup
        \expandafter\collect@cell@addarg\expandafter
        {\expandafter{\the\collect@cell@toks}}%
        \advance\collect@cell@count by \m@ne%
        \expandafter\collect@cell@look
      \fi
    }%
    \cci{%
      \collect@cell@look
    }%
    \default{%
      \expandafter\ccell@swap\expandafter
        {\csname endtabular*\endcsname\endtabular\endarray}{\in@{#1}}%
      \ifin@
          \expandafter\@firstoftwo
      \else
          \expandafter\@secondoftwo
      \fi
      {\collect@cell@cr\\#1}%
      {%
        \collect@cell@addarg{#1}%
        \collect@cell@look
      }%
    }%
 \endcc@case
}
%    \end{macrocode}
% \end{macro}
%
%
% \Finale
% \iffalse
%</package>
% \fi