tikz 中表格的对齐 \node

tikz 中表格的对齐 \node

我试图让一组表格(每个表格都包含在一个 tikz 节点中)全部对齐,就像 一样anchor=north west,即我基本上希望该\node点位于表格的左上角。但是,我似乎无法让它工作,LaTeX 将它放在表格的中心:

表格未对齐

梅威瑟:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{figure} \centering
\begin{tikzpicture}[remember picture]

\draw node (places) {
    \begin{tabular}[t]{|l|}
        \hline
        \textbf{sources.places} \\
        \hline
        id \\
        \hline
        place\_name (unique) \\
        place\_x (unique) \\
        place\_y (unique) \\
        comment \\
        \hline
    \end{tabular}
};

\draw node (meta) [right=of places] {
    \begin{tabular}[t]{|l|}
        \hline
        \textbf{sources.meta} \\
        \hline
        id \\
        \hline
        src (unique) \\
        lang \\
        year\_from \\
        year\_to \\
        \emph{plcid} \\
        comment \\
        \hline
    \end{tabular}
};
\end{tikzpicture}
\end{figure}

\end{document}

答案1

您需要base right密钥而不是right

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}

\begin{figure} \centering
\begin{tikzpicture}[remember picture]

\draw node (places) {
    \begin{tabular}[t]{|l|}
        \hline
        \textbf{sources.places} \\
        \hline
        id \\
        \hline
        place\_name (unique) \\
        place\_x (unique) \\
        place\_y (unique) \\
        comment \\
        \hline
    \end{tabular}
};

\draw node (meta) [base right=of places] {
    \begin{tabular}[t]{|l|}
        \hline
        \textbf{sources.meta} \\
        \hline
        id \\
        \hline
        src (unique) \\
        lang \\
        year\_from \\
        year\_to \\
        \emph{plcid} \\
        comment \\
        \hline
    \end{tabular}
};
\end{tikzpicture}
\end{figure}

\end{document}

在此处输入图片描述

答案2

OP 将 a 放在a 的tabulara 内。我认为将表格绘制为 a 更灵活。(然后我们可以根据需要将其放在环境中)nodetikzpicturetikzpicturetikzpicturetabular

在此处输入图片描述

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\begin{scope}[yscale=.5,xscale=.95]
\draw[shift={(-.5,.5)}] 
(0,0) rectangle (4,-5)
(0,-1)--+(0:4) (0,-2)--+(0:4);

\path[right=-3mm]
(0,0)  node{\bfseries sources.places}
(0,-1) node{id}
(0,-2) node{place\_name (unique)}
(0,-3) node{place\_x (unique)}
(0,-4) node{place\_y (unique)};
\end{scope}

\begin{scope}[shift={(0:4.5)},yscale=.5,xscale=.9]
\draw[shift={(-.5,.5)}] 
(0,0) rectangle (3,-8)
(0,-1)--+(0:3) (0,-2)--+(0:3);

\path[right=-3mm]
(0,0)  node{\bfseries sources.meta}
(0,-1) node{id}
(0,-2) node{scr (unique)}
(0,-3) node{lang}
(0,-4) node{year\_from}
(0,-5) node{year\_to}
(0,-6) node{\emph{plcid}}
(0,-7) node{comment};
\end{scope}
\end{tikzpicture}
\end{document}

答案3

您也可以使用两个 TikZ matrix

我创建了一个以矩阵名称作为参数的样式。

TikZmatrix是节点,您可以用它right=of places.north east, anchor=north west进行定位。

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning, matrix}
\tikzset{
    mystyle/.style={
        matrix of nodes, 
        name=#1,
        inner sep=0pt,
        nodes={
            anchor=west, 
            inner xsep=6pt,
            minimum height=3ex,
            },
        row sep=-\pgflinewidth,
        draw,
        row 1/.style={nodes={font=\bfseries}},
        append after command={%
            \pgfextra{\foreach \myrow in {1,2}{\coordinate (a) at (#1.east |- places-\myrow-1.south west);
            \draw (#1-\myrow-1.south west) +(\pgflinewidth,0)-- (a);}}
            }
        }
    }

\begin{document}

\begin{figure} 
\centering
\begin{tikzpicture}[remember picture]
\matrix[mystyle=places]
    {sources.places \\
    id \\
    place\_name (unique) \\
    place\_x (unique) \\
    place\_y (unique) \\
    comment \\
    };
\matrix[mystyle=meta, right=of places.north east, anchor=north west] 
    {sources.meta \\
    id \\
    src (unique) \\
    lang \\
    year\_from \\
    year\_to \\
    \emph{plcid} \\
    comment \\
    };
\end{tikzpicture}
\end{figure}
\end{document}

在此处输入图片描述

相关内容