Work done by an ideal gas expressed as change in potential energy of fluid I get confused in problems where it is necessary to evaluate the work done by a gas that someway moves a mass. I'll make an example.

Consider the tube containing mercury and an ideal gas in A (Picture
  $(1)$). If the gas expands and increases its volume, what is the work
  done by the gas?


My reasoning, illustrated in $(2)$ and $(3)$ would be: since the only effect of the expansion is to "move" the red volume of Hg from the initial position to an higher position in the thin tube, then the work done by the gas equals the amount of change in potential energy of the red volume of Hg, that's all, there is no other work done by the gas.
It seems right but also a bit contractictory. Suppose for istance that the section of the tube was the same in the two parts. Then there would be still change in the potential energy of the red volume of Hg, but it seems to me that in that case nothing happens, as the Hg "goes down" and "goes up" of the same height, and the work should be zero.
If all the previous is right, then my question is: is it correct to think about such situations in this way? I mean to look at small volumes of the fluid that overall change their position, whitout caring about the rest of the fluid, to evaluate the change in potential energy?
 A: Work is always force times displacement in the direction of the force.  The only place where the gas is doing work is at the bottom surface that is moving downward.  The force it is exerting there is $PA$, where $P$ is the gas pressure and $A$ is the cross sectional area of the tube.  If the lower surface moves downward a differential distance dx, the work done by the gas is $dW=PA \ dx$.  But $A\ dx$ is the change in volume $dV$ of the gas.  Therefore, the differential amount of work that the gas does is $P\ dV$.  You just integrate this to get the total amount of work done.
A: Your intuition that the same amount of fluid goes down and then up by the same amount is incomplete, you are forgetting what happens inside the fluid. It is easier to see using solid blocks as in the figure below:

Here you can see that the effect of moving  block 1 down is to shift block 2 to the right, and moving block 3 back up the same amount that block 1 went down, then in addition to that , you move block 4 up. You can compute the change in energy block by block (easy for blocks but difficult for fluids), or by the net change in configuration: imagine that it was block one that moved on top of block 4 and the rest did not move. Both ways of computing it will result in the same positive change in potential energy.  
To illustrate the idea, the work done by the gas will be equal to the energy transferred to the environment, if there are no heat losses, this can be calculated as the change in potential energy of the mercury plus the work done on the atmosphere:  
$W=P_{atm}\Delta V-m_Ag\Delta h_A/2+m_Bg(d+\Delta h_B/2)$
using $m=\rho_{Hg} \Delta V$ and $\Delta h_i=\Delta V /S_i$ we get:
$W=P_{atm}\Delta V+ \frac{1}{2}\rho_{Hg} g(\Delta V)^2 (\frac{1}{S_B}-\frac{1}{S_A})+\rho_{Hg} g d\Delta V $
A: Your approach is almost correct!
To see what's wrong, consider the case $d = 0$. Then for a small change in volume, the change in potential energy of the water is zero (since you're just moving water from the left side to water at the same height on the right side). But the gas has definitely done $p \, dV$ work.
The mistake is that you've neglected the work done on the atmosphere, i.e. on the gas at the open part of the right end of the tube. When $d = 0$, the atmospheric pressure is equal to the gas pressure $p$, so the work done on the air is $p\, dV$. Energy simply goes from one gas to another.
In general, your approach will work, but you have to take care to include all contributions. The atmosphere is easy to forget about!
