如何使用 tikz 调整节点中部分文本的大小?

如何使用 tikz 调整节点中部分文本的大小?

我有一张图表,其中节点上方的时间是出发时间。我如何才能更好地格式化“+1”?它代表“第二天”。现在相对于时间来说太大了,而且“加号”也太夸张了。我试图把“+1”弄小一点,也许用不同的颜色。

我该如何实现这个?谢谢!

编辑:我怎样才能减少覆盖每个边缘的白色部分?(边缘中间被数字覆盖的部分)

\documentclass[margin=10pt]{standalone}
\usepackage{color,xcolor}
\usepackage{tikz-qtree, tikz}
\usepackage{pgfplots}
\usepackage[utf8]{inputenc} 
\usetikzlibrary{decorations.pathreplacing,arrows,shapes,positioning,shadows,calc}
\usetikzlibrary{decorations, decorations.text,backgrounds}
\tikzset{every picture/.style={font issue=\footnotesize},
    font issue/.style={execute at begin picture={#1\selectfont}}
}

\pgfplotsset{width=10cm,compat=1.17}
\usepgfplotslibrary{fillbetween}

\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usepackage{clock}
\usepackage[clock]{ifsym}

\ClockFrametrue\ClockStyle=0

\newcommand{\orig}{\mathcal{O}} % origin position
\newcommand{\dest}{\mathcal{D}} % destination position

\begin{document}
    \begin{tikzpicture}
            \begin{scope}[every node/.style={circle,thick,draw}]
            \node[fill=red!30] (Orig) at (0,0) {$\orig$};
            \node (A) at (2,2) {A};
            \node (B) at (2.3,0.5) {B};
            \node (C) at (3.3,-1.2) {C};
            \node (H) at (4.2,2.1) {H};
            \node (L) at (6.4,-0.5) {L};
            \node (E) at (5.9,1.9) {E} ;
            \node[fill=green!30] (Dest) at (8,1) {$\dest$};
        \end{scope}
        
        \begin{scope}[>={stealth[black]},
            every node/.style={fill=white,circle},
            every edge/.style={draw=red,very thick}]
            \path [->] (Orig) edge node {$5$} (A);
            \path [->] (A) edge node {$5$} (H);
            \path [->] (H) edge node {$3$} (E);
            \path [->] (E) edge node {$8$} (Dest); 
            % \path [->] (B) edge[bend right=60] node {$1$} (E); 
        \end{scope}
    
        \begin{scope}[>={stealth[black]},
            every node/.style={fill=white,circle},
            every edge/.style={draw=black,very thick}]
            \path [->] (Orig) edge node {$5$} (B);
            \path [->] (Orig) edge node {$5$} (C);
            \path [->] (B) edge node {$3$} (C);
            \path [->] (B) edge node {$3$} (H);
            \path [->] (B) edge node {$3$} (L);
            \path [->] (C) edge node {$5$} (L);
            \path [->] (E) edge node {$8$} (L); 
            \path [->] (L) edge node {$8$} (Dest); 
        \end{scope}
    

        \begin{scope}[font=\tiny]
            \node [above=0.5pt of A] {\clock{15}{30} 15:30};
            \node [below=0.5pt of Orig] {\clock{18}{15} 18:15};
            \node [above=0.5pt of H] {\clock{8}{30} 8:30$^{+1}$};
            \node [above=0.5pt of E] {\clock{10}{45} 10:45$^{+1}$};
            \node [above=0.5pt of Dest, align=center] {Arrive \\ \clock{12}{00} 12:00$^{+1}$};
        \end{scope}

    
        \matrix [draw,above left, row sep=1mm, nodes={font=\scriptsize}] at (current bounding box.north east) {
            \node [circle, black, draw, label=right:Hotel] {H}; \\
            \node [circle, black, draw, label=right:Lunch] {L}; \\
        };
    \end{tikzpicture}
\end{document}

图形

答案1

我会稍微重新设计一下你的形象。我建议使用:

  • quotes边缘标签库
  • 在节点 O、A、H、E 和 D 使用labels时间信息
  • 大多数样式写为选项tikzpicture
\documentclass[margin=10pt]{standalone}

\usepackage{clock}
\ClockFrametrue\ClockStyle=1

\usepackage{tikz}
\usetikzlibrary{arrows.meta,
                positioning,
                quotes}

\begin{document}
    \begin{tikzpicture}[
C/.style = {circle, draw, fill=#1, minimum size=1em, inner sep=1pt},
C/.default = white,
every label/.style = {align=center, font=\tiny, inner sep=2pt},
every edge/.style = {draw, - Stealth, thick},
every edge quotes/.style = {font=\scriptsize, text=black, fill=white, inner sep=2pt}
                        ]
\node (O) [C=red!30, 
           label=below:\clock{18}{15}\\\scriptsize{18:15}]  {$\mathcal{O}$};
\node (A) [C,
           label=above:\clock{15}{30}\\\scriptsize{15:30}]          at (2,2)    {A};
\node (B) [C] at (2.3,0.5) {B};
\node (C) [C] at (3.3,-1.2) {C};
\node (H) [C, 
           label=above:\clock{8}{30}\\\scriptsize{8:30$^{+1}$}]      at (4,2)    {H};
\node (L) [C] at (6.4,-0.5) {L};
\node (E) [C, 
           label=above:\clock{10}{45}\\\scriptsize{10:45$^{+1}$}]   at (6,2)    {E} ;
\node (D) [C=green!30,
           label=85:{\scriptsize Arrive:\\[1ex] 
                        \clock{12}{00}
                        \scriptsize 12:00$^{+1}$}]  at (8,1)    {$\mathcal{D}$};

\path [red] (O) edge ["5"]  (A) 
            (A) edge ["5"]  (H) 
            (H) edge ["3"]  (E) 
            (E) edge ["8"]  (D);
\path       (O) edge ["5"]  (B)
            (O) edge ["5"]  (C)
            (B) edge ["3"]  (C)
            (B) edge ["3"]  (H)
            (B) edge ["3"]  (L)
            (C) edge ["5"]  (L)
            (E) edge ["8"]  (L)
            (L) edge ["8"]  (D);

\matrix [draw,above left, row sep=1mm] 
        at (current bounding box.north east) 
{\node [C, label=right:Hotel] {H}; \\
 \node [C, label=right:Lunch] {L}; \\
};
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

我不知道为什么时钟不为我出现,但其余的都还好,所以......

调整节点中的字体大小

\documentclass[margin=10pt]{standalone}

\usepackage{color,xcolor}
\usepackage{tikz-qtree, tikz}
\usepackage{pgfplots}
\usepackage[utf8]{inputenc} 
\usetikzlibrary{decorations.pathreplacing,arrows,shapes,positioning,shadows,calc}
\usetikzlibrary{decorations, decorations.text,backgrounds}
\tikzset{every picture/.style={font issue=\footnotesize},
    font issue/.style={execute at begin picture={#1\selectfont}}
}

\pgfplotsset{width=10cm,compat=1.17}
\usepgfplotslibrary{fillbetween}

\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usepackage{clock}
\usepackage[clock]{ifsym}

\ClockFrametrue\ClockStyle=0

\newcommand{\orig}{\mathcal{O}} % origin position
\newcommand{\dest}{\mathcal{D}} % destination position

\begin{document}
    \begin{tikzpicture}
            \begin{scope}[every node/.style={circle,thick,draw}]
            \node[fill=red!30] (Orig) at (0,0) {$\orig$};
            \node (A) at (2,2) {A};
            \node (B) at (2.3,0.5) {B};
            \node (C) at (3.3,-1.2) {C};
            \node (H) at (4.2,2.1) {H};
            \node (L) at (6.4,-0.5) {L};
            \node (E) at (5.9,1.9) {E} ;
            \node[fill=green!30] (Dest) at (8,1) {$\dest$};
        \end{scope}
        
        \begin{scope}[>={stealth[black]},
            every node/.style={fill=white,circle, inner sep = 0.5pt},
            every edge/.style={draw=red,very thick}]
            \path [->] (Orig) edge node {$5$} (A);
            \path [->] (A) edge node {$5$} (H);
            \path [->] (H) edge node {$3$} (E);
            \path [->] (E) edge node {$8$} (Dest); 
            % \path [->] (B) edge[bend right=60] node {$1$} (E); 
        \end{scope}
    
        \begin{scope}[>={stealth[black]},
            every node/.style={fill=white,circle,inner sep=0.5pt},
            every edge/.style={draw=black,very thick}]
            \path [->] (Orig) edge node {$5$} (B);
            \path [->] (Orig) edge node {$5$} (C);
            \path [->] (B) edge node {$3$} (C);
            \path [->] (B) edge node {$3$} (H);
            \path [->] (B) edge node {$3$} (L);
            \path [->] (C) edge node {$5$} (L);
            \path [->] (E) edge node {$8$} (L); 
            \path [->] (L) edge node {$8$} (Dest); 
        \end{scope}
    

        \begin{scope}[font=\tiny, every node/.style={scale=0.8}]
            \node [above=0.5pt of A] {\clock{15}{30} 15:30};
            \node [below=0.5pt of Orig] {\clock{18}{15} 18:15};
            \node [above=0.5pt of H] {\clock{8}{30} 8:30$^{+1}$};
            \node [above=0.5pt of E] {\clock{10}{45} 10:45$^{+1}$};
            \node [above=0.5pt of Dest, align=center] {Arrive \\ \clock{12}{00} 12:00$^{+1}$};
        \end{scope}

    
        \matrix [draw,above left, row sep=1mm, nodes={font=\scriptsize}] at (current bounding box.north east) {
            \node [circle, black, draw, label=right:Hotel] {H}; \\
            \node [circle, black, draw, label=right:Lunch] {L}; \\
        };
    \end{tikzpicture}
\end{document}

编辑

我可能一开始就读错了,你只想调整 +1 的大小。一种方法是向节点添加一个标签,其中包含 +1 并正确定位。我试过这个(注意每个节点的样式):

\begin{scope}[font=\tiny,every node/.style={inner sep=0pt}]
            \node [above=0.5pt of A] {\clock{15}{30} 15:30};
            \node [below=0.5pt of Orig] {\clock{18}{15} 18:15};
            \node [above=0.5pt of H,label={[scale=0.7]10:$+1$}] {\clock{8}{30} 8:30};

这就导致了:

不同的标签

您可以看到节点 H(带标签)和节点 E(旧样式)之间的区别。以下是完整代码,其中到达也作为节点标签:

\documentclass[margin=10pt]{standalone}
% https://tex.stackexchange.com/q/635913/204164

\usepackage{color,xcolor}
\usepackage{tikz-qtree, tikz}
\usepackage{pgfplots}
\usepackage[utf8]{inputenc} 
\usetikzlibrary{decorations.pathreplacing,arrows,shapes,positioning,shadows,calc}
\usetikzlibrary{decorations, decorations.text,backgrounds}
\tikzset{every picture/.style={font issue=\footnotesize},
    font issue/.style={execute at begin picture={#1\selectfont}}
}

\pgfplotsset{width=10cm,compat=1.17}
\usepgfplotslibrary{fillbetween}

\usetikzlibrary{arrows.meta}
\usetikzlibrary{positioning}
\usepackage{clock}
\usepackage[clock]{ifsym}

\ClockFrametrue\ClockStyle=0

\newcommand{\orig}{\mathcal{O}} % origin position
\newcommand{\dest}{\mathcal{D}} % destination position

\begin{document}
    \begin{tikzpicture}
            \begin{scope}[every node/.style={circle,thick,draw}]
            \node[fill=red!30] (Orig) at (0,0) {$\orig$};
            \node (A) at (2,2) {A};
            \node (B) at (2.3,0.5) {B};
            \node (C) at (3.3,-1.2) {C};
            \node (H) at (4.2,2.1) {H};
            \node (L) at (6.4,-0.5) {L};
            \node (E) at (5.9,1.9) {E} ;
            \node[fill=green!30] (Dest) at (8,1) {$\dest$};
        \end{scope}
        
        \begin{scope}[>={stealth[black]},
            every node/.style={fill=white,circle, inner sep = 0.5pt},
            every edge/.style={draw=red,very thick}]
            \path [->] (Orig) edge node {$5$} (A);
            \path [->] (A) edge node {$5$} (H);
            \path [->] (H) edge node {$3$} (E);
            \path [->] (E) edge node {$8$} (Dest); 
            % \path [->] (B) edge[bend right=60] node {$1$} (E); 
        \end{scope}
    
        \begin{scope}[>={stealth[black]},
            every node/.style={fill=white,circle,inner sep=0.5pt},
            every edge/.style={draw=black,very thick}]
            \path [->] (Orig) edge node {$5$} (B);
            \path [->] (Orig) edge node {$5$} (C);
            \path [->] (B) edge node {$3$} (C);
            \path [->] (B) edge node {$3$} (H);
            \path [->] (B) edge node {$3$} (L);
            \path [->] (C) edge node {$5$} (L);
            \path [->] (E) edge node {$8$} (L); 
            \path [->] (L) edge node {$8$} (Dest); 
        \end{scope}
    

        \begin{scope}[font=\tiny,every node/.style={inner sep=0pt}]
            \node [above=0.5pt of A] {\clock{15}{30} 15:30};
            \node [below=0.5pt of Orig] {\clock{18}{15} 18:15};
            \node [above=0.5pt of H,label={[scale=0.7]10:$+1$}] {\clock{8}{30} 8:30};
            \node [above=0.5pt of E,label={[scale=0.7]10:$+1$}]{\clock{10}{45} 10:45};
            \node [above=0.5pt of Dest, align=center,label={[label distance=2pt]above:Arrive},label={[scale=0.7]10:$+1$}] {\clock{12}{00} 12:00};
        \end{scope}

    
        \matrix [draw,above left, row sep=1mm, nodes={font=\scriptsize}] at (current bounding box.north east) {
            \node [circle, black, draw, label=right:Hotel] {H}; \\
            \node [circle, black, draw, label=right:Lunch] {L}; \\
        };
    \end{tikzpicture}
\end{document}

不相关,但是您的代码可以改进得更短,您不需要所有这些scope

答案3

在此处输入图片描述

我使用\scalebox命令来缩小+1

通过设置来控制箭头标签周围的空白inner sep

此外,我以一种对我来说更自然的方式重写了您的代码,没有所有的scopes。使用\foreach是一种设置坐标和边缘的简单方法,以防您想要添加或更改任何内容。

以下是代码:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{positioning}

\usepackage{clock}

\ClockFrametrue\ClockStyle=0

\newcommand{\orig}{\ensuremath{\mathcal{O}}} % origin position
\newcommand{\dest}{\ensuremath{\mathcal{D}}} % destination position

\tikzset{vertex/.style={circle, thick, inner sep=0mm, minimum width=7mm, draw}}

\begin{document}

\begin{tikzpicture}
\node[vertex, fill=red!30] (Orig) at (0,0) {$\orig$};
\node[vertex, fill=green!30] (Dest) at (8,1) {$\dest$};
\foreach \x/\y/\nm/\lbl in {2/2/A/A,2.3/.5/B/B,3.3/-1.2/C/C,4.2/2.1/H/H,6.4/-.5/L/L,5.9/1.9/E/E} 
    \node[vertex](\nm) at (\x,\y) {\lbl};
\foreach \a\b\lbl in {Orig/B/5,Orig/C/5,B/C/3,B/H/3,B/L/3,C/L/5,E/L/8,L/Dest/8}
    \draw [very thick,-stealth](\a)--(\b) node[circle, fill=white, inner sep=.5mm, midway]{\lbl};
\foreach \a\b\lbl in {Orig/A/5,A/H/5,H/E/3,E/Dest/8}
    \draw [draw=red, fill=black, very thick, arrows={-stealth[black]}](\a)--(\b) node[circle, fill=white, inner sep=.5mm, midway]{\lbl};
\foreach \n\hr\min\day in {A/15/30/,Orig/18/15/,H/8/30/+1,E/10/45/+1,Dest/12/00/+1}
    \node [font=\tiny, above=0.5pt of \n] {\clock{\hr}{\min} \hr:\min${}^{\scalebox{.8}{\day}}$};

\matrix [draw,above left, row sep=1mm, nodes={font=\scriptsize}] at (current bounding box.north east) {
    \node [circle, black, draw, label=right:Hotel] {H}; \\
    \node [circle, black, draw, label=right:Lunch] {L}; \\
};
\end{tikzpicture}

\end{document}

相关内容