我想使用 tikz 绘制一棵简单的树,并将该树嵌入等式中:
\documentclass{article}
\usepackage[a4paper,margin=25mm,showframe]{geometry}
\usepackage{graphicx} % <--- For including graphics
\usepackage{listings} % <--- For typesetting verbatim code
\usepackage{tikz} % <--- For drawing the diagram
\usepackage{array,amsmath} % <--- For drawing the diagram
\usetikzlibrary{calc,positioning,shapes.geometric}
\begin{document}
\def\mytree{\tikz[inner sep=1pt] {
\node (T1) at (0,0) {$T_1$};
\node (T2) at (1,0) {$T_2$};
\node (INLJ) at (0.5,0.5) {\textsf{IndexJoin}};
\node (T3) at (1.5,0.5) {$T_3$};
\node (SMJ) at (1,1) {\small{\sf HashJoin}};
\node (T4) at (2,1) {$T_4$};
\node (HJ) at (1.5,1.5) {\sf HashJoin};
\draw (T1)--(INLJ)--(T2);
\draw (INLJ)--(SMJ)--(T3);
\draw (SMJ)--(HJ)--(T4);
}}
\[ \left( \mytree, 10.111 \right)\]
\end{document}
你可以看到大量空白元组的两个元素之下。我该如何修复这个问题?
另一个问题:有没有办法让“[逗号] 10.111”部分更美观,比如通过垂直对齐它与 tikz 图片的中间?
==============
编辑
感谢 Phelype 的帮助,我现在能够减少空格。然而,出现了一个相关的问题。我想\def
事先创建树的两个变体,然后使用它们——但 latex 给出了编译错误。如果我创建\def
一个变体,立即使用它,然后\def
使用另一个变体,则不会出现问题。为什么会这样?
代码正常(如果我同时使用这两棵树就会出错):
\def \tree1 {\tikz[inner sep=1pt, baseline=(T3.north)] {
\node (T1) at (0,0) {$T_1$};
\node (T2) at (1,0) {$T_2$};
\node (INLJ) at (0.5,0.5) {\small{\sffamily IndexJoin}};
\node (T3) at (1.5,0.5) {$T_3$};
\node (SMJ) at (1,1) {\small{\sffamily HashJoin}};
\node (T4) at (2,1) {$T_4$};
\node (HJ) at (1.5,1.5) {\small{\sffamily HashJoin}};
\draw (T1)--(INLJ)--(T2);
\draw (INLJ)--(SMJ)--(T3);
\draw (SMJ)--(HJ)--(T4);}}
\[ \left[ \tree1 , \{T1, T2, T3, T4\}; 10.111 \right]\]
\def \tree2 {\tikz[inner sep=1pt, baseline=(T3.south)] {
\node (T1) at (0,0) {$T_1$};
\node (T2) at (1,0) {$T_2$};
\node (INLJ) at (0.5,0.5) {\small{\sffamily IndexJoin}};
\node (T3) at (1.5,0.5) {$T_3$};
\node (SMJ) at (1,1) {\small{\sffamily HashJoin}};
\draw (T1)--(INLJ)--(T2);
\draw (INLJ)--(SMJ)--(T3);}}
\[ \left[ \tree2 , \{T1, T2, T3, T4\}; 10.111 \right]\]
答案1
我会baseline=(current bounding box.center)
用蒂克兹图片。(这并不重要,我也会使用tikzpicture
环境而不是\tikz
命令。)
除非我错过了什么,否则这两棵树几乎完全相同,只是第二棵树有两条额外的边。与其定义两个几乎相同的命令,为什么不创建一个\mytree
和\mytree*
变体呢?这很容易做到,使用\NewDocumentCommand
from解析。这样做的话,输出结果如下:
代码如下:
\documentclass{article}
\usepackage[a4paper,margin=25mm,showframe]{geometry}
\usepackage{graphicx} % <--- For including graphics
\usepackage{listings} % <--- For typesetting verbatim code
\usepackage{tikz} % <--- For drawing the diagram
\usepackage{array,amsmath} % <--- For drawing the diagram
\usetikzlibrary{calc,positioning,shapes.geometric}
\usepackage{xparse}
\NewDocumentCommand\mytree{s}{
\begin{tikzpicture}[inner sep=1pt, baseline=(current bounding box.center)]
\node (T1) at (0,0) {$T_1$};
\node (T2) at (1,0) {$T_2$};
\node (INLJ) at (0.5,0.5) {\textsf{IndexJoin}};
\node (T3) at (1.5,0.5) {$T_3$};
\node (SMJ) at (1,1) {\small{\sf HashJoin}};
\node (T4) at (2,1) {$T_4$};
\node (HJ) at (1.5,1.5) {\sf HashJoin};
\draw (T1)--(INLJ)--(T2);
\draw (INLJ)--(SMJ)--(T3);
\IfBooleanT{#1}{\draw (SMJ)--(HJ)--(T4);}
\end{tikzpicture}
}
\begin{document}
\[ \left[ \mytree*, \{T1, T2, T3, T4\}; 10.111 \right]\]
\[ \left[ \mytree, \{T1, T2, T3, T4\}; 10.111 \right]\]
\end{document}
in表示该宏有一个可选的-参数;也就是说,{s}
它可以用作或。在行的定义中\NewDocumentCommand\mytree{s}{...}
\mytree
*
\mytree
\mytree*
\mytree
\IfBooleanT{#1}{...}
表示如果#1
(存在且)是*
则执行{...}
。还有变体\IfBooleanTF
和\IfBooleanF
。参见解析文档以了解更多详细信息。