如何调整TikZ中L形箭头的起始位置以避免图形重叠
调整TikZ中L形箭头起始位置的解决方案
问题描述
需要调整图表中从“Regimes (of sovereign states)”框引出的L形箭头位置,让箭头从该框的y轴更低位置出发,避免与“Non-regimes”框产生干扰。可复现代码如下:
\documentclass[12pt]{article} \usepackage[margin=1in]{geometry} \usepackage{graphicx} \usepackage{pdflscape} \usepackage{palatino} \usepackage[table,xcdraw]{xcolor} \usepackage{tikz} \usetikzlibrary{shapes.geometric, arrows, positioning} \tikzstyle{arrow}=[draw, -latex] \tikzstyle{tblock} = [rectangle, rounded corners, minimum width=4cm, minimum height=3cm,text centered, draw=black, fill=white] \tikzstyle{autoblock} = [rectangle, rounded corners, minimum width=4cm, minimum height=3cm,text centered, draw=black, fill=gray!20] \begin{figure}[H] \caption{Concept Ladder of Regime Types: Residual vs. Substantive Authoritarianism\\} \vspace*{10mm} \centering \begin{tikzpicture} \node (state) [tblock, text width=3cm] {States of the world}; \node (reg) [tblock, below left= 1cm and .25cm of state, text width=3cm] {Regimes \\ (of sovereign states)}; \node (nonreg) [autoblock, below right= 1cm and .25cm of state, text width=3cm] {Non-regimes \\ (anarchy, foreign \\ occupation, transition)}; \draw [arrow] (state) -- (reg); \draw [arrow] (state) -- (nonreg); \node (demone) [tblock, below left=2cm and .25cm of reg, text width=3cm] {Democratic regimes}; \draw [arrow] (reg) -| (demone); \node (auto) [autoblock, below right=2cm and 4.5cm of reg, text width=3cm] {Authoritarian regimes \\ (substantive authoritarianism)}; \draw [arrow] (reg) -| (auto); \node (lfs) [autoblock, below = 2cm of reg, text width=3cm] {Limited franchise \\ systems}; \draw [arrow] (reg) -- (lfs); \node (dfr) [autoblock, below right=2cm and .25cm of reg, text width=3cm] {Dominant faction \\ regimes}; \draw [arrow] (reg) -| (dfr); \end{tikzpicture} \end{figure} \end{document}
解决方案
核心思路是指定箭头从“Regimes”框的底部节点(而非默认的中心节点)出发,TikZ中可以通过节点的*锚点(anchor)*来精准控制起始位置:
- 对于从
reg框引出的L形箭头,将原来的(reg)替换为(reg.south)(框的底部中心),或者根据需要选择(reg.south east)(右下角)、(reg.south west)(左下角)等锚点,这样箭头会从框的下部开始,避开上方的“Non-regimes”框。 - 如果需要更精细的调整,可以在锚点基础上偏移坐标,比如
(reg.south) + (0, -0.2cm),让箭头起点再往下一点,进一步避免干扰。
修改后的完整代码如下:
\documentclass[12pt]{article} \usepackage[margin=1in]{geometry} \usepackage{graphicx} \usepackage{pdflscape} \usepackage{palatino} \usepackage[table,xcdraw]{xcolor} \usepackage{tikz} \usetikzlibrary{shapes.geometric, arrows, positioning} \tikzstyle{arrow}=[draw, -latex] \tikzstyle{tblock} = [rectangle, rounded corners, minimum width=4cm, minimum height=3cm,text centered, draw=black, fill=white] \tikzstyle{autoblock} = [rectangle, rounded corners, minimum width=4cm, minimum height=3cm,text centered, draw=black, fill=gray!20] \begin{figure}[H] \caption{Concept Ladder of Regime Types: Residual vs. Substantive Authoritarianism\\} \vspace*{10mm} \centering \begin{tikzpicture} \node (state) [tblock, text width=3cm] {States of the world}; \node (reg) [tblock, below left= 1cm and .25cm of state, text width=3cm] {Regimes \\ (of sovereign states)}; \node (nonreg) [autoblock, below right= 1cm and .25cm of state, text width=3cm] {Non-regimes \\ (anarchy, foreign \\ occupation, transition)}; \draw [arrow] (state) -- (reg); \draw [arrow] (state) -- (nonreg); \node (demone) [tblock, below left=2cm and .25cm of reg, text width=3cm] {Democratic regimes}; % 修改箭头起点为reg左下角 \draw [arrow] (reg.south west) -| (demone); \node (auto) [autoblock, below right=2cm and 4.5cm of reg, text width=3cm] {Authoritarian regimes \\ (substantive authoritarianism)}; % 修改箭头起点为reg右下角 \draw [arrow] (reg.south east) -| (auto); \node (lfs) [autoblock, below = 2cm of reg, text width=3cm] {Limited franchise \\ systems}; \draw [arrow] (reg) -- (lfs); \node (dfr) [autoblock, below right=2cm and .25cm of reg, text width=3cm] {Dominant faction \\ regimes}; % 修改箭头起点为reg底部中心 \draw [arrow] (reg.south) -| (dfr); \end{tikzpicture} \end{figure} \end{document}
调整后,所有从reg框引出的L形箭头都会从框的下部出发,完全避开“Non-regimes”框的干扰,满足需求。
内容的提问来源于stack exchange,提问作者Ashley Anderson
相关产品推荐
相关产品推荐

