Debian Codegraph @

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

raw
1 % \iffalse meta-comment
2 %
3 % Copyright (C) 2011--2020 by Leo Liu <[email protected]>
4 % ---------------------------------------------------------------------------
5 % This work may be distributed and/or modified under the
6 % conditions of the LaTeX Project Public License, either version 1.3
7 % of this license or (at your option) any later version.
8 % The latest version of this license is in
9 % http://www.latex-project.org/lppl.txt
10 % and version 1.3 or later is part of all distributions of LaTeX
11 % version 2005/12/01 or later.
12 %
13 % This work has the LPPL maintenance status `maintained'.
14 %
15 % The Current Maintainer of this work is Leo Liu.
16 %
17 % This work consists of the files diagbox.dtx and diagbox.ins
18 % and the derived filebase diagbox.sty.
19 %
20 % \fi
21 %
22 % \iffalse
23 %<*driver>
24 \ProvidesFile{diagbox.dtx}
25 %</driver>
26 %<package>\NeedsTeXFormat{LaTeX2e}[1999/12/01]
27 %<package>\ProvidesPackage{diagbox}
28 %<*package>
29 [2020/02/09 v2.3 Making table heads with diagonal lines]
30 %</package>
31 %
32 %<*driver>
33 \documentclass[letterpaper]{ltxdoc}
34 \AtBeginDocument{
35 \DeleteShortVerb{\|}
36 \MakeShortVerb{\"}}
37 \usepackage{diagbox}
38 \usepackage{xcolor}
39 \usepackage[left=1.7in,top=1in,bottom=1in]{geometry}
40 \usepackage{fancyvrb-ex}
41 \usepackage{amsmath}
42 \usepackage[UTF8]{ctex}
43 \setmainfont{Latin Modern Roman}
44 \setsansfont{Latin Modern Sans}
45 \setmonofont{Latin Modern Mono}
46 \makeatletter
47 \renewcommand\glossary@prologue{%
48 \section*{版本历史}
49 \markboth{版本历史}{版本历史}}
50 \renewcommand\index@prologue{%
51 \section*{Index / 代码索引}
52 \markboth{Index / 代码索引}{Index / 代码索引}
53 斜体的数字表示对应项说明所在的页码。下划线的数字表示定义所在的代码行号;而直
54 立体的数字表示对应项使用时所在的行号。}
55 \renewcommand*\title[2][]{\gdef\@title{#2}}
56 \renewcommand*\author[2][]{\gdef\@author{#2}}
57 \providecommand\eTeX{$\m@th\varepsilon$-\TeX}
58 \makeatother
59 \usepackage[numbered]{hypdoc}
60 \hypersetup{pdfstartview=FitH}
61 \fvset{gobble=1} %% 删除例子代码开头的 %
62 \EnableCrossrefs
63 \CodelineIndex
64 \RecordChanges
65 \begin{document}
66 \DocInput{diagbox.dtx}
67 \PrintChanges
68 \PrintIndex
69 \end{document}
70 %</driver>
71 % \fi
72 %
73 % \CheckSum{901}
74 %
75 % \CharacterTable
76 % {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
77 % 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
78 % Digits \0\1\2\3\4\5\6\7\8\9
79 % Exclamation \! Double quote \" Hash (number) \#
80 % Dollar \$ Percent \% Ampersand \&
81 % Acute accent \' Left paren \( Right paren \)
82 % Asterisk \* Plus \+ Comma \,
83 % Minus \- Point \. Solidus \/
84 % Colon \: Semicolon \; Less than \<
85 % Equals \= Greater than \> Question mark \?
86 % Commercial at \@ Left bracket \[ Backslash \\
87 % Right bracket \] Circumflex \^ Underscore \_
88 % Grave accent \` Left brace \{ Vertical bar \|
89 % Right brace \} Tilde \~}
90 %
91 %
92 % \changes{v1.0}{2011/11/21}{初始版本。}
93 %
94 % \DoNotIndex{\newcommand, \newenvironment, \@nameuse, \@pkgextension,
95 % \@reserveda, \@tfor, \begin, \begingroup, \box, \csname, \def, \define@key,
96 % \dimexpr, \do, \dp, \edef, \else, \end, \endcsname, \endgroup, \endinput,
97 % \expandafter, \fi, \hbox, \ht, \ifcsname, \ifdim, \ifx, \Line, \makebox,
98 % \newbox, \newdimen, \p@, \PackageError, \pkg, \put, \relax, \RequirePackage,
99 % \setbox, \setkeys, \setlength, \strip@pt, \unexpanded, \unitlength,
100 % \vcenter, \wd, \xdef, \z@, \@ifnextchar, \bgroup, \FPadd, \FPmul, \FPneg,
101 % \FPsub, \FPupn, \let, \unless, \hspace, \@tempskipa, \newif, \detokenize,
102 % \@@, \\, \c, \l, \cs, \fp, \dim, \msg, \scan}
103 %
104 % \providecommand*{\pkg}{\textsf}
105 % \GetFileInfo{diagbox.dtx}
106 % \title[diagbox Package (\fileversion):
107 % Making Table Heads with Diagonal Lines]%
108 % {\hypertarget{English}{\pkg{diagbox} Package (\fileversion)}
109 % \makebox[0pt][l]{\hspace{3cm}\large
110 % \hyperlink{Chinese}{$\Rightarrow$ \textsf{中文版}}}\\
111 % Making Table Heads with Diagonal Lines}
112 % \author[Leo Liu <[email protected]>]%
113 % {Leo Liu \\ \path{[email protected]}}
114 % \date{\filedate}
115 %
116 % \maketitle
117 %
118 % \section{Introduction}
119 %
120 % \pkg{diagbox} is a replacement of old \pkg{slashbox} package\footnote{By
121 % Koichi Yasuoka and Toru Sato. Available on
122 % \url{CTAN://macros/latex/contrib/slashbox/slashbox.sty}}. I write this
123 % package simply because that \pkg{slashbox} is not available in \TeX\ Live
124 % for licening problems. \pkg{slashbox} has no explicit license information
125 % available, but \pkg{diagbox} is under LPPL.
126 %
127 % \pkg{diagbox} is a modern alternative of \pkg{slashbox}. I changed the
128 % user interface to use a key-value syntax, get rid of some restrictions of
129 % \pkg{slashbox}, use \pkg{pict2e} to draw diagonal lines. Especially, this
130 % package also provides ability to make a box with two diagonal lines in it.
131 % All these can be obtained by a "\diagbox" command.
132 %
133 % As a replacement of \pkg{slashbox}, \pkg{diagbox} package also provides
134 % compatible macros of \pkg{slashbox}, but the result is a little different.
135 %
136 % To use \pkg{diagbox}, \eTeX{} is needed. And \pkg{diagbox} requires
137 % \pkg{pict2e}, \pkg{keyval}, \pkg{calc}, and \pkg{fp} packages.
138 %
139 %
140 % \section{Usage}
141 %
142 % \subsection{Basic usage}
143 %
144 % To load the package, just put this in the preamble:
145 % \begin{Verbatim}
146 % \usepackage{diagbox}
147 % \end{Verbatim}
148 %
149 % \DescribeMacro{\diagbox}
150 % "\diagbox" is the main command. It can take two arguments, to produce a box
151 % with a diagonal line from north west to south east.
152 %
153 % For example:\\
154 % \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
155 % \begin{tabular}{|l|ccc|}
156 % \hline
157 % \diagbox{Time}{Day} & Mon & Tue & Wed \\
158 % \hline
159 % Morning & used & used & \\
160 % Afternoon & & used & used \\
161 % \hline
162 % \end{tabular}
163 % \end{SideBySideExample}
164 % \medskip
165 %
166 % "\diagbox" can also take three arguments, to draw a table head with two
167 % diagonal lines. For example,
168 % \begin{Example}[frame=single,numbers=left]
169 % \begin{tabular}{|l|ccc|}
170 % \hline
171 % \diagbox{Time}{Room}{Day} & Mon & Tue & Wed \\
172 % \hline
173 % Morning & used & used & \\
174 % Afternoon & & used & used \\
175 % \hline
176 % \end{tabular}
177 % \end{Example}
178 %
179 % \subsection{More options}
180 %
181 % "\diagbox" can take a key-value list as an optional argument to specify the
182 % width and height of the box, the direction of the diagonal line, and the
183 % trimming margins:
184 % \begin{description}
185 % \item[width] Specify the width of the box explicitly. If it is omitted,
186 % \pkg{diagbox} will calculate the width automatically.
187 %
188 % \item[height] Specify the height of the box explicitly. If it is omitted,
189 % \pkg{diagbox} will calculate the height automatically. In the argument, one
190 % can use "\line" as a line height.
191 %
192 % \item[dir] Specify the direction of the diagonal line. The value can be
193 % "NW", "NE", "SW" and "SE". Default value is "NW". The meaning of the values
194 % see below.
195 % \begin{itemize}
196 % \item "\diagbox[dir="\meta{direction}"]{A}{B}" in a table looks:\\
197 % \begin{tabular}{ll@{\qquad}ll}
198 % "NW" or "SE":&
199 % \begin{tabular}{|c|} \hline \diagbox[dir=SE]{A}{B} \\\hline \end{tabular} &
200 % "SW" or "NE":&
201 % \begin{tabular}{|c|} \hline \diagbox[dir=NE]{A}{B} \\\hline \end{tabular}
202 % \end{tabular}
203 % \item "\diagbox[dir="\meta{direction}"]{A}{M}{B}" in a table looks:\\
204 % \begin{tabular}{*4{ll@{\qquad}}}
205 % "NW"&
206 % \begin{tabular}{|c|} \hline \diagbox[dir=NW]{A}{M}{B} \\\hline \end{tabular}&
207 % "NE"&
208 % \begin{tabular}{|c|} \hline \diagbox[dir=NE]{A}{M}{B} \\\hline \end{tabular}&
209 % "SW"&
210 % \begin{tabular}{|c|} \hline \diagbox[dir=SW]{A}{M}{B} \\\hline \end{tabular}&
211 % "SE"&
212 % \begin{tabular}{|c|} \hline \diagbox[dir=SE]{A}{M}{B} \\\hline \end{tabular}
213 % \end{tabular}
214 % \end{itemize}
215 %
216 % \item[innerwidth] Specify the width of the inner content box. "innerwidth"
217 % option is useful when specifying column width. For example:
218 % \begin{Example}
219 % \begin{tabular}{|p{2cm}|} \hline
220 % \diagbox[innerwidth=2cm]{A}{B} \\ \hline
221 % \end{tabular}
222 % \end{Example}
223 %
224 % \item[innerleftsep, innerrightsep] Specify the distances between the border
225 % of the inner content box and the border of the diagonal box. We have:
226 % \[
227 % "innerleftsep" + "innerwidth" + "innerrightsep" = "width".
228 % \]
229 % For example:
230 % \begin{Example}
231 % \begin{tabular}{|c|} \hline
232 % \diagbox[innerleftsep=.5cm,innerrightsep=0pt]{A}{B} \\ \hline
233 % \end{tabular}
234 % \end{Example}
235 %
236 % \item[outerleftsep, outerrightsep] Specify the distances between the border
237 % of the diagonal box and the border of the tabular cell. Usually they are
238 % negative values, which satisfy
239 % \begin{align*}
240 % "outerleftsep" + "LEFTtabcolsep" &= 0\,\mathrm{pt}, \\
241 % "outerrightsep" + "RIGHTtabcolsep" &= 0\,\mathrm{pt}.
242 % \end{align*}
243 % where "LEFTtabcolsep" and "RIGHTtabcolsep" are the distances between
244 % verticle lines and the tabular cell content (the diagonal box). For example:
245 % \begin{Example}
246 % \begin{tabular}{|r@{\hspace{20pt}}|l|} \hline
247 % \diagbox[outerrightsep=-20pt]{A}{B} & C \\ \hline
248 % AABB & CC \\ \hline
249 % \end{tabular}
250 % \end{Example}
251 %
252 % \item[leftsep, rightsep] Specify the left and right distances, which are
253 % equivalent to:
254 % \begin{align*}
255 % "innerleftsep" &:= "leftsep", & "innerrightsep" &:= "rightsep", \\
256 % "outerleftsep" &:= -"leftsep", & "outerrightsep" &:= -"rightsep".
257 % \end{align*}
258 %
259 % \item[trim] Specify the margin to be trimmed. The value can be "l", "r", and
260 % "lr", "rl". This helps the slash line exceeds the boundary when "@{}" column
261 % specifier is used.
262 %
263 % Note: "trim=l" has the same effect as "leftsep=0pt", and "trim=r" has the
264 % same effect as "rightsep=0pt".
265 %
266 % \item[font] Specify the font of the cell.
267 %
268 % "\diagbox[font=\footnotesize\itshape]{Small}{Italic}"\quad
269 % \begin{tabular}{|c|} \hline
270 % \diagbox[font=\footnotesize\itshape]{Small}{Italic} \\ \hline
271 % \end{tabular}
272 %
273 % \item[linewidth] Specify the line width of the diagonal lines.
274 % \item[linecolor] Specify the line color of the diagonal lines. (\pkg{color}
275 % or \pkg{xcolor} is needed.)
276 %
277 % "\diagbox[linewidth=2pt,linecolor=blue]{AAA}{BBB}"\quad
278 % \begin{tabular}{|c|} \hline
279 % \diagbox[linewidth=2pt,linecolor=blue]{AAA}{BBB} \\ \hline
280 % \end{tabular}
281 % \end{description}
282 %
283 % Here is a more complex example to show the usage of the options:
284 % \begin{Example}[frame=single,numbers=left]
285 % \begin{tabular}{|@{}l|c|c|r@{}|}
286 % \hline
287 % \diagbox[width=5em,trim=l]{Time}{Day} & Mon & Tue & Wed\\
288 % \hline
289 % Morning & used & used & used\\
290 % \hline
291 % Afternoon & & used & \diagbox[dir=SW,height=2em,trim=r]{A}{B} \\
292 % \hline
293 % \end{tabular}
294 % \end{Example}
295 %
296 % \bigskip
297 % What's more, you can use "\\" to break lines in "\diagbox". Manual setting
298 % of the height of the head may be needed. For example,
299 % \begin{Example}[frame=single,numbers=left]
300 % \begin{tabular}{|c|}
301 % \hline
302 % \diagbox[height=3\line]{line\\heads}{column\\heads} \\
303 % \hline
304 % \end{tabular}
305 % \end{Example}
306 %
307 % \subsection{Compatibility with \pkg{slashbox}}
308 %
309 % \pkg{diagbox} package emulates \pkg{slashbox} and also prevents
310 % \pkg{slashbox} to be loaded.
311 %
312 % \pkg{diagbox} package provides "\slashbox" and "\backslashbox" which syntax
313 % similar to \pkg{slashbox} package. However, the results of the two packages
314 % are a little different. These two commands are for compatibility only, it is
315 % better to use "\diagbox" instead for new documents.
316 %
317 % \DescribeMacro{\backslashbox}
318 % "\backslashbox" works as "\diagbox", but it takes two optional arguments to
319 % specify the "width" and "trim" options.
320 %
321 % \DescribeMacro{\slashbox}
322 % "\slashbox" works as "\diagbox[dir=SW]", and takes two optional arguments to
323 % specify the "width" and "trim" options.
324 %
325 % For example,\\
326 % \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
327 % \begin{tabular}{|c|c|c|} \hline
328 % \backslashbox[2cm]{num}{alpha}
329 % & A & B \\\hline
330 % 1 & A1 & B1 \\\hline
331 % 2 & A2 & B2 \\\hline
332 % \end{tabular}
333 % \end{SideBySideExample}
334 %
335 %
336 % \section{Known issues and TODO}
337 %
338 % Known issues:
339 % \begin{itemize}
340 % \item The result of "\slashbox" and "\backslashbox" is different with
341 % \pkg{slashbox} package. The algorithms to calculate the width and height are
342 % different; and the results of the second optional argument of "\slashbox"
343 % (i.e. "trim" key in "\diagbox") in the two packages are differernt.
344 %
345 % This is not a bug. Usually the width calculated by \pkg{diagbox} is more
346 % safe than \pkg{slashbox}.
347 %
348 % \item For a triple box (a table head with two diagonal lines), the first
349 % argument must not be too wide and the third argument must not be too high.
350 % Otherwise, the layout algorithm cannot calculate the proper width and height
351 % of the table head, and a warning will be issued. (Thanks to Frank Mittelbach.)
352 %
353 % \item
354 % The cell with "\diagbox" should be the widest one of the column. Otherwise
355 % the slash line cannot exceeds the boundary. For example,\\[1ex]
356 % \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
357 % \begin{tabular}{|c|} \hline
358 % \diagbox{A}{B} \\\hline
359 % Very long term \\\hline
360 % \end{tabular}
361 % \end{SideBySideExample}
362 % \\[1ex]
363 % This can be solved by setting a wider "width" option of "\diagbox" manually.
364 % \end{itemize}
365 %
366 %
367 % \title[]{\hypertarget{Chinese}{\pkg{diagbox} 宏包(\fileversion)}
368 % \makebox[0pt][l]{\hspace{3cm}\large
369 % \hyperlink{English}{$\Rightarrow$ \textsf{English Version}}}\\
370 % 制做斜线表头}
371 % \author{刘海洋 \\ \path{[email protected]}}
372 % \date{\filedate}
373 %
374 % \maketitle
375 %
376 %
377 % \section{简介}
378 %
379 % \pkg{diagbox} 设计用来代替旧的 \pkg{slashbox} 宏包\footnote{作者 Koichi
380 % Yasuoka(安岡孝一)与 Sato Toru(佐藤徹)。宏包见
381 % \url{CTAN://macros/latex/contrib/slashbox/slashbox.sty}。}。编写这个宏包的缘
382 % 起是 \pkg{slashbox} 因为缺少明确的自由许可信息,被 \TeX\ Live 排除。这个宏包
383 % 是在 LPPL 协议下发行的。
384 %
385 % \pkg{diagbox} 是 \pkg{slashbox} 宏包的一个现代的版本。它采用了新的 key-value
386 % 式语法参数,去除了 \pkg{slashbox} 原有的一些长度限制,并调用 \pkg{pict2e} 宏
387 % 包画斜线;特别还添加了绘制两条斜线的表头的新功能。
388 %
389 % 作为 \pkg{slashbox} 的代替,\pkg{diagbox} 除了提供自己的新命令,也提供了
390 % \pkg{slashbox} 原有的两个命令,语法不变,编译结果略有区别。
391 %
392 % \pkg{diagbox} 依赖 \eTeX{} 扩展(这在目前总是可用的),依赖 \pkg{pict2e}、
393 % \pkg{keyval}、\pkg{calc} 和 \pkg{fp} 宏包。
394 %
395 % \section{用法说明}
396 %
397 % \subsection{基本用法}
398 %
399 % 要使用本宏包,首先在导言区调用:
400 % \begin{Verbatim}
401 % \usepackage{diagbox}
402 % \end{Verbatim}
403 %
404 % \DescribeMacro{\diagbox}
405 % "\diagbox" 是宏包提供的主要命令。它可以带有两个必选参数,表示要生成斜线表头的
406 % 两部分内容。默认斜线是从西北到东南方向的。
407 %
408 % 例如:\\
409 % \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
410 % \begin{tabular}{|l|ccc|}
411 % \hline
412 % \diagbox{Time}{Day} & Mon & Tue & Wed \\
413 % \hline
414 % Morning & used & used & \\
415 % Afternoon & & used & used \\
416 % \hline
417 % \end{tabular}
418 % \end{SideBySideExample}
419 % \medskip
420 %
421 % \changes{v2.0}{2011/11/22}{增加有三部分、双斜线的表头格式。}
422 % "\diagbox" 也可以接受三个参数,这样就会生成带有两条斜线的表头,例如:
423 % \begin{Example}[frame=single,numbers=left]
424 % \begin{tabular}{|l|ccc|}
425 % \hline
426 % \diagbox{Time}{Room}{Day} & Mon & Tue & Wed \\
427 % \hline
428 % Morning & used & used & \\
429 % Afternoon & & used & used \\
430 % \hline
431 % \end{tabular}
432 % \end{Example}
433 %
434 % \subsection{更多参数设置}
435 %
436 % "\diagbox" 还可以在前面带一个可选参数,里面用 key-value 的语法设置宽度、方向
437 % 等更多的选项:
438 % \begin{description}
439 % \item[width] 明确指定盒子的总宽度。如果省略,则会自动计算能够放下所有内容的
440 % 宽度。
441 % \item[height] 明确指定盒子的总高度。
442 % \item[dir] 指定斜线方向。可以取 "NW"(西北)、"NE"(东北)、"SW"(西南)、
443 % "SE"(东南)四种方向。在只有一条斜线的表头中,"NE" 与 "SW"、 "SE" 与 "NW" 是
444 % 等价的。斜线方向的默认值是 "NW"。
445 %
446 % "\diagbox[dir="\meta{方向}"]{A}{B}" 在表格中的效果:\\
447 % \begin{tabular}{ll@{\qquad}ll}
448 % "NW" 或 "SE":&
449 % \begin{tabular}{|c|} \hline \diagbox[dir=SE]{A}{B} \\\hline \end{tabular} &
450 % "SW" 或 "NE":&
451 % \begin{tabular}{|c|} \hline \diagbox[dir=NE]{A}{B} \\\hline \end{tabular}
452 % \end{tabular}
453 %
454 % "\diagbox[dir="\meta{方向}"]{A}{M}{B}" 在表格中的效果:\\
455 % \begin{tabular}{*4{ll@{\qquad}}}
456 % "NW"&
457 % \begin{tabular}{|c|} \hline \diagbox[dir=NW]{A}{M}{B} \\\hline \end{tabular}&
458 % "NE"&
459 % \begin{tabular}{|c|} \hline \diagbox[dir=NE]{A}{M}{B} \\\hline \end{tabular}&
460 % "SW"&
461 % \begin{tabular}{|c|} \hline \diagbox[dir=SW]{A}{M}{B} \\\hline \end{tabular}&
462 % "SE"&
463 % \begin{tabular}{|c|} \hline \diagbox[dir=SE]{A}{M}{B} \\\hline \end{tabular}
464 % \end{tabular}
465 %
466 % \item[innerwidth] 设置盒子中内容的宽度。"innerwidth" 选项可以与表格的列宽度
467 % 一起设置并保持一致。如:
468 % \begin{Example}
469 % \begin{tabular}{|p{2cm}|} \hline
470 % \diagbox[innerwidth=2cm]{A}{B} \\ \hline
471 % \end{tabular}
472 % \end{Example}
473 %
474 % \item[innerleftsep, innerrightsep] 设置内间距,即内容盒子与斜线盒子边界之间
475 % 的距离。我们有关系式:
476 % \[
477 % "innerleftsep" + "innerwidth" + "innerrightsep" = "width".
478 % \]
479 % 例如:
480 % \begin{Example}
481 % \begin{tabular}{|c|} \hline
482 % \diagbox[innerleftsep=.5cm,innerrightsep=0pt]{A}{B} \\ \hline
483 % \end{tabular}
484 % \end{Example}
485 %
486 % \item[outerleftsep, outerrightsep] 设置外间距,即斜线盒子边界到表格单元格边
487 % 界的距离。由于斜线是沿单元格画出而非斜线盒子本身画出,斜线通常会伸出斜线盒子
488 % 之外,此距离为负数,并满足如下关系式:
489 % \begin{align*}
490 % "outerleftsep" + "LEFTtabcolsep" &= 0\,\mathrm{pt}, \\
491 % "outerrightsep" + "RIGHTtabcolsep" &= 0\,\mathrm{pt}.
492 % \end{align*}
493 % 其中 "LEFTtabcolsep" 与 "RIGHTtabcolsep" 是表格竖线与表格内容(斜线例子边
494 % 界)之间的距离。例如:
495 % \begin{Example}
496 % \begin{tabular}{|r@{\hspace{20pt}}|l|} \hline
497 % \diagbox[outerrightsep=-20pt]{A}{B} & C \\ \hline
498 % AABB & CC \\ \hline
499 % \end{tabular}
500 % \end{Example}
501 %
502 % \item[leftsep, rightsep] 同时设置左右内外间距,满足关系式:
503 % \begin{align*}
504 % "innerleftsep" &:= "leftsep", & "innerrightsep" &:= "rightsep", \\
505 % "outerleftsep" &:= -"leftsep", & "outerrightsep" &:= -"rightsep".
506 % \end{align*}
507 %
508 % \item[trim] 设置左边界或右边界不计算额外的空白,可以取值为 "l", "r", "lr" 或
509 % "rl"。这个选项在列格式包含 "@{}" 时将会有用。
510 %
511 % 注:"trim=l" 与 "leftsep=0pt" 效果相同,而 "trim=r" 与 "rightsep=0pt" 效果相
512 % 同。
513 %
514 % \item[font] 设置单元格字体。
515 %
516 % "\diagbox[font=\footnotesize\itshape]{Small}{Italic}"\quad
517 % \begin{tabular}{|c|} \hline
518 % \diagbox[font=\footnotesize\itshape]{Small}{Italic} \\ \hline
519 % \end{tabular}
520 %
521 % \item[linewidth] 设置斜线宽度。
522 % \item[linecolor] 设置斜线颜色。(需要自行载入 \pkg{color} 或 \pkg{xcolor} 宏
523 % 包。)
524 %
525 % "\diagbox[linewidth=2pt,linecolor=blue]{AAA}{BBB}"\quad
526 % \begin{tabular}{|c|} \hline
527 % \diagbox[linewidth=2pt,linecolor=blue]{AAA}{BBB} \\ \hline
528 % \end{tabular}
529 % \end{description}
530 %
531 % \changes{v2.0}{2011/11/23}{变更 "trim" 选项的行为,去掉了使用 "trim" 选项时
532 % 内部的间距。这与 \pkg{slashbox} 行为不同。}
533 % 一个更复杂的例子:
534 % \begin{Example}[frame=single,numbers=left]
535 % \begin{tabular}{|@{}l|c|c|r@{}|}
536 % \hline
537 % \diagbox[width=5em,trim=l]{Time}{Day} & Mon & Tue & Wed\\
538 % \hline
539 % Morning & used & used & used\\
540 % \hline
541 % Afternoon & & used & \diagbox[dir=SW,height=2em,trim=r]{A}{B} \\
542 % \hline
543 % \end{tabular}
544 % \end{Example}
545 % \bigskip
546 %
547 % 此外,"\diagbox" 的表头内容还可以用 "\\" 手工换行。此时通常需要对自动计算的
548 % 表头高度进行手工调整。例如:
549 % \begin{Example}[frame=single,numbers=left]
550 % \begin{tabular}{|c|}
551 % \hline
552 % \diagbox[height=3\line]{line\\heads}{column\\heads} \\
553 % \hline
554 % \end{tabular}
555 % \end{Example}
556 %
557 % \subsection{对 \pkg{slashbox} 宏包的兼容性}
558 %
559 % 在使用 \pkg{diagbox} 宏包时,会模拟 \pkg{slashbox} 宏包的功能,并禁止
560 % \pkg{slashbox} 再被调用。
561 %
562 % \pkg{diagbox} 宏包提供了与 \pkg{slashbox} 大致相同的 "\slashbox" 与
563 % "\backslashbox" 两个命令。 "\slashbox" 与 "\backslashbox" 的语法来自
564 % \pkg{slashbox} 宏包,排版效果略有区别。这两个命令仅在旧文档中作为兼容命令使
565 % 用。实际中使用 "\diagbox" 更为方便。
566 %
567 % \DescribeMacro{\backslashbox}
568 % "\backslashbox" 基本功能与 "\diagbox" 类似。它带有两个可选参数,分别表示
569 % "\diagbox" 中的 "width" 与 "trim" 选项。
570 %
571 % \DescribeMacro{\slashbox}
572 % "\slashbox" 基本功能与 "\diagbox[dir=SW]" 类似。它也带有两个可选参数,表示
573 % "\diagbox" 中的 "width" 和 "tirm" 选项。
574 %
575 % 例如:\\
576 % \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
577 % \begin{tabular}{|c|c|c|} \hline
578 % \backslashbox[2cm]{num}{alpha}
579 % & A & B \\\hline
580 % 1 & A1 & B1 \\\hline
581 % 2 & A2 & B2 \\\hline
582 % \end{tabular}
583 % \end{SideBySideExample}
584 %
585 % \section{已知问题和未来版本}
586 %
587 % 已知问题:
588 % \begin{itemize}
589 % \item "\slashbox" 与 "\backslashbox" 命令的效果与在 \pkg{slashbox} 宏包中不
590 % 同。两个宏包在计算盒子宽度和高度时,使用了不同的算法;同时,在处理
591 % "\slashbox" 第二个可选参数(即 "\diagbox" 的 "trim" 键)时,使用的方式也不一
592 % 样。
593 %
594 % 这不是 bug。通常 \pkg{diagbox} 计算出的宽度比 \pkg{slashbox} 的结果更安全一
595 % 些。
596 %
597 % \item 有两条斜线的三部分表头,其所采用的自动布局算法是受限的。特别是,最左的
598 % 部分不能过宽,最右的部分不能过高,否则将无法计算出合理的结果,宏包会发出警
599 % 告。(感谢 Frank Mittelbach 指出该问题。)
600 %
601 % \item
602 % "\diagbox" 生成的单元格必须是表列中最宽的一个。如果不能达到最宽,则画出的斜
603 % 线不能保证在正确的位置。例如:\\[1ex]
604 % \begin{SideBySideExample}[frame=single,numbers=left,xrightmargin=.45\linewidth]
605 % \begin{tabular}{|c|} \hline
606 % \diagbox{A}{B} \\\hline
607 % Very long term \\\hline
608 % \end{tabular}
609 % \end{SideBySideExample}
610 % \\[1ex]
611 % 此时可以手工设置较宽的 "\diagbox" 的 "width" 选项,解决此问题。
612 % \end{itemize}
613 %
614 % \StopEventually{}
615 %
616 %
617 % \clearpage
618 % \section{Implementation / 代码实现}
619 %
620 % \iffalse
621 %<*package>
622 % \fi
623 %
624 % \subsection{实现依赖的工具包}
625 %
626 % 使用 key-value 界面。
627 % \begin{macrocode}
628 \RequirePackage{keyval}
629 % \end{macrocode}
630 % 绘图依赖 \pkg{pict2e} 宏包。
631 % \begin{macrocode}
632 \RequirePackage{pict2e}
633 % \end{macrocode}
634 % \changes{v2.2}{2016/12/28}{避免 \pkg{fp} 包的 \texttt{nomessages} 选项在使用
635 % \pkg{catoptions} 包时冲突}
636 % 长度计算 \pkg{calc} 宏包。
637 % \changes{v2.2}{2016/12/28}{使用 \pkg{calc} 包计算选项参数,以支持
638 % \cs{widthof} 等命令。}
639 % \begin{macrocode}
640 \RequirePackage{calc}
641 % \end{macrocode}
642 % 内部控制 \pkg{array} 宏包。
643 % \begin{macrocode}
644 \RequirePackage{array}
645 % \end{macrocode}
646 %
647 %
648 % \subsection{资源分配}
649 %
650 % 分配用到的盒子寄存器。它们分别对应于 "\diagbox" 三个必选参数的内容。
651 % \begin{macrocode}
652 \newbox\diagbox@boxa
653 \newbox\diagbox@boxb
654 \newbox\diagbox@boxm
655 % \end{macrocode}
656 % 分配长度变量。
657 % \begin{macrocode}
658 \newdimen\diagbox@wd
659 \newdimen\diagbox@ht
660 \newdimen\diagbox@insepl
661 \newdimen\diagbox@insepr
662 \newdimen\diagbox@outsepl
663 \newdimen\diagbox@outsepr
664 % \end{macrocode}
665 %
666 % \begin{macrocode}
667 \def\diagbox@clear{%
668 \diagbox@wd=\z@
669 \diagbox@ht=\z@
670 \diagbox@insepl=\tabcolsep
671 \diagbox@insepr=\tabcolsep
672 \diagbox@outsepl=-\tabcolsep
673 \diagbox@outsepr=-\tabcolsep
674 }
675 % \end{macrocode}
676 %
677 % \subsection{命令选项定义}
678 %
679 % 下面定义 "\diagbox" 的键值选项。
680 %
681 % \changes{v2.1}{2014/07/03}{允许在 \texttt{width} 和 \texttt{height} 的命令选项
682 % 中写表达式计算长度。}
683 % \changes{v2.1}{2016/02/24}{允许在 \texttt{height} 选项中使用
684 % \texttt{\textbackslash line} 表示行高。}
685 % 斜线盒子的总宽度。
686 % \begin{macrocode}
687 \define@key{diagbox}{width}{%
688 \unless\ifdim\diagbox@wd=\z@
689 \PackageWarning{diagbox}%
690 {You should not set width/innerwidth option more than once.}%
691 \fi
692 \setlength{\diagbox@wd}{#1}}
693 % \end{macrocode}
694 % 斜线盒子的总高度,参数中可使用 "\line" 表示行高。
695 % \begin{macrocode}
696 \define@key{diagbox}{height}{%
697 \let\diagbox@save@line\line
698 \def\line{\normalbaselineskip}%
699 \setlength{\diagbox@ht}{#1}%
700 \let\line\diagbox@save@line}
701 % \end{macrocode}
702 %
703 % \changes{v2.1}{2014/08/15}{盒子内容与斜线框距离 \texttt{innerleftsep},
704 % \texttt{innerrightsep} 命令选项。}
705 % 盒子内容与斜线框左右的距离。
706 % \begin{macrocode}
707 \define@key{diagbox}{innerleftsep}{%
708 \setlength{\diagbox@insepl}{#1}}
709 \define@key{diagbox}{innerrightsep}{%
710 \setlength{\diagbox@insepr}{#1}}
711 % \end{macrocode}
712 %
713 % \changes{v2.1}{2016/02/24}{盒子内容宽度 \texttt{innerwidth} 命令选项。}
714 % 设置盒子内容的宽度(与列格式 "p{宽度}" 对应)。盒子内容宽度加上盒子与斜线框
715 % 左右距离之和应为斜线盒子总宽度,即有
716 % \[
717 % "innerleftsep" + "innerwidth" + "innerrightsep" = "width".
718 % \]
719 % "innerwidth" 选项将通过设置盒子总宽度,维护上面的关系式。
720 % \begin{macrocode}
721 \define@key{diagbox}{innerwidth}{%
722 \unless\ifdim\diagbox@wd=\z@
723 \PackageWarning{diagbox}%
724 {You should not set width/innerwidth option more than once.}%
725 \fi
726 \setlength{\diagbox@wd}{#1+\diagbox@insepl+\diagbox@insepr}}
727 % \end{macrocode}
728 %
729 % \changes{v2.1}{2014/08/15}{斜线盒子与表格单元边框距离 \texttt{outerleftsep},
730 % \texttt{outerrightsep} 命令选项。}
731 % 斜线盒子与表格单元边框距离。该外部间距应为实际表列内容与列分隔线之间距离的相
732 % 反数,以此保证斜线与表格竖线能相接,即应输入参数保证
733 % \begin{align*}
734 % "outerleftsep" + "LEFTtabcolsep" &= 0\,\mathrm{pt}, \\
735 % "outerrightsep" + "RIGHTtabcolsep" &= 0\,\mathrm{pt}.
736 % \end{align*}
737 % \begin{macrocode}
738 \define@key{diagbox}{outerleftsep}{%
739 \setlength{\diagbox@outsepl}{#1}}
740 \define@key{diagbox}{outerrightsep}{%
741 \setlength{\diagbox@outsepr}{#1}}
742 % \end{macrocode}
743 %
744 % \changes{v2.1}{2014/08/15}{左右边距 \texttt{leftsep}, \texttt{rightsep} 命令
745 % 选项。}
746 % 设置左右边距,它将同时设置盒子内容与斜线框的内部间距,以及斜线盒子与表格单元
747 % 边框的外部间距。并保持关系:
748 % \begin{align*}
749 % "innerleftsep" &:= "leftsep", & "innerrightsep" &:= "rightsep", \\
750 % "outerleftsep" &:= -"leftsep", & "outerrightsep" &:= -"rightsep".
751 % \end{align*}
752 % \begin{macrocode}
753 \define@key{diagbox}{leftsep}{%
754 \setlength{\diagbox@insepl}{#1}%
755 \setlength{\diagbox@outsepl}{(#1)*-1}}
756 \define@key{diagbox}{rightsep}{%
757 \setlength{\diagbox@insepr}{#1}%
758 \setlength{\diagbox@outsepr}{(#1)*-1}}
759 % \end{macrocode}
760 %
761 % 盒子计算边界时是否忽略左右的空白。"trim=l" 效果等同于 "leftsep=0pt";"trim=r"
762 % 效果等同于 "rightsep=0pt"。
763 % \begin{macrocode}
764 \define@key{diagbox}{trim}{%
765 \@tfor\@reserveda:=#1\do{%
766 \ifcsname diagbox@insep\@reserveda\endcsname
767 \setlength{\csname diagbox@insep\@reserveda\endcsname}{\z@}%
768 \setlength{\csname diagbox@outsep\@reserveda\endcsname}{\z@}%
769 \else
770 \PackageError{diagbox}{Unknown trim option `#1'.}{l, r, lr and rl are supported.}%
771 \fi}}
772 % \end{macrocode}
773 %
774 % 盒子的方向。
775 % \begin{macrocode}
776 \define@key{diagbox}{dir}{%
777 \def\diagbox@dir{#1}%
778 \unless\ifcsname diagbox@dir@#1\endcsname
779 \PackageError{diagbox}{Unknown direction `#1'.}{NW, NE, SW, SE are supported.}%
780 \def\diagbox@dia{NW}%
781 \fi}
782 \let\diagbox@dir@SE\relax
783 \let\diagbox@dir@SW\relax
784 \let\diagbox@dir@NE\relax
785 \let\diagbox@dir@NW\relax
786 % \end{macrocode}
787 %
788 % \changes{v2.1}{2014/07/03}{增加命令选项 \texttt{linewidth}, \texttt{linecolor}
789 % 设置斜线宽度与颜色。}
790 % 斜线宽度与颜色。
791 % \begin{macrocode}
792 \define@key{diagbox}{linewidth}{%
793 \setlength{\@tempskipa}{#1}%
794 \linethickness{\@tempskipa}}
795 \define@key{diagbox}{linecolor}{%
796 \def\diagbox@setlinecolor{\color{#1}}}
797 \let\diagbox@setlinecolor\empty
798 % \end{macrocode}
799 %
800 % \changes{v2.1}{2014/07/03}{增加命令选项 \texttt{font} 支持设置内容字体。}
801 % 设置内容字体。
802 % \begin{macrocode}
803 \define@key{diagbox}{font}{%
804 \def\diagbox@font{#1}}
805 \let\diagbox@font\empty
806 % \end{macrocode}
807 %
808 % \subsection{绘制斜线盒子}
809 %
810 % \begin{macro}{\diagbox@pict}
811 % 这是带斜线的盒子本身。由一个 "picture" 环境实现。
812 % \begin{macrocode}
813 \def\diagbox@pict{%
814 \unitlength\p@
815 \begin{picture}
816 (\strip@pt\dimexpr\diagbox@wd+\diagbox@outsepl+\diagbox@outsepr\relax,\strip@pt\diagbox@ht)
817 (\strip@pt\dimexpr-\diagbox@outsepl\relax,0)
818 \@nameuse{diagbox@\diagbox@part @pict@\diagbox@dir}
819 \end{picture}}
820 % \end{macrocode}
821 % \end{macro}
822 %
823 %
824 % \begin{macro}{\diagbox@double@pict@SE}
825 % 方向为 "SE" 的斜线盒子内容。
826 % \begin{macrocode}
827 \def\diagbox@double@pict@SE{%
828 \put(0,0) {\makebox(0,0)[bl]{\box\diagbox@boxa}}
829 \put(\strip@pt\diagbox@wd,\strip@pt\diagbox@ht) {\makebox(0,0)[tr]{\box\diagbox@boxb}}
830 \diagbox@setlinecolor
831 \Line(0,\strip@pt\diagbox@ht)(\strip@pt\diagbox@wd,0)}
832 % \end{macrocode}
833 % \end{macro}
834 %
835 % \begin{macro}{\diagbox@double@pict@NW}
836 % 方向 "NW" 与 "SE" 相同。
837 % \begin{macrocode}
838 \let\diagbox@double@pict@NW\diagbox@double@pict@SE
839 % \end{macrocode}
840 % \end{macro}
841 %
842 % \begin{macro}{\diagbox@double@pict@NE}
843 % 方向为 "NE" 的斜线盒子内容。
844 % \begin{macrocode}
845 \def\diagbox@double@pict@NE{%
846 \put(0,\strip@pt\diagbox@ht) {\makebox(0,0)[tl]{\box\diagbox@boxa}}
847 \put(\strip@pt\diagbox@wd,0) {\makebox(0,0)[br]{\box\diagbox@boxb}}
848 \diagbox@setlinecolor
849 \Line(0,0)(\strip@pt\diagbox@wd,\strip@pt\diagbox@ht)}
850 % \end{macrocode}
851 % \end{macro}
852 %
853 % \begin{macro}{\diagbox@double@pict@NE}
854 % 方向 "SW" 与 "NE" 相同。
855 % \begin{macrocode}
856 \let\diagbox@double@pict@SW\diagbox@double@pict@NE
857 % \end{macrocode}
858 % \end{macro}
859 %
860 %
861 % \begin{macro}{\diagbox@double}
862 % \changes{v2.0}{2011/11/22}{在使用 "trim" 选项时去掉内容与盒子边界的间距。这
863 % 与 \pkg{slashbox} 的行为不同。}
864 % 分成两部分的盒子。三个参数,分别为 key-value 格式的可选项、左半边内容、右半边
865 % 内容。这里的主要工作是读入参数并计算斜线盒子的大小。
866 %
867 % 这里自动计算斜线盒子大小的算法为:斜线盒子的宽度取两个子盒宽度较大值的二倍,
868 % 而高度则直接取两个子盒的总高度。
869 % \begin{macrocode}
870 \def\diagbox@double#1#2#3{%
871 \begingroup
872 \diagbox@clear
873 \def\diagbox@part{double}%
874 \setkeys{diagbox}{dir=NW,#1}%
875 \setbox\diagbox@boxa=\hbox{%
876 \begin{tabular}{@{\hspace{\diagbox@insepl}}>{\diagbox@font}l@{}}
877 #2
878 \end{tabular}}%
879 \setbox\diagbox@boxb=\hbox{%
880 \begin{tabular}{@{}>{\diagbox@font}r@{\hspace{\diagbox@insepr}}}
881 #3
882 \end{tabular}}%
883 \ifdim\diagbox@wd=\z@
884 \ifdim\wd\diagbox@boxa>\wd\diagbox@boxb
885 \diagbox@wd=\dimexpr2\wd\diagbox@boxa+\diagbox@insepl+\diagbox@insepr\relax
886 \else
887 \diagbox@wd=\dimexpr2\wd\diagbox@boxb+\diagbox@insepl+\diagbox@insepr\relax
888 \fi
889 \fi
890 \ifdim\diagbox@ht=\z@
891 \diagbox@ht=\dimexpr\ht\diagbox@boxa+\dp\diagbox@boxa+\ht\diagbox@boxb+\dp\diagbox@boxb\relax
892 \fi
893 $\vcenter{\hbox{\diagbox@pict}}$%
894 \endgroup}
895 % \end{macrocode}
896 % \end{macro}
897 %
898 % \begin{macro}{\diagbox@triple@setbox@NW}
899 % \begin{macrocode}
900 \def\diagbox@triple@setbox@NW#1#2#3{%
901 \setbox\diagbox@boxa=\hbox{%
902 \begin{tabular}{@{\hspace{\diagbox@insepl}}>{\diagbox@font}l@{}}
903 #1
904 \end{tabular}}%
905 \setbox\diagbox@boxm=\hbox{%
906 \begin{tabular}{@{\hspace{\diagbox@insepl}}>{\diagbox@font}l@{}}
907 #2
908 \end{tabular}}%
909 \setbox\diagbox@boxb=\hbox{%
910 \begin{tabular}{@{}>{\diagbox@font}r@{\hspace{\diagbox@insepr}}}
911 #3
912 \end{tabular}}}
913 % \end{macrocode}
914 % \end{macro}
915 %
916 % \begin{macro}{\diagbox@triple@setbox@SW}
917 % \begin{macrocode}
918 \let\diagbox@triple@setbox@SW\diagbox@triple@setbox@NW
919 % \end{macrocode}
920 % \end{macro}
921 %
922 % \begin{macro}{\diagbox@triple@setbox@SE}
923 % \begin{macrocode}
924 \def\diagbox@triple@setbox@SE#1#2#3{%
925 \setbox\diagbox@boxa=\hbox{%
926 \begin{tabular}{@{\hspace{\diagbox@insepl}}>{\diagbox@font}l@{}}
927 #1
928 \end{tabular}}%
929 \setbox\diagbox@boxm=\hbox{%
930 \begin{tabular}{@{}>{\diagbox@font}r@{\hspace{\diagbox@insepr}}}
931 #2
932 \end{tabular}}%
933 \setbox\diagbox@boxb=\hbox{%
934 \begin{tabular}{@{}>{\diagbox@font}r@{\hspace{\diagbox@insepr}}}
935 #3
936 \end{tabular}}}
937 % \end{macrocode}
938 % \end{macro}
939 %
940 % \begin{macro}{\diagbox@triple@setbox@NE}
941 % \begin{macrocode}
942 \let\diagbox@triple@setbox@NE\diagbox@triple@setbox@SE
943 % \end{macrocode}
944 % \end{macro}
945 %
946 %
947 % \begin{macro}{\diagbox@triple@pict@NW}
948 % \begin{macrocode}
949 \def\diagbox@triple@pict@NW{%
950 \put(0,0) {\makebox(0,0)[bl]{\box\diagbox@boxa}}
951 \put(0,\y) {\makebox(0,0)[tl]{\box\diagbox@boxm}}
952 \put(\x,\y) {\makebox(0,0)[tr]{\box\diagbox@boxb}}
953 \diagbox@setlinecolor
954 \Line(0,\yym)(\x,0)
955 \Line(\xm,\y)(\x,0)}
956 % \end{macrocode}
957 % \end{macro}
958 %
959 %
960 % \begin{macro}{\diagbox@triple@pict@NE}
961 % \begin{macrocode}
962 \def\diagbox@triple@pict@NE{%
963 \put(0,\y) {\makebox(0,0)[tl]{\box\diagbox@boxa}}
964 \put(\x,\y) {\makebox(0,0)[tr]{\box\diagbox@boxm}}
965 \put(\x,0) {\makebox(0,0)[br]{\box\diagbox@boxb}}
966 \diagbox@setlinecolor
967 \Line(0,0)(\xxm,\y)
968 \Line(0,0)(\x,\yym)}
969 % \end{macrocode}
970 % \end{macro}
971 %
972 %
973 % \begin{macro}{\diagbox@triple@pict@SW}
974 % \begin{macrocode}
975 \def\diagbox@triple@pict@SW{%
976 \put(0,\y) {\makebox(0,0)[tl]{\box\diagbox@boxa}}
977 \put(0,0) {\makebox(0,0)[bl]{\box\diagbox@boxm}}
978 \put(\x,0) {\makebox(0,0)[br]{\box\diagbox@boxb}}
979 \diagbox@setlinecolor
980 \Line(0,\ym)(\x,\y)
981 \Line(\xm,0)(\x,\y)}
982 % \end{macrocode}
983 % \end{macro}
984 %
985 %
986 % \begin{macro}{\diagbox@triple@pict@SE}
987 % \begin{macrocode}
988 \def\diagbox@triple@pict@SE{%
989 \put(0,0) {\makebox(0,0)[bl]{\box\diagbox@boxa}}
990 \put(\x,0) {\makebox(0,0)[br]{\box\diagbox@boxm}}
991 \put(\x,\y) {\makebox(0,0)[tr]{\box\diagbox@boxb}}
992 \diagbox@setlinecolor
993 \Line(0,\y)(\xxm,0)
994 \Line(0,\y)(\x,\ym)}
995 % \end{macrocode}
996 % \end{macro}
997 %
998 %
999 % \begin{macro}{\diagbox@triplebox}
1000 % \changes{v2.0}{2011/11/22}{新增三部分双斜线的盒子}
1001 % \changes{v2.3}{2020/02/09}{检测二次方程无解、异常解的情形}
1002 % 分成三部分的盒子。四个参数,分别为 key-value 格式的可选项、左半边内容、中间
1003 % 内容、右半边内容。
1004 % \begin{macrocode}
1005 \def\diagbox@triple#1#2#3#4{%
1006 \begingroup
1007 \diagbox@clear
1008 \def\diagbox@part{triple}%
1009 \setkeys{diagbox}{dir=NW,#1}%
1010 \@nameuse{diagbox@triple@setbox@\diagbox@dir}{#2}{#3}{#4}%
1011 % \end{macrocode}
1012 % 在宏包最后定义,需要返回 "\x", "\xm", "\xxm", "\y", "\ym" 和 "\yym"。
1013 % \begin{macrocode}
1014 \diagbox@solve@equations
1015 $\vcenter{\hbox{\diagbox@pict}}$%
1016 \endgroup}
1017 % \end{macrocode}
1018 % \end{macro}
1019 %
1020 % \subsection{用户命令}
1021 %
1022 % \begin{macro}{\diagbox}
1023 % \changes{v2.0}{2011/11/22}{判断参数个数,选择两部分或三部分盒子。}
1024 % 主要的用户命令。判断使用两部分还是三部分的盒子。
1025 % \begin{macrocode}
1026 \newcommand\diagbox[3][]{%
1027 \@ifnextchar\bgroup
1028 {\diagbox@triple{#1}{#2}{#3}}{\diagbox@double{#1}{#2}{#3}}}
1029 % \end{macrocode}
1030 % \end{macro}
1031 %
1032 % 以下代码用来模拟 \pkg{slashbox} 宏包的功能。
1033 %
1034 % 禁止读入 \pkg{slashbox}。
1035 % \begin{macrocode}
1036 \expandafter\xdef\csname ver@slashbox.\@pkgextension\endcsname{9999/99/99}
1037 % \end{macrocode}
1038 %
1039 %
1040 % \begin{macro}{\slashbox}
1041 % 模拟 "\slashbox"。
1042 % \begin{macrocode}
1043 \def\slashbox{%
1044 \def\diagbox@slashbox@options{dir=SW,}%
1045 \slashbox@}
1046 % \end{macrocode}
1047 % \end{macro}
1048 %
1049 %
1050 % \begin{macro}{\backslashbox}
1051 % 模拟 "\backslashbox"。
1052 % \begin{macrocode}
1053 \def\backslashbox{%
1054 \def\diagbox@slashbox@options{dir=NW,}%
1055 \slashbox@}
1056 % \end{macrocode}
1057 % \end{macro}
1058 %
1059 %
1060 % \begin{macro}{\slashbox@}
1061 % \begin{macrocode}
1062 \newcommand\slashbox@[1][]{%
1063 \ifx\relax#1\relax\else
1064 \edef\diagbox@slashbox@options{%
1065 \unexpanded\expandafter{\diagbox@slashbox@options}%
1066 \unexpanded{width=#1,}}%
1067 \fi
1068 \slashbox@@}
1069 % \end{macrocode}
1070 % \end{macro}
1071 %
1072 %
1073 % \begin{macro}{\slashbox@@}
1074 % \begin{macrocode}
1075 \newcommand\slashbox@@[3][]{%
1076 \edef\diagbox@slashbox@options{%
1077 \unexpanded\expandafter{\diagbox@slashbox@options}%
1078 \unexpanded{trim=#1,}}%
1079 \expandafter\diagbox\expandafter[\diagbox@slashbox@options]{#2}{#3}}
1080 % \end{macrocode}
1081 % \end{macro}
1082 %
1083 % \changes{v2.4}{2020/02/28}{利用 \pkg{l3fp} 解方程组。}
1084 %
1085 % \subsection{解方程组}
1086 %
1087 % 这里计算双斜线盒子宽、高的算法是简单而直观的。如下图所示,将 A、M、B 三个子盒
1088 % 子分别放在斜线盒的三个角后,斜线正好使 M 盒、A 或 B 盒、斜线盒的一角三点共
1089 % 线。
1090 % \[
1091 % \tabcolsep=0pt
1092 % \linespread{1}\selectfont
1093 % \begin{tabular}{|c|}\hline
1094 % \diagbox{\fbox{A}}{\fbox{M}}{\fbox{B}}\\ \hline
1095 % \end{tabular}
1096 % \]
1097 % 在 A、M、B 三个盒子内容确定后,斜线盒的宽、高即可通过相似三角形的比例关系求
1098 % 解。设斜线盒的宽、高为 $x$, $y$,而 A、M、B 盒的宽、高分别为 $(x_a, y_a)$,
1099 % $(x_m, y_m)$, $(x_b, y_b)$。则有:
1100 % \begin{align*}
1101 % \frac{x}{x_a} &= \frac{y - y_m}{y - y_m - y_a}, \\
1102 % \frac{y}{y_b} &= \frac{x - x_m}{x - x_m - x_b}.
1103 % \end{align*}
1104 % 该方程组通常可化简为一个一元二次方程求解,两组共轭根中可以只需要较大的一组。
1105 % 通过 Mathematica 软件,容易得到方程组的解为:
1106 % \begin{align*}
1107 % x & = \frac{u + v \pm \sqrt{\Delta}} {2(t - y_b)}, \\
1108 % y & = \frac{u - v \mp \sqrt{\Delta}} {2(x_a - s)}, \\
1109 % \Delta & := (u + v)^2 + 4 x_a (t - y_b) \bigl(x_m (y_b - y_m) - x_b y_m\bigr), \\
1110 % u & := x_a y_m - x_m y_b, \\
1111 % v & := st - x_a y_b, \\
1112 % s & := x_b + x_m, \\
1113 % t & := y_a + y_m.
1114 % \end{align*}
1115 % 但须注意上面的方程组并非总有正的实根,经过简单的代数分析可知,要给出几何直观
1116 % 的可行解(正实根),需要同时满足以下条件:
1117 % \begin{align*}
1118 % x_a &< x_m + x_b, \\
1119 % y_b &< y_m + y_a.
1120 % \end{align*}
1121 % 该条件需要在计算中予以检查。
1122 %
1123 % \begin{macro}{\diagbox@solve@equations}
1124 % 如果 \pkg{expl3} 环境可用,就使用 \pkg{l3fp} 计算,否则使用 \pkg{fp} 包。
1125 % \begin{macrocode}
1126 \ifcsname\detokenize{fp_eval:n}\endcsname
1127 \csname fi\endcsname
1128 \ExplSyntaxOn
1129 %<@@=diagbox>
1130 \cs_new_protected:Npn \@@_solve_equations:
1131 {
1132 % \end{macrocode}
1133 % 取长宽。
1134 % \begin{macrocode}
1135 \fp_set:Nn \l_@@_xa_fp
1136 { \dim_to_fp:n { \wd \diagbox@boxa } }
1137 \fp_set:Nn \l_@@_ya_fp
1138 { \dim_to_fp:n { \ht \diagbox@boxa + \dp \diagbox@boxa } }
1139 \fp_set:Nn \l_@@_xb_fp
1140 { \dim_to_fp:n { \wd \diagbox@boxb } }
1141 \fp_set:Nn \l_@@_yb_fp
1142 { \dim_to_fp:n { \ht \diagbox@boxb + \dp \diagbox@boxb } }
1143 \fp_set:Nn \l_@@_xm_fp
1144 { \dim_to_fp:n { \wd \diagbox@boxm } }
1145 \fp_set:Nn \l_@@_ym_fp
1146 { \dim_to_fp:n { \ht \diagbox@boxm + \dp \diagbox@boxm } }
1147 \fp_set:Nn \l_@@_s_fp { \l_@@_xb_fp + \l_@@_xm_fp }
1148 \fp_set:Nn \l_@@_t_fp { \l_@@_ya_fp + \l_@@_ym_fp }
1149 % \end{macrocode}
1150 % 如果宽度和长度都被指定了,就不需要列方程组求解。
1151 % \begin{macrocode}
1152 \fp_set_eq:NN \l_@@_delta_fp \c_nan_fp
1153 \dim_compare:nNnTF \diagbox@wd = \c_zero_dim
1154 { \@@_calculate_width: }
1155 { \fp_set:Nn \l_@@_x_fp { \dim_to_fp:n { \diagbox@wd } } }
1156 \dim_compare:nNnTF \diagbox@ht = \c_zero_dim
1157 { \@@_calculate_height: }
1158 { \fp_set:Nn \l_@@_y_fp { \dim_to_fp:n { \diagbox@ht } } }
1159 \edef \x { \fp_use:N \l_@@_x_fp }
1160 \edef \y { \fp_use:N \l_@@_y_fp }
1161 \edef \xm { \fp_use:N \l_@@_xm_fp }
1162 \edef \ym { \fp_use:N \l_@@_ym_fp }
1163 \edef \xxm { \fp_eval:n { \l_@@_x_fp - \l_@@_xm_fp } }
1164 \edef \yym { \fp_eval:n { \l_@@_y_fp - \l_@@_ym_fp } }
1165 }
1166 % \end{macrocode}
1167 % 检查可行解条件,解二次方程。这里对于无正实根的情形,会给出一个宽松的值作为斜
1168 % 线盒子的宽高:
1169 % \begin{align*}
1170 % x &= 2 \max(x_a + x_m, x_b + x_m),\\
1171 % y &= 2 \max(y_a + y_m, y_b + y_m).
1172 % \end{align*}
1173 % \begin{macrocode}
1174 \cs_new_protected:Npn \@@_calculate_width:
1175 {
1176 \fp_zero:N \l_@@_x_fp
1177 \fp_compare:nNnT \l_@@_yb_fp < \l_@@_t_fp
1178 {
1179 \@@_calculate_coefficient:
1180 \fp_if_nan:nF { \l_@@_sqrt_fp }
1181 {
1182 \fp_set:Nn \l_@@_x_fp
1183 {
1184 ( \l_@@_u_fp + \l_@@_v_fp + \l_@@_sqrt_fp ) /
1185 ( 2 ( \l_@@_t_fp - \l_@@_yb_fp ) )
1186 }
1187 }
1188 }
1189 \fp_compare:nNnF \l_@@_x_fp > \c_zero_fp
1190 {
1191 \fp_set:Nn \l_@@_x_fp
1192 { 2 ( max( \l_@@_xa_fp , \l_@@_xb_fp ) + \l_@@_xm_fp ) }
1193 \msg_warning:nnxx { diagbox } { calculate-error }
1194 { width } { \fp_use:N \l_@@_x_fp }
1195 }
1196 \diagbox@wd = \fp_to_dim:N \l_@@_x_fp \scan_stop:
1197 }
1198 \cs_new_protected:Npn \@@_calculate_height:
1199 {
1200 \fp_zero:N \l_@@_y_fp
1201 \fp_compare:nNnT \l_@@_xa_fp < \l_@@_s_fp
1202 {
1203 \@@_calculate_coefficient:
1204 \fp_if_nan:nF { \l_@@_sqrt_fp }
1205 {
1206 \fp_set:Nn \l_@@_y_fp
1207 {
1208 ( \l_@@_u_fp - \l_@@_v_fp - \l_@@_sqrt_fp ) /
1209 ( 2 ( \l_@@_xa_fp - \l_@@_s_fp ) )
1210 }
1211 }
1212 }
1213 \fp_compare:nNnF \l_@@_y_fp > \c_zero_fp
1214 {
1215 \fp_set:Nn \l_@@_y_fp
1216 { 2 ( max( \l_@@_ya_fp , \l_@@_yb_fp ) + \l_@@_ym_fp ) }
1217 \msg_warning:nnxx { diagbox } { calculate-error }
1218 { height } { \fp_use:N \l_@@_y_fp }
1219 }
1220 \diagbox@ht = \fp_to_dim:N \l_@@_y_fp \scan_stop:
1221 }
1222 \cs_new_protected:Npn \@@_calculate_coefficient:
1223 {
1224 \fp_if_nan:nT { \l_@@_delta_fp }
1225 { \@@_calculate_coefficient_aux: }
1226 }
1227 \cs_new_protected:Npn \@@_calculate_coefficient_aux:
1228 {
1229 \fp_set:Nn \l_@@_u_fp
1230 {
1231 \l_@@_xa_fp * \l_@@_ym_fp -
1232 \l_@@_xm_fp * \l_@@_yb_fp
1233 }
1234 \fp_set:Nn \l_@@_v_fp
1235 {
1236 \l_@@_s_fp * \l_@@_t_fp -
1237 \l_@@_xa_fp * \l_@@_yb_fp
1238 }
1239 \fp_set:Nn \l_@@_delta_fp
1240 {
1241 ( \l_@@_u_fp + \l_@@_v_fp )^2 +
1242 4 * \l_@@_xa_fp * ( \l_@@_t_fp - \l_@@_yb_fp ) *
1243 ( \l_@@_xm_fp * ( \l_@@_yb_fp - \l_@@_ym_fp ) -
1244 \l_@@_xb_fp * \l_@@_ym_fp )
1245 }
1246 \fp_compare:nNnTF \l_@@_delta_fp < \c_zero_fp
1247 { \fp_set_eq:NN \l_@@_sqrt_fp \c_nan_fp }
1248 { \fp_set:Nn \l_@@_sqrt_fp { sqrt(\l_@@_delta_fp) } }
1249 }
1250 \cs_new_eq:NN \diagbox@solve@equations \@@_solve_equations:
1251 \fp_new:N \l_@@_xa_fp
1252 \fp_new:N \l_@@_ya_fp
1253 \fp_new:N \l_@@_xb_fp
1254 \fp_new:N \l_@@_yb_fp
1255 \fp_new:N \l_@@_xm_fp
1256 \fp_new:N \l_@@_ym_fp
1257 \fp_new:N \l_@@_x_fp
1258 \fp_new:N \l_@@_y_fp
1259 \fp_new:N \l_@@_s_fp
1260 \fp_new:N \l_@@_t_fp
1261 \fp_new:N \l_@@_u_fp
1262 \fp_new:N \l_@@_v_fp
1263 \fp_new:N \l_@@_sqrt_fp
1264 \fp_new:N \l_@@_delta_fp
1265 \msg_new:nnn { diagbox } { calculate-error }
1266 {
1267 Cannot~calculate~proper~#1~of~triple~diagbox~
1268 \msg_line_context:. \\
1269 Use~#2pt~instead.
1270 }
1271 \file_input_stop:
1272 % \end{macrocode}
1273 %
1274 % 若 \pkg{l3fp} 不可用,则引入 \pkg{fp} 包计算。
1275 % \begin{macrocode}
1276 \fi
1277 \RequirePackage{fp}
1278 \FPmessagesfalse
1279 \def\diagbox@solve@equations{%
1280 % \end{macrocode}
1281 % 取长宽
1282 % \begin{macrocode}
1283 \edef\xa{\strip@pt\wd\diagbox@boxa}%
1284 \edef\ya{\strip@pt\dimexpr\ht\diagbox@boxa+\dp\diagbox@boxa\relax}%
1285 \edef\xb{\strip@pt\wd\diagbox@boxb}%
1286 \edef\yb{\strip@pt\dimexpr\ht\diagbox@boxb+\dp\diagbox@boxb\relax}%
1287 \edef\xm{\strip@pt\wd\diagbox@boxm}%
1288 \edef\ym{\strip@pt\dimexpr\ht\diagbox@boxm+\dp\diagbox@boxm\relax}%
1289 % \end{macrocode}
1290 % 列方程,计算方程系数
1291 % \begin{macrocode}
1292 \FPneg\bi\yb
1293 \FPadd\ci\xb\xm \FPneg\ci\ci
1294 \FPmul\di\xm\yb
1295 \FPadd\bj\ya\ym \FPneg\bj\bj
1296 \FPneg\cj\xa
1297 \FPmul\dj\xa\ym
1298 % \end{macrocode}
1299 % 检查可行解条件,解二次方程。这里对于无正实根的情形,会给出一个宽松的值作为斜
1300 % 线盒子的宽高:
1301 % \begin{align*}
1302 % x &= 2 \max(x_a + x_m, x_b + x_m),\\
1303 % y &= 2 \max(y_a + y_m, y_b + y_m).
1304 % \end{align*}
1305 % \begin{macrocode}
1306 \FPsub\u\dj\di
1307 \FPupn{v}{bj ci * bi cj * -}%
1308 \FPupn{delta}{bi dj * bj di * - cj ci - * 4 * v u + copy * -}%
1309 \newif\ifdeltapositive
1310 \FPifneg\delta \deltapositivefalse \else \deltapositivetrue \fi
1311 \FPset\x{0}%
1312 \FPset\y{0}%
1313 \ifdim\diagbox@wd=\z@
1314 \ifdim\bi\p@>\bj\p@\ifdeltapositive
1315 \FPupn{x}{2 bj bi - 2 delta root v u - + / /}%
1316 \fi\fi
1317 \ifdim\x\p@=\z@
1318 \FPupn{x}{xa xm + xb xm + max 2 *}%
1319 \PackageWarning{diagbox}{Cannot calculate proper width of triple diagbox.\MessageBreak
1320 Use \x pt instead.}%
1321 \fi
1322 \diagbox@wd=\x\p@
1323 \else
1324 \edef\x{\strip@pt\diagbox@wd}%
1325 \fi
1326 \ifdim\diagbox@ht=\z@
1327 \ifdim\ci\p@<\cj\p@\ifdeltapositive
1328 \FPupn{y}{2 cj ci - 2 delta root v u + - / /}%
1329 \fi\fi
1330 \ifdim\y\p@=\z@
1331 \FPupn{y}{ya ym + yb ym + max 2 *}%
1332 \PackageWarning{diagbox}{Cannot calculate proper height of triple diagbox.\MessageBreak
1333 Use \y pt instead.}%
1334 \fi
1335 \diagbox@ht=\y\p@
1336 \else
1337 \edef\y{\strip@pt\diagbox@ht}%
1338 \fi
1339 \FPsub\xxm\x\xm
1340 \FPsub\yym\y\ym
1341 }
1342 % \end{macrocode}
1343 % \end{macro}
1344 %
1345 % \iffalse
1346 %</package>
1347 % \fi
1348 %
1349 % \Finale
1350 \endinput