如何更改 tikzpicture 中某些节点之间的垂直距离(或将其设置为任意值)

如何更改 tikzpicture 中某些节点之间的垂直距离(或将其设置为任意值)

在下面的例子中,我想使节点/章节 1 和节点/章节 3 之间的垂直距离等于节点/章节 3 和节点/章节 4 之间的当前距离。节点/章节 2 的位置应在水平方向上与现在的位置相同,在垂直方向上与节点/章节 1 和节点/章节 3 的新位置的距离相等。节点/章节 3 和 4 之间的距离不应改变。我该怎么做?

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{float}
\usepackage[colorlinks=true]{hyperref}

\definecolor{lightestgray}{rgb}{0.95,0.95,0.95}

\begin{document}

\setcounter{chapter}{-1}

\chapter{Preface} \label{Preface}

\newpage
\begin{figure}[H]%
    \noindent\begin{center}%
        \begin{tikzpicture}[%
            >=stealth,%
            every node/.style={%
                text width=2cm,
                align=center,
                fill=lightestgray,
                draw%
            },]%
            % create the nodes,
            \node (c0) [shape=rectangle,rounded corners] {
                \hyperref[Preface]{\ref{Preface}\\\nameref{Preface}}
            };%
            
            \node (c1) [shape=circle] [below left =of c0] {
                \hyperref[chapter:Intro]{Chapter \ref{chapter:Intro}\\\nameref{chapter:Intro}}
            };%
            
            \node (c2) [shape=circle] [below right =of c1] {
                \hyperref[chapter:Lit]{Chapter \ref{chapter:Lit}\\\nameref{chapter:Lit}}
            };%
            
            \node (c3) [shape=circle] [below left =of c2] {
                \hyperref[chapter:Nice]{Chapter \ref{chapter:Nice}\\\nameref{chapter:Nice}}
            };%
            
            \node (c4) [shape=circle] [below =of c3] {
                \hyperref[chapter:Conclus]{Chapter \ref{chapter:Conclus}\\\nameref{chapter:Conclus}}
            };%
            
            \node (c5) [shape=rectangle,rounded corners] [below right =of c4] {
                \hyperref[appendix:SCode]{Appendix \ref{appendix:SCode}\\\nameref{appendix:SCode}}
            };%
            
            \node (c6) [shape=rectangle,rounded corners] [right =of c5] {
                \hyperref[appendix:Docs]{Appendix \ref{appendix:Docs}\\\nameref{appendix:Docs}}
            };%
            
            % connect the nodes
            \draw[->,dashed] (c0.south west) to[out=225,in=45] (c1.north east);%
            \draw[->] (c1.south east) to[out=315,in=135] (c2.north west);%
            \draw[->] (c1.south) to[out=270,in=90] (c3.north);%
            \draw[->,dashed] (c2.south west) to[out=225,in=45] (c3.north east);%
            \draw[->] (c3.south) to[out=270,in=90] (c4.north);%
            \draw[->,dashed] (c5.north) to[out=90,in=270] (c2.south);%
            \draw[->,dashed] (c6.north) to[out=90,in=315] (c2.south east);%
        \end{tikzpicture}%
    \end{center}%
\end{figure}%

\chapter{Intro} \label{chapter:Intro}

\chapter{Lit Review} \label{chapter:Lit}

\chapter{Nice things} \label{chapter:Nice}

\chapter{Conclus} \label{chapter:Conclus}

\renewcommand{\thechapter}{A}
\chapter{S Code} \label{appendix:SCode}

\renewcommand{\thechapter}{B}
\chapter{Docs} \label{appendix:Docs}

\end{document}

快照

快照

答案1

像这样?

在此处输入图片描述

通过使用calcchainspositioning库:

\documentclass{book}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                calc, chains, 
                positioning}
\usepackage[colorlinks=true]{hyperref}

\definecolor{lightestgray}{rgb}{0.95,0.95,0.95}

\begin{document}
    \begin{figure}[ht]%
    \centering
\begin{tikzpicture}[>=stealth,%
    node distance = 11mm and 13mm,
      start chain = going below,
         C/.style = {circle, draw, fill=lightestgray, text width=2cm, align=center},
         R/.style = {C, rectangle, rounded corners},
every edge/.style = {draw, -Stealth}
                    ]
% create the nodes,
    \begin{scope}[nodes={C, on chain}]
\node (c1) {\hyperref[chapter:Intro]{Chapter \ref{chapter:Intro}\\
            \nameref{chapter:Intro}}};
\node (c3) {\hyperref[chapter:Nice]{Chapter \ref{chapter:Nice}\\
            \nameref{chapter:Nice}}};
\node (c4) {\hyperref[chapter:Conclus]{Chapter \ref{chapter:Conclus}\\
            \nameref{chapter:Conclus}}};
    \end{scope}
\node (c2) [C, right=of $(c1.east)!0.5!(c3.east)$]
           {\hyperref[chapter:Lit]{Chapter \ref{chapter:Lit}\\
            \nameref{chapter:Lit}}};
% Appendinces 
\node (c0)  [R, above right=of c1]
            {\hyperref[Preface]{\ref{Preface}\\\nameref{Preface}}};
 \node (c5) [R, below right=of c4.south] 
            {\hyperref[appendix:SCode]{Appendix \ref{appendix:SCode}\\
             \nameref{appendix:SCode}}};
\node (c6)  [R, right =of c5] 
            {\hyperref[appendix:Docs]{Appendix \ref{appendix:Docs}\\
             \nameref{appendix:Docs}}};
% links between nodes nodes
\draw   (c1) edge   (c3)
        (c3) edge   (c4)
        (c1) edge   (c2);
\draw[dashed] 
        (c0.south west) edge   (c1)
        (c2) edge   (c3)
        (c5) edge   (c2)
        (c6) edge   (c2);
\end{tikzpicture}%
    \end{figure}%

\chapter{Intro} \label{chapter:Intro}

\chapter{Lit Review} \label{chapter:Lit}

\chapter{Nice things} \label{chapter:Nice}

\chapter{Conclus} \label{chapter:Conclus}

\renewcommand{\thechapter}{A}
\chapter{S Code} \label{appendix:SCode}

\renewcommand{\thechapter}{B}
\chapter{Docs} \label{appendix:Docs}

\end{document}

相关内容