图中标题在节点上方水平对齐

图中标题在节点上方水平对齐

我正在尝试创建一个从左到右的流程图,其中每个阶段都有一个与之对齐的标题,位于图表上方的一定高度。我正在尝试使用氢裂解示例(参见下划线标题),但该示例使用固定定位,而我理想情况下希望使用固定的垂直坐标(可能相对于我最高的列)和相对的水平坐标。

下面的代码不起作用,但希望能够显示我正在尝试做的事情。

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}[node distance = 1mm, auto,
    box/.style={
        rectangle,
        draw,
        fill=blue!20, 
        text width=5em,
        text centered,
        rounded corners, 
        minimum height=4em
    }]  

    % First column
    \node [box]                  (b1)   {Box 1};
    \node [box, below=of b1]     (b2)   {Box 2};
    \node [box, below=of b2]     (b3)   {Box 3};

    % Second column
    \node [box, right=of b2, xshift=30mm]   (b4) {Box 4};

    % Labels at top
    \node [above=of b1, font=\large] (title1) {\underline{Column One}};
    \node [right=of title1, above=of b4, font=\large]{\underline{Column Two}};


\end{tikzpicture}
\end{document}

在此处输入图片描述

答案1

一种选择是使用隐式语法( <p> |- <q> )(或( <q> -| <p> ))来获取(u,v),其中u是的 x 坐标<p>,v 是的 y 坐标<q>;在您的情况下,您可以使用at (title1-|b4)

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}[node distance = 1mm and 30mm, auto,
    box/.style={
        rectangle,
        draw,
        fill=blue!20, 
        text width=5em,
        text centered,
        rounded corners, 
        minimum height=4em
    }]  

    % First column
    \node [box]                  (b1)   {Box 1};
    \node [box, below=of b1]     (b2)   {Box 2};
    \node [box, below=of b2]     (b3)   {Box 3};

    % Second column
    \node [box, right=of b2]   (b4) {Box 4};

    % Labels at top
    \node [above=of b1, font=\large] (title1) {\underline{Column One}};
    \node [font=\large,text centered] at (title1-|b4) {\underline{Column Two}};

\end{tikzpicture}
\end{document}

在此处输入图片描述

如果需要更多复杂的计算,可以使用let构造(需要calc库)来获取其他节点坐标(在本例中y为的坐标(title1)x的坐标(b4)),并使用这些坐标来放置标题:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows,positioning,calc}

\begin{document}
\pagestyle{empty}

\begin{tikzpicture}[node distance = 1mm and 30mm, auto,
    box/.style={
        rectangle,
        draw,
        fill=blue!20, 
        text width=5em,
        text centered,
        rounded corners, 
        minimum height=4em
    }]  

    % First column
    \node [box]                  (b1)   {Box 1};
    \node [box, below=of b1]     (b2)   {Box 2};
    \node [box, below=of b2]     (b3)   {Box 3};

    % Second column
    \node [box, right=of b2]   (b4) {Box 4};

    % Labels at top
    \node [above=of b1, font=\large] (title1) {\underline{Column One}};
    \draw let \p1=(b4), \p2=(title1) in node [font=\large,text centered] at (\x1,\y2) {\underline{Column Two}};

\end{tikzpicture}
\end{document}

相关内容