Learn How to Draw a Cylinder Shape in TikZ

  • In this tutorial, we will learn how to draw a cylinder shape in TikZ using shapes.geometric library. This includes changing its size and its color, adding text and access to its border coordinates.

Node shape cylinder in TikZ

Cylinder shape is a part of the shapes.geometric TikZ library, so to work with this shape, we need to declare the library. We need to add the following piece of code after the TikZ package declaration:

\usetikzlibrary{shapes.geometric}

A cylinder shape can be drawn by using the option cylinder to the \node command. The following code creates a cylinder node named (c) at coordinate (0,0).

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder] (c) at (0,0) {};

\end{tikzpicture}

\end{document}

Compiling this line of code displays nothing. The cylinder node will be drawn only if we provide draw option to the node command as follows:

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, draw] (c) at (0,0) {};

\end{tikzpicture}

\end{document}
cylinder shape in TikZ

Add text to the cylinder node

The previous code creates a cylinder node with empty content. Text can be added inside the cylinder node by putting it between the curly braces {} as follows.

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, draw] (c) at (0,0) {Cylinder};

\end{tikzpicture}

\end{document}

To fit the text inside the shape, this line of code yields to a cylinder with a size depends on its content:

Add text to cylinder shape in TikZ

Change text color of the cylinder node

In the previous section, we have added content to the cylinder node and now we would like to change the text color. This can be achieved by providing the option text = <color> to the \node command:

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, draw, text = purple] (c) at (0,0) {Cylinder};

\end{tikzpicture}

\end{document}
Change text color cylinder shape TikZ

Add filling color to a node

The cylinder node can be filled with any color using the option fill = <color>. Here is an illustrative example of a light magenta filling with our purple text:

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, 
	draw, 
	text = purple,
	fill = magenta!20] (c) at (0,0) {Cylinder};

\end{tikzpicture}

\end{document}
Fill cylinder shape with color in TikZ
  • We can choose to have two different filling colors for the body and end parts of the cylinder. First, we need to use cylinder uses custom fill option to declare we will have two fillings. Then we can fill the area we want using cylinder body fill and cylinder end fill options.

Here, we will fill the cylinder body with the same light tone of magenta, and the end part with a darker tone of magenta than the body.

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, 
	draw, 
	text = purple,
	cylinder uses custom fill, 
	cylinder body fill = magenta!10, 
	cylinder end fill = magenta!40] (c) at (0,0) {Cylinder};

\end{tikzpicture}

\end{document}
Fill cylinder different parts shape with color in TikZ

Change the border line color of a node

The cylinder border color can be modified (default is black) by providing the color name to the draw option draw = <color>. Here is an example of a cylinder filled with light magenta, has purple text and violet borders:

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, 
	draw = violet, 
	text = purple,
	cylinder uses custom fill, 
	cylinder body fill = magenta!10, 
	cylinder end fill = magenta!40] (c) at (0,0) {Cylinder};

\end{tikzpicture}

\end{document}
Change border color cylinder shape TikZ

Change the size of a cylinder node

We mentioned above that the size of a cylinder depends on its content. This is always true and we can set only the minimum size of a cylinder. We can arrange a minimum size for its width with minimum width option, and similarly, we can set its height by using minimum height option. For a cylinder shape, width is the distance between the straight sides and height is the distance between the curly sides. Below, we set the minimum width value to 1 cm and the minimum height value to 2.5 cm.

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, 
	draw = violet, 
	text = purple,
	cylinder uses custom fill, 
	cylinder body fill = magenta!10, 
	cylinder end fill = magenta!40,
	minimum width = 1cm,
	minimum height = 2.5cm] (c) at (0,0) {Cylinder};

\end{tikzpicture}

\end{document}
Change size of cylinder shape TikZ

If we need both the width and the length sizes to be the same, we can use minimum size option. Following line of code sets both the sizes to 2 cm.

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, 
	draw = violet, 
	text = purple,
	cylinder uses custom fill, 
	cylinder body fill = magenta!10, 
	cylinder end fill = magenta!40,
	minimum size = 2cm] (c) at (0,0) {Cylinder};

\end{tikzpicture}

\end{document}
Change size proportional of cylinder shape TikZ
  • We can also change the width/height ratio of the ellipse on the cylinder end, using the aspect option. By default, the option is set to 1. This option may be ignored if the minimum width option is also used. To have a slimmer ellipse, we can set it to a parameter smaller than 1. Check the code below!
\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, 
	draw = violet, 
	text = purple,
	cylinder uses custom fill, 
	cylinder body fill = magenta!10, 
	cylinder end fill = magenta!40,
	aspect=0.25] (c) at (0,0) {Cylinder};

\end{tikzpicture}

\end{document}
Ellipse aspect ratio cylinder shape TikZ

Rotate the cylinder shape

In some cases we might need a standing cylinder. We can rotate the cylinder node shape without rotating its contents with shape border rotate option. Here is an example of a 90 degrees rotated cylinder, with an aspect of 0.2.

\documentclass[border=0.2cm]{standalone}

\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\begin{document}

\begin{tikzpicture}

\node[cylinder, 
	draw = violet, 
	text = purple,
	cylinder uses custom fill, 
	cylinder body fill = magenta!10, 
	cylinder end fill = magenta!40,
	aspect = 0.2, 
	shape border rotate = 90] (c) at (0,0) {Cylinder};

\end{tikzpicture}

\end{document}
Standing cylinder shape TikZ

Anchors of a cylinder node

The advantage of using a node cylinder is that it defines a set of anchors that we can use to get coordinates of the node borders or to position nodes with accuracy with respect to given coordinates. By default, node center is positioned at the provided coordinates ((0,0) for the previous examples).

Anchors of a cylinder shape TikZ
Anchors of a cylinder shape TikZ (angles)
  • (c.east) represents the coordinates of the point located at the east of the cylinder node where c is the node name. We can also use angles where (c.270) is the coordinates of the point located at the south of the cylinder node.
  • Adding anchor = north will place the node point (c.north) at the provided coordinate (0,0). Check this post: how to annotate an image in LaTeX (Positioning Nodes using Anchors section).

Outer separation

The above anchors can be moved outside the node border by adding outer sep = <value> to the node command. Here is an example of anchors moved outside the node border with 10pt.

Push anchors outside cylinder shape TikZ
  • We reached the end of this post, If you have any questions or suggestionsleave me a comment or reach me via email at admin@latexdraw.com, I will be happy to hear from you!