LaTeX : The TikZ package

Tikz is probably the most complex and powerful tool to create graphic elements in LaTeX. The TikZ package allows you to draw high quality shapes and diagrams. To start using it, include the tikz package in your LaTeX document using the command :

 \usepackage{tikz}

To create any tikz diagram or picture, you need to create the tikzpicture environment using the command:

 \begin{tikzpicture} 

This begins the picture environment and you can then write various commands to draw the figure you require.
Once it is done, you can end the same with :

 \end{tikzpicture} 

The draw command

One of the simplest and most commonly used command in tikz is the ‘draw’ command.

  • To draw a line
     \draw (start-point) -- (end-point);  

    For Example,

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

    where (0,0) is the starting co-ordinate and (5,0) is the ending co-ordinate.

  • To draw a square

    You can add more coordinates to the above command to make it a square :

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

    Or instead use the ‘cycle’ keyword ( because we are ending at the same point from where we started ) instead of the last coordinate as :

     \draw (0,0) -- (5,0) -- (5,5) -- (0,5) -- cycle; 

    To simplify further, we can use the rectangle keyword as :

     \draw (start-point) rectangle (end-point); 

    For Example,

     \draw (0,0) rectangle (5,5); 
  • To draw a curved line
     \draw (start-point) .. controls (control-points separated by and) .. (final-point); 

    For Example,

     \draw (0,0) .. controls (0,5) and (5,0) .. (5,5); 

    The control points act like magnets attracting the line towards it.
    Running this command after drawing the rectangle as described in previous point would give us the following output :
    Screenshot from 2014-08-31 17:16:32

  • To draw an arc
     \draw (0,0) arc (180:75:3cm); 

    where (0,0) is the starting point and the final bracket goes as (starting-angle:ending-angle:radius).
    Drawing an arc with this gives the following output :

    Screenshot from 2014-09-02 02:04:14

  • To draw a circle
     \draw (centre-point) circle (radius); 

    For example,

     \draw (2,3) circle (4cm);
  • To draw an ellipse
     \draw (2,2) ellipse ( 3cm and 1cm); 

    where (2,2) is the centre point and 3cm and 1cm is the x and y direction radius respectively.

  • Customizing the lines and shapes

    To customize the way these lines and shapes are drawn, we add extra arguments to the draw command.

    For Example,

     \draw[red,thick,dashed] (0,0) rectangle (5,5); 

    would give the following output :
    Screenshot from 2014-09-02 02:17:35

    Filling in colors

    • The \filldraw command

      The \filldraw command is used to draw elements and fill them with some specific colour.

      Example,

       \filldraw[red] (0,0) rectangle (5,5); 

      This gives the result as :
      Screenshot from 2014-09-02 03:00:40

    • The argument fill

      Alternatively, you can fill the shapes with colors as per your requirement using the fill argument.

      For Example,

       \draw[blue,thick,dashed,fill=red] (0,0) rectangle (5,5); 

      Or

        \draw[blue,thick,style=dashed,fill=red] (0,0) rectangle (5,5); 

      would give the result as shown:
      Screenshot from 2014-09-02 02:30:58

      Now, if you want to lighten the red color filled in the entire area of the rectangle or square, you may use the symbol ‘!’. For Example,

       \draw[blue,thick,dashed,fill=red!75] (0,0) rectangle (5,5); 

      where red!75 sets the color to 75% of red, as shown below :
      Screenshot from 2014-09-02 02:41:39

      Another Example,

       \draw[thick,dashed,blue!75, fill=red!75] (0,0) rectangle (5,5); 

      Or

       \draw[thick,dashed,color=blue!75, fill=red!75] (0,0) rectangle (5,5); 

      sets the color of the outline that surrounds the square to 75% of blue.

    All this sums up the basic part. These elements can be combined to create more elaborated figures.
    For an Example, the figure shown below is a combination of simple and curved lines.
    Screenshot from 2014-09-02 03:11:46

    The commands go as:

    \begin{tikzpicture}
     
    \draw (-2,0) -- (2,0);
    \filldraw [gray] (0,0) circle (2pt);
    \draw (-2,-2) .. controls (0,0) .. (2,-2);
    \draw (-2,2) .. controls (-1,0) and (1,0) .. (2,2);
     
    \end{tikzpicture}

    Explaining things :

    \draw (-2,0) — (2,0);
    This defines a line whose endpoint are (-2,0) and (2,0).

    \filldraw [gray] (0,0) circle (2pt);
    The point is created as a very small gray circle centred at (0,0) and whose radius is (2pt).

    \draw (-2,2) .. controls (-1,0) and (1,0) .. (2,2);
    Draws a Bézier curve (the name sounds bit tricky 😛 ; call it a curved line for convenience 😉 ). There are 4 points defining it: (-2,2) and (2,2) are its endpoints, (-1,0) and (1,0) are control points (can be equal) that determine ‘how curved’ it is. As stated earlier, you can think of these two points as “attractor points”.

    You may now proceed to create such more of complex and interesting things. Feel free to comment and show what nice things you have done.
    Cheers ! 🙂

One thought on “LaTeX : The TikZ package

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: