Categorical traces and a relative Lefschetz–Verdier formula