Gold shakes off weakness to eke out a gain as dollar falls