我有以下 TikZ 图片:
我希望我的节点按照红色指示的方式对齐。
如何才能做到这一点?
到目前为止我所拥有的相当混乱,我不知道如何以一种优雅的方式实现我想要的......
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,positioning}
\begin{document}
\begin{tikzpicture}[>=stealth',thick,large-box/.style={draw,rounded corners,minimum width=30em,minimum height=3em,align=center},box/.style={draw,rounded corners,minimum width=10em,minimum height=3em,align=center},frame/.style={dashed,black}]
\node[large-box] (solver) {Solver};
\node[box,above=of solver,xshift=7.5em] (checker) {Checker};
\node[box,above=of checker] (parser) {Parser};
\node[above=of parser,font=\bfseries,minimum width=10em,align=center] (query) {Query};
\node[above=of solver,left=of query,font=\bfseries,minimum width=10em,align=center] (model) {Model};
\node[box,below=of solver,xshift=7.5em] (simplifier) {Simplifier};
\node[below=of simplifier,align=center,font=\bfseries,align=center] (simplification) {Simplification};
\node[below=of solver,left=of simplification,font=\bfseries] (solution) {Solution};
\draw[->] (model) -> ([xshift=-7.5em]solver.north);
\draw[->] (query) -> (parser);
\draw[->] (parser) -> (checker);
\draw[->] (checker) -> ([xshift=7.5em]solver.north);
\draw[->] (simplifier) -> (simplification);
\draw[->] (solver) |- ++(0,-1) -| (simplifier);
\draw[->] (solver) |- ++(0,-1) -| (solution);
\draw[frame] ([yshift=-9.2em,xshift=-16.5em]solver) rectangle ([yshift=3em,xshift=9em]parser);
\end{tikzpicture}
\end{document}
答案1
|-
您可以在路径中使用路径运算符:
\draw[->] (model) -> (model|-solver.north);
并且在节点定位方面:
\node[font=\bfseries] (solution) at (model|-simplification) {Solution};
修复:
对齐问题是由于 Solution 的高度比 Simplification 的高度要低得多,因为后者的字母“P”位于下方。通过在
text depth
两个节点上添加相同的字母“P”,这个问题已经得到解决。现在,解决方案和简化与规划求解中心的距离相同。
输出
代码
\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,arrows,positioning}
\begin{document}
\begin{tikzpicture}[>=stealth',thick,large-box/.style={draw,rounded corners,minimum width=30em,minimum height=3em,align=center},box/.style={draw,rounded corners,minimum width=10em,minimum height=3em,align=center},frame/.style={dashed,black}]
\node[large-box] (solver) {Solver};
\node[box,above=of solver,xshift=7.5em] (checker) {Checker};
\node[box,above=of checker] (parser) {Parser};
\node[above=of parser,font=\bfseries,minimum width=10em,align=center] (query) {Query};
\node[xshift=-7.5em,font=\bfseries,minimum width=10em,align=center] (model) at (query.west-|solver.north) {Model};
\node[box,below=of solver,xshift=7.5em] (simplifier) {Simplifier};
\node[below=of simplifier,align=center,font=\bfseries, text depth=1ex] (simplification) {Simplification};
\node[font=\bfseries, text depth=1ex] (solution) at (model|-simplification) {Solution};
\draw[->] (model) -> (model|-solver.north);
\draw[->] (query) -> (parser);
\draw[->] (parser) -> (checker);
\draw[->] (checker) -> (checker|-solver.north);
\draw[->] (simplifier) -> (simplification);
\draw[->] (solver) |- ++(0,-1) -| (simplifier);
\draw[->] (solver) |- ++(0,-1) -| (model|-solution.north);
\draw[frame] ([yshift=-9.2em,xshift=-16.5em]solver) rectangle ([yshift=3em,xshift=9em]parser);
% arrow to check height, you can delete this.
\draw[red] (solution.base) --++ (6,0);
\end{tikzpicture}
\end{document}