How would I search within an angular distance of an object corresponding to a distance in parsecs?

I'm interested in the learning about the local densities around galaxies. I've found myself a bit confused on how to relate angular distances (arcseconds) with physical distances (parsecs) when thinking about distances between two distant galaxies. For example, let's say the average distance between galaxies within clusters is a few megaparsecs. If I had ra, dec coordinates of a particular galaxy and could hypothetically query an all-sky catalog, let's say I'd want to search within a few megaparsecs to get a feel for its density field. But generally when querying catalogs you specify a search radius in some arcseconds. How would these relate here?

Ken G's answer is essentially correct, but there is one important thing to keep in mind: The distance $$r$$ between the galaxies that is interesting for you is the distance they had at the time they emitted the light you see (you don't care about what has happened to them since that time and how far apart they are today; you care about the physical properties then). But the angle you observe them to span is not angle they spanned when they emitted the light (since they were closer to you in the past, so the light they emitted in your direction at that time would "miss" you by the time it reached that place).

This means that, in your calculation, the distance from you to the galaxies should not be the "physical" distance $$d$$ (i.e. the actual distance right now to the galaxies, which also corresponds to the comoving distance), but the so-called angular diameter distance, $$d_A$$. These are related (in a flat universe) by $$d_A = \frac{d}{1+z},$$ where $$z$$ is the redshift of the galaxies (I assume they have almost the same redshift; if they are too different, then the distance from each other is not simply given by the projected distance). Note that in a non-flat universe, the formula is slightly more complicated; see the link above.

The comoving distance, in turn, is obtained by integration: $$d(z) = \frac{c}{H_0} \int_0^z \frac{dz'}{\left( \Omega_\mathrm{M}(1+z)^3 + \Omega_\Lambda \right)^{1/2}},$$ where $$c$$ is the speed of light, and $$\Omega_\mathrm{M}$$, $$\Omega_\Lambda$$, and $$H_0$$ are the present matter and dark energy density parameters and Hubble constant, respectively. Here, I've again assumed a flat universe, and neglected radiation since when that was important, there were no galaxies anyway).

Obviously, this effect is not big for small redshifts, but $$d_A$$ has the interesting property that it increases with physical distance only out to a certain point (roughly $$z=1.6$$), after which it decreases, because the galaxies were closer to us in the past.

So the physical distance $$r$$ between two galaxies spanning an angle $$\theta$$ is $$r = \frac{\theta}{d_A}$$ or $$\boxed{ \frac{r}{\mathrm{kpc}} = \frac{\theta / \mathrm{arcsec}}{d / \mathrm{Mpc}} (1+z) \, \frac{10^{-3} \times 180\times3600}{\pi}, }$$ where the last factor accounts for converting Mpc to kpc and radians to arcseconds.

In Python, arbitrarily for $$z=2.2$$ and $$\theta=12.3''$$, it's as easy as

from astropy.cosmology import Planck15
from astropy import units as u
z     = 2.2                               # redshift of galaxies
theta = 12.3 * u.arcsec                   # angle
r_ang = Planck15.kpc_proper_per_arcmin(z) # phys. dist. per angle
r     = r_ang * theta                     # physical distance
print('Distance between galaxies: ', r.to(u.kpc))


which will print Distance between galaxies: 104.22540165050975 kpc.

Remember that an arcsecond is an angle, so imagine a pie wedge that extends out from your observatory, having that angle inside the point of the pie wedge. That's all you can tell by looking at the sky, the angle in that pie wedge. Also, notice that a field on the sky is not a line, it's more like a circle, so the pie wedge is really a cone with that angle in it. But you don't know from just searching how far away anything is, so if you want to search on distance, then you have to go through and determine the distance to everything in that search angle.

Let's say you center your search on a particular galaxy that you do know the distance to. Now you have a search cone that has its point at your observatory, and the galaxy in question at its center some known distance away. But within that angular search cone, you may see other objects at all kinds of distances. There's no easy way to limit to things that have a similar distance as that galaxy, you get the whole cone unless you go through and find the distance to everything and throw out what you don't want.

If you want the galaxy density at nearly the same distance as that galaxy, then you will have to throw out things that are not nearly at the same distance. Perhaps you have a lower and upper distance cutoff, so now you are searching a section from that cone, but it's not a sphere within some given distance from that galaxy, that shape would be much harder to figure out. Still, the section I was talking about does have a volume, and you can count the galaxies within it, so that does give you a galaxy density around that galaxy.

To figure out that volume, you would need the linear distance across the cone at the distance to that galaxy. That sounds like what you are asking about. That is simple geometry, because the connection between angles and distances can be seen from the formula for the circumference of a circle. The angle all the way around the center point of the circle is 2*Pi radians, and the circumference is 2*Pi*r, so that tells you that you take the angle in radians (you have to convert from arcsec) and multiply by the radius to get the length of the arc across from that angle. That's not just true for 2*Pi radians, it's true for any smaller angle as well, as you know if you've ever sliced up a pizza.

So for your application, the r is the distance to the galaxy, and the angle (converted into radians) is the angle of your search field, and when you multiply them you get the length of the arc that crosses the cone we are talking about, at the distance to that galaxy. That length is the key to figuring out the volume you are counting galaxies in, as the rest of its dimensions depend on what distances you are using to limit what you are counting within that field.