Robert F. Kennedy Jr. was expected to appear in court Tuesday to defend his effort to get on the ballot for president in the premier battleground state of Pennsylvania, where Democrats are angling to force him off in what is expected to be a closely contested race.