summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/diagbox/diagbox.dtx
blob: a061992b3d73fb6519740b5eeb0e6a44db6e4c73 (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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
% \iffalse meta-comment
%
% Copyright (C) 2011 by Leo Liu <leoliu.pku@gmail.com>
% ---------------------------------------------------------------------------
% This work may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3
% 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.3 or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This work has the LPPL maintenance status `maintained'.
%
% The Current Maintainer of this work is Leo Liu.
%
% This work consists of the files diagbox.dtx and diagbox.ins
% and the derived filebase diagbox.sty.
%
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{diagbox.dtx}
%</driver>
%<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
%<package>\ProvidesPackage{diagbox}
%<*package>
    [2011/11/23 v2.0 Making table heads with diagonal lines]
%</package>
%
%<*driver>
\documentclass[letterpaper]{ltxdoc}
\AtBeginDocument{
  \DeleteShortVerb{\|}
  \MakeShortVerb{\"}}
\usepackage{diagbox}
\usepackage[left=1.7in,top=1in,bottom=1in]{geometry}
\usepackage{fvrb-ex}
\usepackage[UTF8]{ctex}
\makeatletter
\renewcommand\glossary@prologue{%
  \pdfbookmark[1]{版本历史}{history}
  \section*{版本历史}
  \markboth{版本历史}{版本历史}}
\renewcommand\index@prologue{%
  \pdfbookmark[1]{Index / 代码索引}{index}
  \section*{Index / 代码索引}
  \markboth{Index / 代码索引}{Index / 代码索引}
  斜体的数字表示对应项说明所在的页码。下划线的数字表示定义所在的代码行号;而直
  立体的数字表示对应项使用时所在的行号。}
\makeatother
\usepackage[pdfstartview=FitH,bookmarksnumbered,colorlinks]{hyperref}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\begin{document}
  \DocInput{diagbox.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{521}
%
% \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{v1.0}{2011/11/21}{初始版本。}
%
% \DoNotIndex{\newcommand, \newenvironment, \@nameuse, \@pkgextension,
% \@reserveda, \@tfor, \begin, \begingroup, \box, \csname, \def, \define@key,
% \dimexpr, \do, \dp, \edef, \else, \end, \endcsname, \endgroup, \endinput,
% \expandafter, \fi, \hbox, \ht, \ifcsname, \ifdim, \ifx, \Line, \makebox,
% \newbox, \newdimen, \p@, \PackageError, \pkg, \put, \relax, \RequirePackage,
% \setbox, \setkeys, \setlength, \strip@pt, \unexpanded, \unitlength,
% \vcenter, \wd, \xdef, \z@, \@ifnextchar, \bgroup, \FPadd, \FPmul, \FPneg,
% \FPsub, \FPupn, \let, \unless, \hspace}
%
% \providecommand*{\pkg}{\textsf}
% \GetFileInfo{diagbox.dtx}
% \title{\hypertarget{English}{\pkg{diagbox} Package (\fileversion)}
%   \makebox[0pt][l]{\hspace{3cm}\large
%     \hyperlink{Chinese}{$\Rightarrow$ \textsf{中文版}}}\\
%    Making Table Heads with Diagonal Lines}
% \author{Leo Liu \\ \path{leoliu.pku@gmail.com}}
% \date{\filedate}
%
% \maketitle
%
% \section{Introduction}
%
% \pkg{diagbox} is a replacement of old \pkg{slashbox} package\footnote{By
% Koichi Yasuoka and Toru Sato. Available on
% \url{CTAN://macros/latex/contrib/slashbox/slashbox.sty}}. I write this
% package simply because that \pkg{slashbox} is not available in \TeX\ Live
% for licening problems. \pkg{slashbox} has no explicit license information
% available, but \pkg{diagbox} is under LPPL.
%
% \pkg{diagbox} is a modern alternative of \pkg{slashbox}. I changed the
% user interface to use a key-value syntax, get rid of some restrictions of
% \pkg{slashbox}, use \pkg{pict2e} to draw diagonal lines. Especially, this
% package also provides ability to make a box with two diagonal lines in it.
% All these can be obtained by a "\diagbox" command.
%
% As a replacement of \pkg{slashbox}, \pkg{diagbox} package also provides
% compatible macros of \pkg{slashbox}, but the result is a little different.
%
% To use \pkg{diagbox}, \eTeX{} is needed. And \pkg{diagbox} requires
% \pkg{pict2e}, \pkg{keyval} and \pkg{fp} packages.
%
%
% \section{Usage}
%
% \subsection{Basic usage}
%
% To load the package, just put this in the preamble:
% \begin{Verbatim}
% \usepackage{diagbox}
% \end{Verbatim}
%
% \DescribeMacro{\diagbox}
% "\diagbox" is the main command. It can take two arguments, to produce a box
% with a diagonal line from north west to south east.
%
% For example:\\
% \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
% \begin{tabular}{|l|ccc|}
% \hline
% \diagbox{Time}{Day} & Mon & Tue & Wed \\
% \hline
% Morning   & used & used &      \\
% Afternoon &      & used & used \\
% \hline
% \end{tabular}
% \end{SideBySideExample}
% \medskip
%
% "\diagbox" can also take three arguments, to draw a table head with two
% diagbox lines. For example,
% \begin{Example}[frame=single,numbers=left]
% \begin{tabular}{|l|ccc|}
% \hline
% \diagbox{Time}{Room}{Day} & Mon & Tue & Wed \\
% \hline
% Morning   & used & used &      \\
% Afternoon &      & used & used \\
% \hline
% \end{tabular}
% \end{Example}
%
% \subsection{More options}
%
% "\diagbox" can take a key-value list as an optional argument to specify the
% width and height of the box, the direction of the diagonal line, and the
% trimming margins:
% \begin{description}
% \item[width] Specify the width of the box explicitly. If it is omitted,
% package will calculate a width automatically.
% 
% \item[height] Specify the height of the box explicitly. If it is omitted,
% package will calculate a height automatically.
% 
% \item[dir] Specify the direction of the diagonal line. The value can be
% "NW", "NE", "SW" and "SE". Default value is "NW". The meaning of the values
% see below.
% \begin{itemize}
% \item "\diagbox[dir="\meta{direction}"]{A}{B}" in a table looks:\\
% \begin{tabular}{ll@{\qquad}ll}
% "NW" or "SE":&
% \begin{tabular}{|c|} \hline \diagbox[dir=SE]{A}{B} \\\hline \end{tabular} &
% "SW" or "NE":&
% \begin{tabular}{|c|} \hline \diagbox[dir=NE]{A}{B} \\\hline \end{tabular}
% \end{tabular}
% \item "\diagbox[dir="\meta{direction}"]{A}{M}{B}" in a table looks:\\
% \begin{tabular}{*4{ll@{\qquad}}}
% "NW"&
% \begin{tabular}{|c|} \hline \diagbox[dir=NW]{A}{M}{B} \\\hline \end{tabular}&
% "NE"&
% \begin{tabular}{|c|} \hline \diagbox[dir=NE]{A}{M}{B} \\\hline \end{tabular}&
% "SW"&
% \begin{tabular}{|c|} \hline \diagbox[dir=SW]{A}{M}{B} \\\hline \end{tabular}&
% "SE"&
% \begin{tabular}{|c|} \hline \diagbox[dir=SE]{A}{M}{B} \\\hline \end{tabular}
% \end{tabular}
% \end{itemize}
%
% \item[trim] Specify the margin to be trimmed. The value can be "l", "r", and
% "lr", "rl". This helps the slash line exceeds the boundary when "@{}" column
% specifier is used.
% \end{description}
%
% Here is a more complex example to show the usage of the options:
% \begin{Example}[frame=single,numbers=left]
% \begin{tabular}{|@{}l|c|c|r@{}|}
% \hline
% \diagbox[width=5em,trim=l]{Time}{Day} & Mon & Tue & Wed\\
% \hline
% Morning   & used & used & used\\
% \hline
% Afternoon &      & used & \diagbox[dir=SW,height=2em,trim=r]{A}{B} \\
% \hline
% \end{tabular}
% \end{Example}
%
% \bigskip
% What's more, you can use "\\" to break lines in "\diagbox". Manual setting
% of the height of the head may be needed. For example,
% \begin{Example}[frame=single,numbers=left]
% \begin{tabular}{|c|}
% \hline
% \diagbox[height=45pt]{line\\heads}{column\\heads} \\
% \hline
% \end{tabular}
% \end{Example}
%
% \subsection{Compatibility with \pkg{slashbox}}
%
% \pkg{diagbox} package emulates \pkg{slashbox} and also prevents
% \pkg{slashbox} to be loaded.
%
% \pkg{diagbox} package provides "\slashbox" and "\backslashbox" which syntax
% similar to \pkg{slashbox} package. However, the results of the two packages
% are a little different. These two commands are for compatibility only, it is
% better to use "\diagbox" instead for new documents.
%
% \DescribeMacro{\backslashbox}
% "\backslashbox" works as "\diagbox", but it takes two optional arguments to
% specify the "width" and "trim" options.
%
% \DescribeMacro{\slashbox}
% "\slashbox" works as "\diagbox[dir=SW]", and takes two optional arguments to
% specify the "width" and "trim" options.
%
% For example,\\
% \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
% \begin{tabular}{|c|c|c|} \hline
% \backslashbox[2cm]{num}{alpha}
%   & A  & B  \\\hline
% 1 & A1 & B1 \\\hline
% 2 & A2 & B2 \\\hline
% \end{tabular}
% \end{SideBySideExample}
%
%
% \section{Known issues and TODO}
%
% Known issues:
% \begin{itemize}
% \item The result of "\slashbox" and "\backslashbox" is different with
% \pkg{slashbox} package. The algorithms to calculate the width and height are
% different; and the results of the second optional argument of "\slashbox"
% (i.e. "trim" key in "\diagbox") in the two packages are differernt.
%
% This is not a bug. Usually the width calculated by \pkg{diagbox} is more
% safe than \pkg{slashbox}.
%
% \item
% The cell with "\diagbox" should be the widest one of the column. Otherwise
% the slash line cannot exceeds the boundary. For example,\\[1ex]
% \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
% \begin{tabular}{|c|}  \hline
%   \diagbox{A}{B}    \\\hline
%   Very long term    \\\hline
% \end{tabular}
% \end{SideBySideExample}
% \\[1ex]
% This can be solved by setting a wider "width" option of "\diagbox" manually.
% \end{itemize}
%
% TODO:
% \begin{itemize}
% \item
% Improve the document of the source code. The algorithm of "\diagbox@triple"
% should be explained in detail. However, the explanations would be only
% available in Chinese, I'm sorry.
% \end{itemize}
%
%
% \title{\hypertarget{Chinese}{\pkg{diagbox} 宏包(\fileversion)}
%   \makebox[0pt][l]{\hspace{3cm}\large
%     \hyperlink{English}{$\Rightarrow$ \textsf{English Version}}}\\
%    制做斜线表头}
% \author{刘海洋 \\ \path{leoliu.pku@gmail.com}}
% \date{\filedate}
%
% \maketitle
%
%
% \section{简介}
%
% \pkg{diagbox} 设计用来代替旧的 \pkg{slashbox} 宏包\footnote{作者 Koichi
% Yasuoka(安岡孝一)与 Sato Toru(佐藤徹)。宏包见
% \url{CTAN://macros/latex/contrib/slashbox/slashbox.sty}。}。编写这个宏包的缘
% 起是 \pkg{slashbox} 因为缺少明确的自由许可信息,被 \TeX\ Live 排除。这个宏包
% 是在 LPPL 协议下发行的。
%
% \pkg{diagbox} 是 \pkg{slashbox} 宏包的一个现代的版本。它采用了新的 key-value
% 式语法参数,去除了 \pkg{slashbox} 原有的一些长度限制,并调用 \pkg{pict2e} 宏
% 包画斜线;特别还添加了绘制两条斜线的表头的新功能。
%
% 作为 \pkg{slashbox} 的代替,\pkg{diagbox} 除了提供自己的新命令,也提供了
% \pkg{slashbox} 原有的两个命令,语法不变,编译结果略有区别。
%
% \pkg{diagbox} 依赖 \eTeX{} 扩展(这在目前总是可用的),依赖 \pkg{pict2e},
% \pkg{keyval} 和 \pkg{fp} 宏包。
%
% \section{用法说明}
%
% \subsection{基本用法}
%
% 要使用本宏包,首先在导言区调用:
% \begin{Verbatim}
% \usepackage{diagbox}
% \end{Verbatim}
%
% \DescribeMacro{\diagbox}
% "\diagbox" 是宏包提供的主要命令。它可以带有两个必选参数,表示要生成斜线表头的
% 两部分内容。默认斜线是从西北到东南方向的。
%
% 例如:\\
% \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
% \begin{tabular}{|l|ccc|}
% \hline
% \diagbox{Time}{Day} & Mon & Tue & Wed \\
% \hline
% Morning   & used & used &      \\
% Afternoon &      & used & used \\
% \hline
% \end{tabular}
% \end{SideBySideExample}
% \medskip
%
% \changes{v2.0}{2011/11/22}{增加有三部分、双斜线的表头格式。}
% "\diagbox" 也可以接受三个参数,这样就会生成带有两条斜线的表头,例如:
% \begin{Example}[frame=single,numbers=left]
% \begin{tabular}{|l|ccc|}
% \hline
% \diagbox{Time}{Room}{Day} & Mon & Tue & Wed \\
% \hline
% Morning   & used & used &      \\
% Afternoon &      & used & used \\
% \hline
% \end{tabular}
% \end{Example}
%
% \subsection{更多参数设置}
%
% "\diagbox" 还可以在前面带一个可选参数,里面用 key-value 的语法设置宽度、方向
% 等更多的选项:
% \begin{description}
% \item[width] 明确指定盒子的总宽度。如果省略,则会自动计算能够放下所有内容的
% 宽度。
% \item[height] 明确指定盒子的总高度。
% \item[dir] 指定斜线方向。可以取 "NW"(西北)、"NE"(东北)、"SW"(西南)、
% "SE"(东南)四种方向。在只有一条斜线的表头中,"NE" 与 "SW"、 "SE" 与 "NW" 是
% 等价的。斜线方向的默认值是 "NW"。
%
% "\diagbox[dir="\meta{方向}"]{A}{B}" 在表格中的效果:\\
% \begin{tabular}{ll@{\qquad}ll}
% "NW" 或 "SE":&
% \begin{tabular}{|c|} \hline \diagbox[dir=SE]{A}{B} \\\hline \end{tabular} &
% "SW" 或 "NE":&
% \begin{tabular}{|c|} \hline \diagbox[dir=NE]{A}{B} \\\hline \end{tabular}
% \end{tabular}
%
% "\diagbox[dir="\meta{方向}"]{A}{M}{B}" 在表格中的效果:\\
% \begin{tabular}{*4{ll@{\qquad}}}
% "NW"&
% \begin{tabular}{|c|} \hline \diagbox[dir=NW]{A}{M}{B} \\\hline \end{tabular}&
% "NE"&
% \begin{tabular}{|c|} \hline \diagbox[dir=NE]{A}{M}{B} \\\hline \end{tabular}&
% "SW"&
% \begin{tabular}{|c|} \hline \diagbox[dir=SW]{A}{M}{B} \\\hline \end{tabular}&
% "SE"&
% \begin{tabular}{|c|} \hline \diagbox[dir=SE]{A}{M}{B} \\\hline \end{tabular}
% \end{tabular}
%
% \item[trim] 设置左边界或右边界不计算额外的空白,可以取值为 "l", "r", "lr" 或
% "rl"。这个选项在列格式包含 "@{}" 时将会有用。
% \end{description}
%
% \changes{v2.0}{2011/11/23}{变更 "trim" 选项的行为,去掉了使用 "trim" 选项时
% 内部的间距。这与 \pkg{slashbox} 行为不同。}
% 一个更复杂的例子:
% \begin{Example}[frame=single,numbers=left]
% \begin{tabular}{|@{}l|c|c|r@{}|}
% \hline
% \diagbox[width=5em,trim=l]{Time}{Day} & Mon & Tue & Wed\\
% \hline
% Morning   & used & used & used\\
% \hline
% Afternoon &      & used & \diagbox[dir=SW,height=2em,trim=r]{A}{B} \\
% \hline
% \end{tabular}
% \end{Example}
% \bigskip
%
% 此外,"\diagbox" 的表头内容还可以用 "\\" 手工换行。此时通常需要对自动计算的
% 表头高度进行手工调整。例如:
% \begin{Example}[frame=single,numbers=left]
% \begin{tabular}{|c|}
% \hline
% \diagbox[height=45pt]{line\\heads}{column\\heads} \\
% \hline
% \end{tabular}
% \end{Example}
%
% \subsection{对 \pkg{slashbox} 宏包的兼容性}
%
% 在使用 \pkg{diagbox} 宏包时,会模拟 \pkg{slashbox} 宏包的功能,并禁止
% \pkg{slashbox} 再被调用。
%
% \pkg{diagbox} 宏包提供了与 \pkg{slashbox} 大致相同的 "\slashbox" 与
% "\backslashbox" 两个命令。 "\slashbox" 与 "\backslashbox" 的语法来自
% \pkg{slashbox} 宏包,排版效果略有区别。这两个命令仅在旧文档中作为兼容命令使
% 用。实际中使用 "\diagbox" 更为方便。
%
% \DescribeMacro{\backslashbox}
% "\backslashbox" 基本功能与 "\diagbox" 类似。它带有两个可选参数,分别表示
% "\diagbox" 中的 "width" 与 "trim" 选项。
%
% \DescribeMacro{\slashbox}
% "\slashbox" 基本功能与 "\diagbox[dir=SW]" 类似。它也带有两个可选参数,表示
% "\diagbox" 中的 "width" 和 "tirm" 选项。
%
% 例如:\\
% \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
% \begin{tabular}{|c|c|c|} \hline
% \backslashbox[2cm]{num}{alpha}
%   & A  & B  \\\hline
% 1 & A1 & B1 \\\hline
% 2 & A2 & B2 \\\hline
% \end{tabular}
% \end{SideBySideExample}
%
% \section{已知问题和未来版本}
%
% 已知问题:
% \begin{itemize}
% \item "\slashbox" 与 "\backslashbox" 命令的效果与在 \pkg{slashbox} 宏包中不
% 同。两个宏包在计算盒子宽度和高度时,使用了不同的算法;同时,在处理
% "\slashbox" 第二个可选参数(即 "\diagbox" 的 "trim" 键)时,使用的方式也不一
% 样。
%
% 这不是 bug。通常 \pkg{diagbox} 计算出的宽度比 \pkg{slashbox} 的结果更安全一
% 些。
%
% \item
% "\diagbox" 生成的单元格必须是表列中最宽的一个。如果不能达到最宽,则画出的斜
% 线不能保证在正确的位置。例如:\\[1ex]
% \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
% \begin{tabular}{|c|}  \hline
%   \diagbox{A}{B}    \\\hline
%   Very long term    \\\hline
% \end{tabular}
% \end{SideBySideExample}
% \\[1ex]
% 此时可以手工设置较宽的 "\diagbox" 的 "width" 选项,解决此问题。
% \end{itemize}
%
% 未尽的工作:
% \begin{itemize}
% \item 源代码的文档需要改进。特别是在 "\diagbox@triple" 中的宽度和高度计算算
% 法需要详细说明。
% \end{itemize}
%
% \StopEventually{}
%
%
% \clearpage
% \section{Implementation / 代码实现}
%
% \iffalse
%<*package>
% \fi
%
%
% 使用 key-value 界面。
%    \begin{macrocode}
\RequirePackage{keyval}
%    \end{macrocode}
% 绘图依赖 \pkg{pict2e} 宏包。
%    \begin{macrocode}
\RequirePackage{pict2e}
%    \end{macrocode}
% 计算依赖 \pkg{fp} 宏包。
%    \begin{macrocode}
\RequirePackage[nomessages]{fp}
%    \end{macrocode}
% 
% 分配用到的盒子寄存器。它们分别对应于 "\diagbox" 三个必选参数的内容。
%    \begin{macrocode}
\newbox\diagbox@boxa
\newbox\diagbox@boxb
\newbox\diagbox@boxm
%    \end{macrocode}
% 分配长度变量。
%    \begin{macrocode}
\newdimen\diagbox@wd
\newdimen\diagbox@ht
\newdimen\diagbox@sepl
\newdimen\diagbox@sepr
%    \end{macrocode}
% 
% 定义 "\diagbox" 的键值选项。
%    \begin{macrocode}
\define@key{diagbox}{width}{%
  \setlength{\diagbox@wd}{#1}}
\define@key{diagbox}{height}{%
  \setlength{\diagbox@ht}{#1}}
\define@key{diagbox}{trim}{%
  \@tfor\@reserveda:=#1\do{%
    \ifcsname diagbox@sep\@reserveda\endcsname
      \setlength{\csname diagbox@sep\@reserveda\endcsname}{\z@}%
    \else
      \PackageError{diagbox}{Unknown trim option `#1'.}{l, r, lr and rl are supported.}%
    \fi}}
\define@key{diagbox}{dir}{%
  \def\diagbox@dir{#1}%
  \unless\ifcsname diagbox@dir@#1\endcsname
    \PackageError{diagbox}{Unknown direction `#1'.}{NW, NE, SW, SE are supported.}%
    \def\diagbox@dia{NW}%
  \fi}
\let\diagbox@dir@SE\relax
\let\diagbox@dir@SW\relax
\let\diagbox@dir@NE\relax
\let\diagbox@dir@NW\relax
%    \end{macrocode}
%
% \begin{macro}{\diagbox@pict}
% 这是带斜线的盒子本身。由一个 "picture" 环境实现。
%    \begin{macrocode}
\def\diagbox@pict{%
  \unitlength\p@
  \begin{picture}
    (\strip@pt\dimexpr\diagbox@wd-\diagbox@sepl-\diagbox@sepr\relax,\strip@pt\diagbox@ht)
    (\strip@pt\diagbox@sepl,0)
      \@nameuse{diagbox@\diagbox@part @pict@\diagbox@dir}
  \end{picture}}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\diagbox@double@pict@SE}
% 方向为 "SE" 的斜线盒子内容。
%    \begin{macrocode}
\def\diagbox@double@pict@SE{%
  \put(0,0) {\makebox(0,0)[bl]{\box\diagbox@boxa}}
  \put(\strip@pt\diagbox@wd,\strip@pt\diagbox@ht) {\makebox(0,0)[tr]{\box\diagbox@boxb}}
  \Line(0,\strip@pt\diagbox@ht)(\strip@pt\diagbox@wd,0)}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\diagbox@double@pict@NW}
% 方向 "NW" 与 "SE" 相同。
%    \begin{macrocode}
\let\diagbox@double@pict@NW\diagbox@double@pict@SE
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\diagbox@double@pict@NE}
% 方向为 "NE" 的斜线盒子内容。
%    \begin{macrocode}
\def\diagbox@double@pict@NE{%
  \put(0,\strip@pt\diagbox@ht) {\makebox(0,0)[tl]{\box\diagbox@boxa}}
  \put(\strip@pt\diagbox@wd,0) {\makebox(0,0)[br]{\box\diagbox@boxb}}
  \Line(0,0)(\strip@pt\diagbox@wd,\strip@pt\diagbox@ht)}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\diagbox@double@pict@NE}
% 方向 "SW" 与 "NE" 相同。
%    \begin{macrocode}
\let\diagbox@double@pict@SW\diagbox@double@pict@NE
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\diagbox@double}
% \changes{v2.0}{2011/11/22}{在使用 "trim" 选项时去掉内容与盒子边界的间距。这
% 与 \pkg{slashbox} 的行为不同。}
% 分成两部分的盒子。三个参数,分别为 key-value 格式的可选项、左半边内容、右半边
% 内容。这里的主要工作是读入参数并计算斜线盒子的大小。
%    \begin{macrocode}
\def\diagbox@double#1#2#3{%
  \begingroup
  \diagbox@wd=\z@
  \diagbox@ht=\z@
  \diagbox@sepl=\tabcolsep
  \diagbox@sepr=\tabcolsep
  \def\diagbox@part{double}%
  \setkeys{diagbox}{dir=NW,#1}%
  \setbox\diagbox@boxa=\hbox{%
    \begin{tabular}{@{\hspace{\diagbox@sepl}}l@{}}#2\end{tabular}}%
  \setbox\diagbox@boxb=\hbox{%
    \begin{tabular}{@{}r@{\hspace{\diagbox@sepr}}}#3\end{tabular}}%
  \ifdim\diagbox@wd=\z@
    \ifdim\wd\diagbox@boxa>\wd\diagbox@boxb
      \diagbox@wd=\dimexpr2\wd\diagbox@boxa+\diagbox@sepl+\diagbox@sepr\relax
    \else
      \diagbox@wd=\dimexpr2\wd\diagbox@boxb+\diagbox@sepl+\diagbox@sepr\relax
    \fi
  \fi
  \ifdim\diagbox@ht=\z@
    \diagbox@ht=\dimexpr\ht\diagbox@boxa+\dp\diagbox@boxa+\ht\diagbox@boxb+\dp\diagbox@boxb\relax
  \fi
  $\vcenter{\hbox{\diagbox@pict}}$%
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\diagbox@triple@setbox@NW}
%    \begin{macrocode}
\def\diagbox@triple@setbox@NW#1#2#3{%
  \setbox\diagbox@boxa=\hbox{%
    \begin{tabular}{@{\hspace{\diagbox@sepl}}l@{}}#1\end{tabular}}%
  \setbox\diagbox@boxm=\hbox{%
    \begin{tabular}{@{\hspace{\diagbox@sepl}}l@{}}#2\end{tabular}}%
  \setbox\diagbox@boxb=\hbox{%
    \begin{tabular}{@{}r@{\hspace{\diagbox@sepr}}}#3\end{tabular}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\diagbox@triple@setbox@SW}
%    \begin{macrocode}
\let\diagbox@triple@setbox@SW\diagbox@triple@setbox@NW
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\diagbox@triple@setbox@NW}
%    \begin{macrocode}
\def\diagbox@triple@setbox@SE#1#2#3{%
  \setbox\diagbox@boxa=\hbox{%
    \begin{tabular}{@{\hspace{\diagbox@sepl}}l@{}}#1\end{tabular}}%
  \setbox\diagbox@boxm=\hbox{%
    \begin{tabular}{@{}r@{\hspace{\diagbox@sepr}}}#2\end{tabular}}%
  \setbox\diagbox@boxb=\hbox{%
    \begin{tabular}{@{}r@{\hspace{\diagbox@sepr}}}#3\end{tabular}}}
%    \end{macrocode}
% \end{macro}
%
% \begin{macro}{\diagbox@triple@setbox@NE}
%    \begin{macrocode}
\let\diagbox@triple@setbox@NE\diagbox@triple@setbox@SE
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\diagbox@triple@pict@NW}
%    \begin{macrocode}
\def\diagbox@triple@pict@NW{%
  \put(0,0)   {\makebox(0,0)[bl]{\box\diagbox@boxa}}
  \put(0,\y)  {\makebox(0,0)[tl]{\box\diagbox@boxm}}
  \put(\x,\y) {\makebox(0,0)[tr]{\box\diagbox@boxb}}
  \Line(0,\yym)(\x,0)
  \Line(\xm,\y)(\x,0)}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\diagbox@triple@pict@NE}
%    \begin{macrocode}
\def\diagbox@triple@pict@NE{%
  \put(0,\y)  {\makebox(0,0)[tl]{\box\diagbox@boxa}}
  \put(\x,\y) {\makebox(0,0)[tr]{\box\diagbox@boxm}}
  \put(\x,0)  {\makebox(0,0)[br]{\box\diagbox@boxb}}
  \Line(0,0)(\xxm,\y)
  \Line(0,0)(\x,\yym)}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\diagbox@triple@pict@SW}
%    \begin{macrocode}
\def\diagbox@triple@pict@SW{%
  \put(0,\y) {\makebox(0,0)[tl]{\box\diagbox@boxa}}
  \put(0,0)  {\makebox(0,0)[bl]{\box\diagbox@boxm}}
  \put(\x,0) {\makebox(0,0)[br]{\box\diagbox@boxb}}
  \Line(0,\ym)(\x,\y)
  \Line(\xm,0)(\x,\y)}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\diagbox@triple@pict@SE}
%    \begin{macrocode}
\def\diagbox@triple@pict@SE{%
  \put(0,0)   {\makebox(0,0)[bl]{\box\diagbox@boxa}}
  \put(\x,0)  {\makebox(0,0)[br]{\box\diagbox@boxm}}
  \put(\x,\y) {\makebox(0,0)[tr]{\box\diagbox@boxb}}
  \Line(0,\y)(\xxm,0)
  \Line(0,\y)(\x,\ym)}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\diagbox@triplebox}
% \changes{v2.0}{2011/11/22}{新增三部分双斜线的盒子}
% 分成三部分的盒子。四个参数,分别为 key-value 格式的可选项、左半边内容、中间
% 内容、右半边内容。
%    \begin{macrocode}
\def\diagbox@triple#1#2#3#4{%
  \begingroup
  \diagbox@wd=\z@
  \diagbox@ht=\z@
  \diagbox@sepl=\tabcolsep
  \diagbox@sepr=\tabcolsep
  \def\diagbox@part{triple}%
  \setkeys{diagbox}{dir=NW,#1}%
  \@nameuse{diagbox@triple@setbox@\diagbox@dir}{#2}{#3}{#4}%
%    \end{macrocode}
% 取长宽
%    \begin{macrocode}
  \edef\xa{\strip@pt\wd\diagbox@boxa}%
  \edef\ya{\strip@pt\dimexpr\ht\diagbox@boxa+\dp\diagbox@boxa\relax}%
  \edef\xb{\strip@pt\wd\diagbox@boxb}%
  \edef\yb{\strip@pt\dimexpr\ht\diagbox@boxb+\dp\diagbox@boxb\relax}%
  \edef\xm{\strip@pt\wd\diagbox@boxm}%
  \edef\ym{\strip@pt\dimexpr\ht\diagbox@boxm+\dp\diagbox@boxm\relax}%
%    \end{macrocode}
% 列方程,求方程系数
%    \begin{macrocode}
  \FPneg\bi\yb
  \FPadd\ci\xb\xm  \FPneg\ci\ci
  \FPmul\di\xm\yb
  \FPadd\bj\ya\ym  \FPneg\bj\bj
  \FPneg\cj\xa
  \FPmul\dj\xa\ym
%    \end{macrocode}
% 解方程
%    \begin{macrocode}
  \FPsub\u\dj\di
  \FPupn{v}{bj ci * bi cj * -}%
  \FPupn{delta}{bi dj * bj di * - cj ci - * 4 * %
    v u + copy * %
    - 2 swap root}%
  \ifdim\diagbox@wd=\z@
    \FPupn{x}{2 bj bi - delta v u - + / /}%
    \diagbox@wd=\x\p@
  \else
    \edef\x{\strip@pt\diagbox@wd}%
  \fi
  \ifdim\diagbox@ht=\z@
    \FPupn{y}{2 cj ci - delta v u + - / /}%
    \diagbox@ht=\y\p@
  \else
    \edef\y{\strip@pt\diagbox@ht}%
  \fi
  \FPsub\xxm\x\xm
  \FPsub\yym\y\ym
%    \end{macrocode}
% 画盒子
%    \begin{macrocode}
  $\vcenter{\hbox{\diagbox@pict}}$%
  \endgroup}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\diagbox}
% \changes{v2.0}{2011/11/22}{判断参数个数,选择两部分或三部分盒子。}
% 主要的用户命令。判断使用两部分还是三部分的盒子。
%    \begin{macrocode}
\newcommand\diagbox[3][]{%
  \@ifnextchar\bgroup
    {\diagbox@triple{#1}{#2}{#3}}{\diagbox@double{#1}{#2}{#3}}}
%    \end{macrocode}
% \end{macro}
%
% 以下代码用来模拟 \pkg{slashbox} 宏包的功能。
%
% 禁止读入 \pkg{slashbox}。
%    \begin{macrocode}
\expandafter\xdef\csname ver@slashbox.\@pkgextension\endcsname{9999/99/99}
%    \end{macrocode}
%
%
% \begin{macro}{\slashbox}
% 模拟 "\slashbox"。
%    \begin{macrocode}
\def\slashbox{%
  \def\diagbox@slashbox@options{dir=SW,}%
  \slashbox@}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\backslashbox}
% 模拟 "\backslashbox"。
%    \begin{macrocode}
\def\backslashbox{%
  \def\diagbox@slashbox@options{dir=NW,}%
  \slashbox@}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\slashbox@}
%    \begin{macrocode}
\newcommand\slashbox@[1][]{%
  \ifx\relax#1\relax\else
    \edef\diagbox@slashbox@options{%
      \unexpanded\expandafter{\diagbox@slashbox@options}%
      \unexpanded{width=#1,}}%
  \fi
  \slashbox@@}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\slashbox@@}
%    \begin{macrocode}
\newcommand\slashbox@@[3][]{%
  \edef\diagbox@slashbox@options{%
    \unexpanded\expandafter{\diagbox@slashbox@options}%
    \unexpanded{trim=#1,}}%
  \expandafter\diagbox\expandafter[\diagbox@slashbox@options]{#2}{#3}}
\endinput
%    \end{macrocode}
% \end{macro}
%
%
% \iffalse
%</package>
% \fi
%
% \Finale
\endinput