在 Tikz 时间线上调整文本角度

在 Tikz 时间线上调整文本角度

我对 Latex 还不太熟悉,正在尝试排版一些财务笔记。如何在使用 Tikz 制作的时间线上倾斜文本或数学方程式?

这就是我所拥有的:

\begin{tikzpicture}
 % draw horizontal line   

  \draw (0,0) -- (10,0);



  % draw vertical lines
   \foreach \x in {0,1.5,3,4.5,8.5,10}
   \draw (\x cm,3pt) -- (\x cm,-3pt);

    % draw nodes
   \draw (0,0) node[below=3pt] {$T_0$ } node[above=3pt] {};
   \draw (1.5,0) node[below=3pt] {$T_1$} node[above=3pt] {P};
   \draw (3,0) node[below=3pt] {$T_2$} node[above=3pt] {P+Q};
   \draw (4.5,0) node[below=3pt] {$T_3$} node[above=3pt] {P+2Q};
   \draw (8.5,0) node[below=3pt] {$T_{n-1}$} node[above=3pt] {[P+(n-2)Q]};
   \draw (10,0) node[below=3pt] {$T_n$} node[above=3pt] {[P+(n-1)Q]};

   \end{tikzpicture}

结果如下:在此处输入图片描述

有没有什么方法可以让我调整公式的末端的角度,使它们不重叠?

答案1

仅旋转两个标签可能会非常难看,您可能希望以相同的方式旋转所有标签。

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{graphicx,tikz}
\begin{document}
\begin{tikzpicture}[scale=1,rotLabel/.style={above,rotate=30,anchor=200}]
 % draw horizontal line 
  \draw (0,0) -- (10,0);
  % draw vertical lines
   \foreach \x in {0,1.5,3,4.5,8.5,10}
   \draw (\x cm,3pt) -- (\x cm,-3pt);
    % draw nodes
   \draw (0,0) node[below=3pt] {$T_0$ } node[rotLabel] {};
   \draw (1.5,0) node[below=3pt] {$T_1$} node[rotLabel] {P};
   \draw (3,0) node[below=3pt] {$T_2$} node[rotLabel] {P+Q};
   \draw (4.5,0) node[below=3pt] {$T_3$} node[rotLabel] {P+2Q};
   \draw (8.5,0) node[below=3pt] {$T_{n-1}$} node[rotLabel] {[P+(n-2)Q]};
   \draw (10,0) node[below=3pt] {$T_n$} node[rotLabel] {[P+(n-1)Q]};
   \end{tikzpicture}
\end{document}

在此处输入图片描述

另一个选项是将最后两个标签中的一个稍微提高一点。只有最后一个标签会像这样改变(所有其他标签above=3pt都变为公正above,所有标签below=3pt都变为公正below):

\draw (10,0) node[below=3pt] {$T_n$} node[above=15pt] {[P+(n-1)Q]};
\draw[<-] (10,0.2) -- ++(0,1em);

在此处输入图片描述

答案2

您可以使用rotate,将要旋转的节点锚定在东南方。

编辑:当然,如果您愿意,您可以旋转所有节点。

\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
% draw horizontal line   
\draw (0,0) -- (10,0);

% draw vertical lines
\foreach \x in {0,1.5,3,4.5,8.5,10}
\draw (\x cm,3pt) -- (\x cm,-3pt);

% draw nodes
\draw (0,0) node[below=3pt] {$T_0$ } node[above=3pt] {};
\draw (1.5,0) node[below=3pt] {$T_1$} node[above=3pt] {P};
\draw (3,0) node[below=3pt] {$T_2$} node[above=3pt] {P+Q};
\draw (4.5,0) node[below=3pt] {$T_3$} node[above=3pt] {P+2Q};
\draw (8.5,0) node[below=3pt] {$T_{n-1}$} node[above=3pt, anchor= south east, rotate=-45] {[P+(n-2)Q]};
\draw (10,0) node[below=3pt] {$T_n$} node[above=3pt, anchor= south east, rotate=-45] {[P+(n-1)Q]};

\end{tikzpicture}

\vspace{30pt}As AboAmmar said, you could also rotate all the labels:

\begin{tikzpicture}[mylabel/.style={above=3pt, anchor= south east, rotate=-45}]
% draw horizontal line   
\draw (0,0) -- (10,0);

% draw vertical lines
\foreach \x in {0,1.5,3,4.5,8.5,10}
\draw (\x cm,3pt) -- (\x cm,-3pt);

% draw nodes
\draw (0,0) node[below=3pt] {$T_0$ } node[mylabel] {};
\draw (1.5,0) node[below=3pt] {$T_1$} node[mylabel] {P};
\draw (3,0) node[below=3pt] {$T_2$} node[mylabel] {P+Q};
\draw (4.5,0) node[below=3pt] {$T_3$} node[mylabel] {P+2Q};
\draw (8.5,0) node[below=3pt] {$T_{n-1}$} node[mylabel] {[P+(n-2)Q]};
\draw (10,0) node[below=3pt] {$T_n$} node[mylabel] {[P+(n-1)Q]};

\end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

第一个例子的修改阿博阿马尔答案......使用了两个循环,用于上述标签的数学环境......

\documentclass[12pt,a4paper]{article}
\usepackage{tikz}
\usetikzlibrary{positioning}

\begin{document}
    \begin{tikzpicture}[node distance =2mm]
% draw horizontal line
  \draw (0,0) -- (10,0);
% draw vertical lines
\foreach \x/\j [count=\xx] in {0/0, 1.5/1, 3/2, 4.5/,8.5/{n-1}, 10/n}
   \draw (\x,3pt) -- +(0,-6pt) node (q\xx) [below] {$T_{\j}$};
\foreach \x [count=\xx from 2] in {P, P+Q, P+2Q, P+(n-2)Q, P+(n-2)Q}
    \node [above=of q\xx, rotate=45, anchor=south west] {$\mathsf{\x}$};
   \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容