编辑

编辑

我需要在乳胶中绘制一个图形,我决定使用 tikzpicture 和 tabular 来使图表的所有层居中,但问题是生成的图表没有整齐地对齐。

这是我的代码:

\begin{figure} [ht!]
\begin{tabular}{|cc|}
\hline
\begin{tikzpicture}
    \node (is-root) {is} [sibling distance=1.5cm]
        child {node {X}}
        child {node {precise }};
    \path (is-root) +(0,-2.5\tikzleveldistance)
    node {\textit{}};
\end{tikzpicture}
& 
\begin{tikzpicture}
    \node (is-root) {is} [sibling distance=2cm]
        child {node {X}}
        child {node {and}[sibling distance=1.5cm]
        child {node {exact}}
        child {node {accurate}}
    };
    \path (is-root) +(0,-2.5\tikzleveldistance)
    node {\textit{}};
\end{tikzpicture}\\
\multicolumn{2}{|c|}
{$\Downarrow$}\\
\multicolumn{2}{|c|}
{\begin{tikzpicture}
    \node (is-root) {if} [sibling distance=3cm]
        child {node {is} [sibling distance=2cm]
        child {node {X}}
        child {node {precise }
        }}
        child {node {is} [sibling distance=2cm]
        child {node {X} }
        child {node {and}[sibling distance=1.5cm]
            child {node {exact}}
            child {node {accurate}}
        }};
 ;

     \path (is-root) +(0,-2.5\tikzleveldistance)
     node {\textit{}};
\end{tikzpicture}}  \\
\multicolumn{2}{|c|}
{$\Downarrow$}     \\
\multicolumn{2}{|c|}
{[\textit{X is precise}] $\Rightarrow$ [\textit{X is exact and accurate}]} \\   \hline
\end{tabular}
\caption{The rules generation process}
\label{Rules-Gen}

\end{figure}

我希望箭头更长一些,并且位于顶部两棵树的中间,并与下面树的顶部“如果”的同一级别相匹配。另一个向下箭头也类似。

在此处输入图片描述

答案1

像这样?

在此处输入图片描述

\downarrows我在arrows.meta库的帮助下进行绘制,并将两者与第二行的图像合并。对于图像的定位,baseline使用选项;对于箭头和图像的定位,使用positioning库。

\documentclass[twocolumn]{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning}

\begin{document}
    \begin{figure} [ht!]
    \centering
\begin{tabular}{|cc|}
    \hline
\begin{tikzpicture}[baseline]
    \node (is-root) {is}
        child {node {X}}
        child {node {precise }};
    node {\textit{}};
\end{tikzpicture}
&
\begin{tikzpicture}[baseline]
    \node (is-root) {is} 
        child {node {X}}
        child {node {and}
        child {node {exact}}
        child {node {accurate}}
    };
    node {\textit{}};
\end{tikzpicture}\\
\multicolumn{2}{|c|}{
    \begin{tikzpicture}[baseline, 
    node distance=5mm,
    level 1/.style = {sibling distance=3cm},
    level 2/.style = {sibling distance=2cm} 
                        ]
    \draw[double equal sign distance,-Implies] (0,0) -> (0,-1) coordinate (start);
    \node[below=of start] (is-root) {if} 
        child {node {is}
        child {node {X}}
        child {node {precise }
        }}
        child {node {is}
        child {node {X} }
        child {node {and}
            child {node (extract) {exact}}
            child {node {accurate}}
        }};
    \draw[double equal sign distance,-Implies, shorten <=3mm,shorten >=3mm] 
        (is-root |- extract.south) -> ++(0,-1.6);
\end{tikzpicture}}          \\
\multicolumn{2}{|c|}{[\textit{X is precise}] $\Rightarrow$ 
                     [\textit{X is exact and accurate}]} \\   
    \hline
\end{tabular}
    \caption{The rules generation process}
\label{Rules-Gen}
    \end{figure}
\end{document}

答案2

嗯,它毕竟是一棵树,那么为什么它不是一片森林呢?

请注意,这样做没有什么特别的原因,除了可以避免对子树进行两次编码。

此版本需要 Forest 2.1。如果您想要早期 Forest 版本,请告诉我。

\documentclass{article}
\usepackage[linguistics]{forest}
\usepackage{amssymb,array}
\usetikzlibrary{arrows.meta}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}
\begin{document}
\begin{figure}
  \centering
  \begin{forest}
    before packing={
      for nodewalk={fake={name=earoot},L}{tier=this},
      delay={
        for nodewalk={fake={name=cond},L}{tier=that},
      },
    },
    before typesetting nodes={
      where content={}{
        if={>O_={!1.content}{}}{phantom}{no edge},
        if level=0{phantom}{
          calign with current edge,
          !1.edge+={-Implies, double, double distance=1.5pt},
        },
      }{}
    }
    [,
      [is, name=proot
        [X]
        [precise]
      ]
      [
        [
          [, tier=this
            [if, name=cond
              [, delay={replace by'=proot}]
              [
                [
                  [, tier=that
                    [{[X is precise] & $\Longrightarrow$ & [X is exact and accurate]}, font=\itshape, align={@{}R{40mm}cL{40mm}@{} }
                    ]
                  ]
                ]
              ]
              [, delay={replace by'=earoot}]
            ]
          ]
        ]
      ]
      [is, name=earoot
        [X]
        [and
          [exact]
          [accurate]
        ]
      ]
    ]
  \end{forest}
  \caption{The rules generation process}\label{fig:rgp}
\end{figure}
\end{document}

森林版

编辑

对于 Forest 2.03 版本,替换

        if={>O_={!1.content}{}}{phantom}{no edge},

        if={strequal(content("!1"),"")}{phantom}{no edge},

得到相同的结果。这可能适用于 Forest v2 及更高版本。

相关内容