tikzpicture 中的文本对齐

tikzpicture 中的文本对齐

我想在下方显示两行文字和时间线。我该怎么做:

梅威瑟:

 \documentclass{article}
 \usepackage{tikz}

 \begin{document}

 \begin{tikzpicture}
 % draw horizontal line   

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

   % draw vertical line
   \foreach \x in {0,2,9,12}
   \draw (\x cm,3pt) -- (\x cm,-3pt);

   % draw nodes
   \draw (0,0) node[below=3pt] {00' } node[above=3pt] {Starts};
   % \draw (0,0) node[below=3pt] {00' } node[above=3pt] {Lesson\\starts};  % <- !
   \draw (2,0) node[below=3pt] {10'} node[above=3pt] {Pre-text};
   \draw (9,0) node[below=3pt] {45'} node[above=3pt] {Text};
   \draw (12,0) node[below=3pt] {60'} node[above=3pt] {Ends};

 \end{tikzpicture}

 \end{document}

输出在此处输入图片描述

问题

当我尝试使用\\命令添加新行以便“课程开始”出现在彼此的顶部时,\draw (0,0) node[below=3pt] {00' } node[above=3pt] {Lesson\\starts};我得到了以下信息: 在此处输入图片描述

问题

我该如何更改上述代码以便“Lesson”位于“starts”之上?

答案1

默认情况下,节点的文本放在 中\hbox,因此只占用一行。有几种方法可以实现您想要的功能,其中一种是align=left为多行文本节点指定(或类似):

 \documentclass{article}
 \usepackage{tikz}

 \begin{document}

 \begin{tikzpicture}
   % draw horizontal line

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

   % draw vertical lines
   \foreach \x in {0, 2, 9, 12} \draw (\x cm,3pt) -- (\x cm,-3pt);

   % draw nodes
   \draw (0,0) node[below=3pt] {00' } node[above=3pt, align=left] {Lesson\\starts};  % <- !
   \draw (2,0) node[below=3pt] {10'} node[above=3pt] {Pre-text};
   \draw (9,0) node[below=3pt] {45'} node[above=3pt] {Text};
   \draw (12,0) node[below=3pt] {60'} node[above=3pt] {Ends};

 \end{tikzpicture}

 \end{document}

截屏

有关详细信息和其他技术,请参阅文本参数:多行文本的对齐方式和宽度在里面前列腺素和钛Z 手册(版本 3.1.3 为第 229 页)。

简而言之,手册中给出的其他技术包括:

  • tabular在节点内使用:

    \draw (0,0) node[below=3pt] {00' } node[above=3pt]
      {%
        \begin{tabular}{@{}l@{}}
          Lesson\\
          starts
        \end{tabular}%
      };
    
  • 通过指定固定的来要求节点内部自动换行text width

    \draw (0,0) node[below=3pt] {00' } node[above=3pt, text width=3em]
      {Lesson starts};
    

答案2

调整文本的宽度:

 [![\documentclass{article}
 \usepackage{tikz}

 \begin{document}

 \begin{tikzpicture}
 % draw horizontal line

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

   % draw vertical line
   \foreach \x in {0,2,9,12}
   \draw (\x cm,3pt) -- (\x cm,-3pt);

   % draw nodes
   \draw (0,0) node\[below=3pt\] {00' } node\[above=3pt,text width = 1cm, align=center\] {Starts New };
   % \draw (0,0) node\[below=3pt\] {00' } node\[above=3pt\] {Lesson\\starts};  % <- !
   \draw (2,0) node\[below=3pt\] {10'} node\[above=3pt\] {Pre-text};
   \draw (9,0) node\[below=3pt\] {45'} node\[above=3pt\] {Text};
   \draw (12,0) node\[below=3pt\] {60'} node\[above=3pt\] {Ends};

 \end{tikzpicture}

 \end{document}

在此处输入图片描述

答案3

还有一个类似的解决方案,代码稍微紧凑一些:

\documentclass{article}
\usepackage{tikz}

\begin{document}
    \begin{tikzpicture}[
every node/.style = {align=center, text width=3em}
                        ]
 % draw horizontal line
\draw (0,0) -- (12,0);
% draw vertical lines woith nodes
\draw ( 0,-3pt) node[below] {00'} -- ++ (0,6pt) node[above] {Lesson starts};
\draw ( 2,-3pt) node[below] {10'} -- ++ (0,6pt) node[above] {Pre-text};
\draw ( 9,-3pt) node[below] {45'} -- ++ (0,6pt) node[above] {Text};
\draw (12,-3pt) node[below] {60'} -- ++ (0,6pt) node[above] {Ends};
    \end{tikzpicture}
 \end{document}

在此处输入图片描述

相关内容