2

I would like to have two lines of text with my time line below. How could I do this:

MWE:

 \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}

OUTPUT: enter image description here

Problem:

When I try to put a new line \\ so that 'Lesson starts' appear on top of each other with the command \draw (0,0) node[below=3pt] {00' } node[above=3pt] {Lesson\\starts}; I get this: enter image description here

Question:

How would I change the above code so "Lesson" is on top of "starts"?

3
  • Line breaks are only supported if the node has a specified width
    – daleif
    Jun 17, 2019 at 11:43
  • 1
    Off topic. You should use the {$00^\prime$} instead of {00'}.
    – user31034
    Jun 17, 2019 at 11:50
  • @ferahfezaThank you!
    – 3kstc
    Jun 17, 2019 at 11:52

3 Answers 3

5

By default, the text of a node is put in an \hbox, and therefore occupies only one line. There are several ways to do what you want, one of which is to specify align=left (or similar) for your multiline text node:

 \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}

Screenshot

For details and other techniques, see Text Parameters: Alignment and Width for Multi-Line Text in the PGF & TikZ manual (page 229 for version 3.1.3).

Briefly, the other techniques given in the manual are:

  • use a tabular inside your node:

    \draw (0,0) node[below=3pt] {00' } node[above=3pt]
      {%
        \begin{tabular}{@{}l@{}}
          Lesson\\
          starts
        \end{tabular}%
      };
    
  • ask for automatic line-breaking inside the node by specifying a fixed text width:

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

Play with the width of the text:

 [![\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}

enter image description here

2

One more similar solution with slightly more compact code:

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .