How to remove a costant group delay?
If the low pass filter has a linear phase response, then the group delay it introduces is indeed constant, and it equals $\tau_g=(N-1)/2$ samples, with $N$ being the number of taps. If $N$ is odd, then the group delay is an integer number of samples, and in this case you just have to shift the output signal $\tau_g$ samples to the left as compared to the original signal:

$$y[n]=x[n]-x_f[n+\tau],\quad \tau\in\mathbb{N}$$

This is exactly what the Matlab code in your question does. Don't forget to make sure that the number of filter taps $N$ is odd!

Alternatively, you could delay the original signal $x[n]$ by the filter's group delay before subtracting from it the filtered signal.