# How to draw moon phases in TikZ

I am trying to draw a diagram of the phases of the moon. I found some code here, and adapted it so the styling is consistent with the other diagrams in my document. Here is what I have so far:

\documentclass{article}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
\draw[dashed, color=black!20] (0,0) circle(2.5);
\draw (0,0) circle(1) node {The Earth};
\foreach \x in {0,45,...,360} {
\filldraw[fill=white] (\x:2.5cm) circle(0.25cm);
\filldraw[fill=white] (\x:3.5cm) circle(0.5cm);
\draw[fill=black] (\x:2.5cm)-- +(0,0.25cm) arc (90:-90:0.25cm) -- cycle;
}
\end{tikzpicture}

\end{document}


I am struggling to figure out how to add the correct moon phases to the outer sections. Can anyone help explain a) How I can draw the phases and b) How to position them in the correct circles. Thanks so much in advance.

EDIT: To be more clear, I want the end result to look somewhat like this:

• Did you see tex.stackexchange.com/questions/327796/…? Maybe duplicate? Apr 30, 2021 at 13:59
• @Marijn I tried to, but couldn't figure out how to resize it and change other styles. Apr 30, 2021 at 14:25
• The moon's rotation is perfectly aligned with the sun, so the terminator will always lie on lines of longitude. OTOH, its orbit does not align with earth's so it may need to be rotated north or south. Apr 30, 2021 at 14:56
• Welcome to TeX.se. Instead of posting a code fragment, please post a compilable document that includes the fragment. Since you have built the code from an existing answer, please also link to that answer in your question so that the original author is acknowledged. It would also be helpful to explain exactly what problems you're having with that code in particular. Apr 30, 2021 at 16:52

It's not too bad with some clipping and coordinate calculations:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\draw[dashed, color=black!20] (0,0) circle(2.5);
\draw (0,0) circle(1) node {The Earth};
\foreach \x in {0,45,...,360} {
\filldraw[fill=white] (\x:2.5cm) circle(0.25cm);
\draw[fill=black] (\x:2.5cm)-- +(0,0.25cm) arc (90:-90:0.25cm) -- cycle;
}
% New Moon
\draw[fill=black] (0:3.5cm) circle(0.5cm);
\node at ($(0:3.5) + (0, 0.7)$) {\small New Moon};
% Waxing Crescent
\begin{scope}
\draw[clip] (45:3.5cm) circle(0.5cm);
\draw[fill=black] ($(45:3.5cm) - (0.25, 0)$) circle(0.5cm);
\end{scope}
\node at ($(45:3.5) + (0, 0.7)$) {\small Waxing Crescent};
% First Quarter
\begin{scope}
\draw[clip] (90:3.5cm) circle(0.5cm);
\draw[fill=black] (0, 3) rectangle (-0.5, 4);
\end{scope}
\node at ($(90:3.5) + (0, 0.7)$) {\small First Quarter};
% Waxing Gibbous
\begin{scope}
\draw[fill=black] (135:3.5) circle(0.5);
\draw[clip] (135:3.5) circle(0.5);
\draw[fill=white] ($(135:3.5) + (0.25, 0)$) circle(0.5);
\end{scope}
\node at ($(135:3.5) + (0, 0.7)$) {\small Waxing Gibbous};
% Full Moon
\draw (180:3.5) circle(0.5);
\node at ($(180:3.5) + (0, 0.7)$) {\small Full Moon};
% Waning Gibbous
\begin{scope}
\draw[fill=black] (225:3.5) circle(0.5);
\draw[clip] (225:3.5) circle(0.5);
\draw[fill=white] ($(225:3.5) - (0.25, 0)$) circle(0.5);
\end{scope}
\node at ($(225:3.5) - (0, 0.7)$) {\small Waning Gibbous};
% Last Quarter
\begin{scope}
\draw[clip] (270:3.5) circle(0.5);
\draw[fill=black] (0, -3) rectangle (0.5, -4);
\end{scope}
\node at ($(270:3.5) - (0, 0.7)$) {\small Last Quarter};
% Waning Crescent
\begin{scope}
\draw[clip] (305:3.5) circle(0.5);
\draw[fill=black] ($(305:3.5) + (0.25, 0)$) circle(0.5);
\end{scope}
\node at ($(305:3.5) - (0, 0.7)$) {\small Waning Crescent};
\end{tikzpicture}
\end{document}


For each phase I'm first drawing a circle at the correct point and using the clip option which means that nothing outside of that circle (and in the same scope) will be drawn. Then simply place a circle/rectangle on top filled in black.

• Thank you so much! Apr 30, 2021 at 17:55

Another approach. It was tricky to made it with a \foreach but I knew it should be a way. I did not know that I could give negative radii in the arcs, though.

This is my solution:

\documentclass[border=2mm]{standalone}
\usepackage    {tikz}
\usetikzlibrary{calc}

\newcommand\moon[4] % center, radius, color, rotation (-180,...,180),
{%
\ifnum #4 > 0
\pgfmathsetmacro\lb{ #2}            % left  arc, horizontal axis
\pgfmathsetmacro\rb{ #2*(90-#4)/90} % right arc, horizontal axis
\else
\pgfmathsetmacro\lb{-#2*(90+#4)/90} % left  arc, horizontal axis
\pgfmathsetmacro\rb{-#2}            % right arc, horizontal axis
\fi
\draw[thick,#3,fill=white] #1 circle (#2);
\fill[#3,opacity=0.5] ($#1+(0,#2)$) arc (90:270:\lb cm and #2 cm)
arc (270:90:\rb cm and #2 cm);
}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round,x={(-1cm,0cm)}]
\moon{(0,0)}{1}{blue}{90}{Earth}
\draw[dashed] (0,0) circle (3);
\foreach\i in {-180,-135,...,180}
{
\moon{(\i:3)}{0.25}{black}{90}
\moon{(\i:5)}{0.75}{black}{\i}
}
\node at    (0,0)                {Earth};
\node at (-135:5) [yshift=-1 cm] {waning crescent};
\node at  (-90:5) [yshift=-1 cm] {last quarter};
\node at  (-45:5) [yshift=-1 cm] {waning gibbous};
\node at    (0:5) [yshift= 1 cm] {full moon};
\node at   (45:5) [yshift= 1 cm] {waxing gibbous};
\node at   (90:5) [yshift= 1 cm] {first quarter};
\node at  (135:5) [yshift= 1 cm] {waxing crescent};
\node at  (180:5) [yshift= 1 cm] {new moon};
\end{tikzpicture}
\end{document}


• Thank you so much! I really appreciate all the help this community is giving me! May 1, 2021 at 11:53

The tikz-planets package would seem to be exactly what you're looking for.

• Welcome to TeX.SX! Instead of just "linking" to a package it would be nice to at least give a simplified example of the answer, e.g. showing the code for just one moon position. May 4, 2021 at 5:23
• Whoever did the downvote: This is a new user and when you downvote it would be nice to add as a comment also why you downvoted. Otherwise this new user has no clue what to do better. Otherwise I don't think that he/she is motivated to provide further answers ... May 4, 2021 at 5:26