Virtual coupling, which refers to the operation of railway vehicles that enables the merge and separation of vehicles on the move by controlling the gap between the vehicles without any mechanical coupling, is one of the technologies for increasing the transport capacity and enhancing operational efficiency. This paper proposes a robust gap controller based on sliding mode control with a nonlinear train model with uncertainties. Additionally, a gap reference generation scheme is developed that ensures that the merge and separation of two trains is completed before a given location and respects constraints on acceleration and jerk. The position and velocity measurement errors arising from imperfect knowledge of wheel diameters are also considered, and a new error correction scheme is proposed to reduce the perturbation in the gap control performance. The proposed schemes are validated through simulations.