免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
Board logo

标题: 全包围矩阵 [打印本页]

作者: kuing    时间: 2017-9-18 14:08     标题: 全包围矩阵

问题来自:http://bbs.ctex.org/forum.php?mod=viewthread&tid=154094
要输入这样的矩阵:
微信截图_20170915202442.png
2017-9-18 14:03


方法一:用 http://www.latexstudio.net/archives/643 里提供的 qbordermatrix 包。

方法二:就是我在原帖中的 tikz 画法:
  1. \documentclass{article}
  2. \usepackage{amsmath,amssymb}
  3. \usepackage{tikz}
  4. \usetikzlibrary{matrix,calc}
  5. \begin{document}
  6. \begin{tikzpicture}[baseline=(m-3-3.base),every node/.style={inner sep=0pt}]
  7. \matrix (m) [matrix of math nodes,column sep=12pt,row sep=8pt]
  8. {
  9. \square & x & y & \lambda & c_i \\
  10. f_1 & 2^* & \square & 0^\circ & 2 \\
  11. f_2 & \square & 2^\circ & 0^* & 2 \\
  12. f_2 & 0^\circ & 0^* & \square & 0 \\
  13. d_j & 2 & 2 & 0 & \square \\
  14. };
  15. \coordinate (jiao1) at (m-2-2.north west);
  16. \coordinate (jiao2) at (m-4-4.south east);
  17. \node[rectangle,left delimiter=(,right delimiter=)]
  18. at ($(jiao1)!0.5!(jiao2)$)
  19. {
  20. \tikz{\path(jiao1)(jiao2);}
  21. };
  22. \end{tikzpicture}
  23. \end{document}
复制代码
效果:
QQ截图20170917172225.png
2017-9-18 14:08


用 tikz 画虽然麻烦些,但更自由些。

需要注意的是,括号的位置由 jiao1 和 jiao2 两个点来决定,它们实际上就是对角线的顶点。
上面的矩阵由于各元素宽度都差不多,所以取 m-2-2、m-4-4 或者 m-4-2、m-2-4 都是可以的。
而对于一般情形,还要看情况来取点。
像下面这样的情形就比较麻烦:
1 & 1 & 1 & 1 & 1 \\
2 & 2222222 & 2 & 2222222 & 2 \\
3 & 3 & 3 & 3 & 3 \\
4 & 4 & 4 & 4 & 4 \\
5 & 5 & 5 & 5 & 5 \\
最长的东西在同一行,无论取 m-2-2、m-4-4 还是 m-4-2、m-2-4 都不行,会是这样子:
QQ截图20170918143612.png
2017-9-18 14:37
   
QQ截图20170918143711.png
2017-9-18 14:37

这时得这样取:
\coordinate (jiao1) at (m-2-2.north west);
\coordinate (jiao2) at (m-4-4.south east-|m-2-4.east);
效果才是正确的:
QQ截图20170918144039.png
2017-9-18 14:40


图片附件: 微信截图_20170915202442.png (2017-9-18 14:03, 5.41 KB) / 下载次数 645
http://kuing.orzweb.net/attachment.php?aid=5319&k=14a3be216ec1330a90b292aefe31669b&t=1711616784&sid=xIAxDp



图片附件: QQ截图20170917172225.png (2017-9-18 14:08, 11.88 KB) / 下载次数 655
http://kuing.orzweb.net/attachment.php?aid=5320&k=3946328ba0cfaa87a5ab0147fe4bfc42&t=1711616784&sid=xIAxDp



图片附件: QQ截图20170918143612.png (2017-9-18 14:37, 2.34 KB) / 下载次数 616
http://kuing.orzweb.net/attachment.php?aid=5321&k=59f5f8976c0ecfcab52b272a50c5dfaf&t=1711616784&sid=xIAxDp



图片附件: QQ截图20170918143711.png (2017-9-18 14:37, 2.42 KB) / 下载次数 642
http://kuing.orzweb.net/attachment.php?aid=5322&k=9db395dbff9e99bb0f4a2c018f56ce6a&t=1711616784&sid=xIAxDp



图片附件: QQ截图20170918144039.png (2017-9-18 14:40, 2.32 KB) / 下载次数 656
http://kuing.orzweb.net/attachment.php?aid=5323&k=8e897efdaeadecbbce498b864726e1ba&t=1711616784&sid=xIAxDp


作者: isee    时间: 2017-9-18 22:43

tikz  画起图来真是棒的
作者: kuing    时间: 2017-9-20 14:39

本来想再演示更复杂的情况,比如在前三列上面加个花括号,于是照用同样的招式,在后面加上:
\coordinate (t1) at (m-1-1.north west);
\coordinate (t2) at (m-1-3.north east);
\node[rectangle,above delimiter=\{]
at (\$(t1)!0.5!(t2)\$)
{
\tikz{\path(t1)(t2);}
};
结果却出科意料:
QQ截图20170920135350.png
2017-9-20 13:55


为什么会向上偏移了呢?
研究了很久,通过各种测试,终于发现了问题所在,就是整个图指定了 baseline=(m-3-3.base),去掉它,就不会偏移。
这是为什么呢,因为这个 baseline 影响到了里面的 \tikz{\path(t1)(t2);},结果这个 \node 的大小除了要包含 (t1)(t2),还有 (m-3-3.base),而由于它在 (t1)(t2) 的下方,于是整个 \node 就变高了。
而1楼的没问题,正是因为 (m-3-3.base) 在 (jiao1)(jiao2) 之间,换言之,如果将1楼的两个点改成
\coordinate (jiao1) at (m-1-1.north west);
\coordinate (jiao2) at (m-2-2.south east);
同样会出问题:
QQ截图20170920142037.png
2017-9-20 14:21


但是,指定 baseline 是有必要的,它决定了与外面的东西如何对齐,不应该去掉,所以解决办法是将 \tikz{\path(t1)(t2);} 改成 \tikz[baseline=default]{\path(t1)(t2);}。

综上所述,可以写出以下的:
  1. \[
  2. f(x)=
  3. \begin{tikzpicture}[baseline=(m-3-3.base),every node/.style={inner sep=0pt}]
  4. \matrix (m) [matrix of math nodes,column sep=12pt,row sep=8pt]
  5. {
  6. \square & x & y & \lambda & c_i \\
  7. f_1 & 2^* & \square & 0^\circ & 2 \\
  8. f_2 & \square & 2^\circ & 0^* & 2 \\
  9. f_2 & 0^\circ & 0^* & \square & 0 \\
  10. d_j & 2 & 2 & 0 & \square \\
  11. };
  12. \coordinate (jiao1) at (m-2-2.north west);
  13. \coordinate (jiao2) at (m-4-4.south east);
  14. \node[rectangle,left delimiter=(,right delimiter=)]  
  15. at ($(jiao1)!0.5!(jiao2)$)  
  16. {
  17. \tikz[baseline=default]{\path(jiao1)(jiao2);}
  18. };
  19. \coordinate (jiao3) at (m-1-1.north west);
  20. \coordinate (jiao4) at (m-2-2.south east);
  21. \node[rectangle,left delimiter={[},right delimiter={]}]  
  22. at ($(jiao3)!0.5!(jiao4)$)  
  23. {
  24. \tikz[baseline=default]{\path(jiao3)(jiao4);}
  25. };
  26. \coordinate (t1) at (m-1-1.north west);
  27. \coordinate (t2) at (m-1-3.north east);
  28. \node[rectangle,above delimiter=\{] (tt)
  29. at ($(t1)!0.5!(t2)$)
  30. {
  31. \tikz[baseline=default]{\path(t1)(t2);}
  32. };
  33. \node[above=12pt] at (tt) {$t$};
  34. \end{tikzpicture}
  35. \]
复制代码
效果:
QQ截图20170920143905.png
2017-9-20 14:39


图片附件: QQ截图20170920135350.png (2017-9-20 13:55, 5.27 KB) / 下载次数 664
http://kuing.orzweb.net/attachment.php?aid=5337&k=bd0daffd8404dccfa33457d9525a4c30&t=1711616784&sid=xIAxDp



图片附件: QQ截图20170920142037.png (2017-9-20 14:21, 5.1 KB) / 下载次数 651
http://kuing.orzweb.net/attachment.php?aid=5338&k=3bef2dc2aff035f6913d91226c32b69a&t=1711616784&sid=xIAxDp



图片附件: QQ截图20170920143905.png (2017-9-20 14:39, 6.95 KB) / 下载次数 636
http://kuing.orzweb.net/attachment.php?aid=5339&k=a5bd9e693c8ab054754973e6828d39ac&t=1711616784&sid=xIAxDp


作者: 其妙    时间: 2017-10-3 22:37

tikz好不好学?
作者: kuing    时间: 2017-10-3 22:40

回复 4# 其妙

不好
作者: 其妙    时间: 2017-10-3 22:54

看你们搞得那么欢,还准备学呢,
作者: kuing    时间: 2020-3-9 15:19

昨天在这帖用的 fit,突然想起也可以用于本帖。
测试结果是:用 fit 完全不会出现上面说的各种问题,连代码也简化不少!

比如开头的,只需这样写:
  1. \documentclass{article}
  2. \usepackage{amsmath,amssymb}
  3. \usepackage{tikz}
  4. \usetikzlibrary{matrix,fit}
  5. \begin{document}
  6. \begin{tikzpicture}[baseline=(m-3-3.base),every node/.style={inner sep=0pt}]
  7. \matrix (m) [matrix of math nodes,column sep=12pt,row sep=8pt]
  8. {
  9. \square & x & y & \lambda & c_i \\
  10. f_1 & 2^* & \square & 0^\circ & 2 \\
  11. f_2 & \square & 2^\circ & 0^* & 2 \\
  12. f_2 & 0^\circ & 0^* & \square & 0 \\
  13. d_j & 2 & 2 & 0 & \square \\
  14. };
  15. \node[fit=(m-2-2)(m-4-4)(m-2-4)(m-4-2),
  16. left delimiter=(,right delimiter=)]{};
  17. \end{tikzpicture}
  18. \end{document}
复制代码
把四个角都 fit 起来,就完全不用管哪个长哪个短的问题,保证能完全包住。
也不用在 node 里面再来一个 \tikz 了。
(效果图没变,就不再重复截图了,下同)
3# 最后那个复杂点的,也只需这样写:
  1. \[
  2. f(x)=
  3. \begin{tikzpicture}[baseline=(m-3-3.base),every node/.style={inner sep=0pt}]
  4. \matrix (m) [matrix of math nodes,column sep=12pt,row sep=8pt]
  5. {
  6. \square & x & y & \lambda & c_i \\
  7. f_1 & 2^* & \square & 0^\circ & 2 \\
  8. f_2 & \square & 2^\circ & 0^* & 2 \\
  9. f_2 & 0^\circ & 0^* & \square & 0 \\
  10. d_j & 2 & 2 & 0 & \square \\
  11. };
  12. \node[fit=(m-2-2)(m-4-4)(m-4-2)(m-2-4),
  13. left delimiter=(,right delimiter=)]{};
  14. \node[fit=(m-1-1)(m-2-2)(m-2-1)(m-1-2),
  15. left delimiter={[},right delimiter={]}]{};
  16. \node[fit=(m-1-1)(m-1-3),label={[yshift=10pt]90:$t$},
  17. above delimiter=\{]{};
  18. \end{tikzpicture}
  19. \]
复制代码
简单太多了,也完全没有什么 baseline 的问题。




欢迎光临 悠闲数学娱乐论坛(第2版) (http://kuing.orzweb.net/) Powered by Discuz! 7.2