如何在 tikz 中沿路径添加标签?

如何在 tikz 中沿路径添加标签?

我想通过 tikz 绘制下图。我尝试了几种方法(装饰、中间等)将标签词放在函数图形上,但失败了。

在此处输入图片描述

这是我的代码

\usepackage{pgf,tikz}
\usetikzlibrary{matrix}
\usetikzlibrary{automata,patterns,calc,trees,positioning,arrows,chains,shapes.geometric,%
decorations.pathreplacing,decorations.pathmorphing,decorations.markings,shapes,%
matrix,shapes.symbols}
\tikzset{>=latex}
\begin{tikzpicture}[scale=2.5]
\draw[->] (-0.5,0) -- (3,0) node[below] {$q$};
\draw[->] (0,-0.5) -- (0,3) node[left] {$t$};
\draw[domain=0:3, samples=200, smooth, variable=\x, thick] plot ({\x},{1.4142*sqrt(\x)}) node[above] {\small $\theta_H$-type buyer's indifference curve: $\theta_H v(q) = t$};
\draw[domain=2.9:0.2, smooth, variable=\x, thin, gray] plot ({\x},{0.5*\x+1}) node[above] {\small Seller's isoprofit curve};
\draw[domain=0:3, samples=200, smooth, variable=\y, very thick, gray] plot ({\y},{sqrt(\y)}) node[below=1, black] {\small $\theta_L$-type buyer's indifference curve: $\theta_L v(q) = t$};
\draw[domain=0.2:2.9, smooth, variable=\y, thin, gray] plot ({\y},{0.5*\y+0.5}) node[below] {\small Seller's isoprofit curve};
\draw (0,0) node[anchor=north east] {$O$};
\draw (1,0) node[anchor=north] {$q_L^*$};
\draw (0,1) node[anchor=east] {$t_L^*$};
\draw (2,0) node[anchor=north] {$q_H^*$};
\draw (0,2) node[anchor=east] {$t_H^*$};
\filldraw [black] (1,1) circle (0.5pt) node {};
\filldraw [black] (2,2) circle (0.5pt) node {};
\draw[help lines, dashed] (1,0) -- (1,1) -- (0,1);
\draw[help lines, dashed] (2,0) -- (2,2) -- (0,2);
\end{tikzpicture}

答案1

使用线时,节点可以更容易定位。选项sloped自动计算旋转角度,例如:

\documentclass{article}

\usepackage{pgf,tikz}
\tikzset{>=latex}

\begin{document}
\begin{tikzpicture}[scale=2.5]
  \draw[help lines, dashed] (1,0) -- (1,1) -- (0,1);
  \draw[help lines, dashed] (2,0) -- (2,2) -- (0,2);
  \draw[->] (-0.5,0) -- (3,0) node[below] {$q$};
  \draw[->] (0,-0.5) -- (0,3) node[left] {$t$};
  \draw[domain=0:3, samples=200, smooth, variable=\x, thick]
    plot ({\x},{sqrt(2*\x)})
  ;
  \path
    (2.2, {sqrt(2*2.2)}) -- (2.3, {sqrt(2*2.3)})
    node[above, font=\small, align=center, midway, sloped] {%
      $\theta_H$-type buyer's
      indiffer-\\ence curve: $\theta_H v(q) = t$%
    };
  % \draw[domain=2.9:0.2, smooth, variable=\x, thin, gray]
  %   plot ({\x},{0.5*\x+1})
  \draw[thin, gray]
    (.2, .5*.2+1) -- (2.9, .5*2.9+1)
    node[above right, pos=0, sloped, font=\small] {Seller's isoprofit curve};
  \draw[domain=0:3, samples=200, smooth, variable=\y, very thick, gray]
    plot ({\y},{sqrt(\y)})
  ;
  \path
    (1.6, {sqrt(1.6)}) -- (2.9, {sqrt(2.9)})
    node[below, black, font=\small, align=center, midway, sloped] {%
      $\theta_L$-type buyer's indiffer-\\ence curve: $\theta_L v(q) = t$%
    };
  %\draw[domain=0.2:2.9, smooth, variable=\y, thin, gray]
  %  plot ({\y},{0.5*\y+0.5})
  \draw[thin, gray]
    (.2, .5*.2+.5) -- (2.9, .5*2.9+.5)
    node[above left, pos=1, sloped, font=\small]
    {Seller's isoprofit curve};
  \draw (0,0) node[anchor=north east] {$O$};
  \draw (1,0) node[anchor=north] {$q_L^*$};
  \draw (0,1) node[anchor=east] {$t_L^*$};
  \draw (2,0) node[anchor=north] {$q_H^*$};
  \draw (0,2) node[anchor=east] {$t_H^*$};
  \filldraw [black] (1,1) circle (0.5pt) node {};
  \filldraw [black] (2,2) circle (0.5pt) node {};
\end{tikzpicture}
\end{document}

结果

或者使用白色背景来将注释覆盖在帮助行上:

\documentclass{article}

\usepackage{pgf,tikz}
\tikzset{>=latex}

\begin{document}
\begin{tikzpicture}[scale=2.5]
  \draw[help lines, dashed] (1,0) -- (1,1) -- (0,1);
  \draw[help lines, dashed] (2,0) -- (2,2) -- (0,2);
  \draw[->] (-0.5,0) -- (3,0) node[below] {$q$};
  \draw[->] (0,-0.5) -- (0,3) node[left] {$t$};
  \draw[domain=0:3, samples=200, smooth, variable=\x, thick]
    plot ({\x},{sqrt(2*\x)})
  ;
  \path
    (2.2, {sqrt(2*2.2)}) -- (2.3, {sqrt(2*2.3)})
    node[above, font=\small, align=center, midway, sloped,
      yshift=.5pt, fill=white] {%
      $\theta_H$-type buyer's
      indiffer-\\ence curve: $\theta_H v(q) = t$%
    };
  % \draw[domain=2.9:0.2, smooth, variable=\x, thin, gray]
  %   plot ({\x},{0.5*\x+1})
  \draw[thin, gray]
    (.2, .5*.2+1) -- (2.9, .5*2.9+1)
    node[above right, pos=0, sloped, font=\small] {Seller's isoprofit curve};
  \draw[domain=0:3, samples=200, smooth, variable=\y, very thick, gray]
    plot ({\y},{sqrt(\y)})
  ;
  \path
    (1.6, {sqrt(1.6)}) -- (2.9, {sqrt(2.9)})
    node[below, black, font=\small, align=center,
      midway, sloped, yshift=-.5ex, fill=white] {%
      $\theta_L$-type buyer's indiffer-\\ence curve: $\theta_L v(q) = t$%
    };
  %\draw[domain=0.2:2.9, smooth, variable=\y, thin, gray]
  %  plot ({\y},{0.5*\y+0.5})
  \draw[thin, gray]
    (.2, .5*.2+.5) -- (2.9, .5*2.9+.5)
    node[yshift=.5pt, fill=white, above left, pos=1, sloped, font=\small]
    {Seller's isoprofit curve};
  \draw (0,0) node[anchor=north east] {$O$};
  \draw (1,0) node[anchor=north] {$q_L^*$};
  \draw (0,1) node[anchor=east] {$t_L^*$};
  \draw (2,0) node[anchor=north] {$q_H^*$};
  \draw (0,2) node[anchor=east] {$t_H^*$};
  \filldraw [black] (1,1) circle (0.5pt) node {};
  \filldraw [black] (2,2) circle (0.5pt) node {};
\end{tikzpicture}
\end{document}

结果

相关内容