Hello everyone!

Sometime ago I saw an astrophoto of a galaxy, and in the image description was written something like "this galaxy is X light years in diameter and it's Y light years from earth. Taking a picture of this galaxy is like taking a picture of a 10 centimeter pen Z miles away." And I'd like to know how to calculate this so I can do similar comparisons with other objects.

I've tried some formulas but nothing that I was sure about the result. I'm having trouble with the units of measure, like using kilometers for distance and the real size of the galaxy but baing forced to use arc minutes for the apparent size of it.

If anyone knows how to do the math, I'd be very greatful.