Arithmetic of Hamiltonian in canonical transformation I have the following Hamiltonian:
$$ \mathcal{H} = \frac{p^2}{2m} + V(q-X(t)) + \dot{X}(t)p, $$
and I make the usual canonical transformation for the momentum:
$$ p \rightarrow p' = p + m\dot{X},$$ 
and complete the square, which should give the following:
$$ \mathcal{H}' = \frac{p'^2}{2m} + V(q - X(t)) - \color{red}{m\ddot{X}(t)q} - \frac{m\dot{X^2}}{2}.$$ 
I can get most of this expression apart from the one in $\color{red}{red}$.
This has to come from the cross term $(\frac{(\hat{p} \cdot m\dot{X}+m\dot{X}\cdot\hat{p})}{2m}),$ but I can't get the $q$ to come out. 
Any pointers?
 A: The Hamiltonian transforms according to the following rule:
$H^\prime = H - \partial_t f$, where $f=f(q,t)$ (1).
We can find this function by using that:
$p = p^\prime-\partial_qf=p^\prime-m \dot{X}$.
So we see that $f=m \dot{X}q +c , c \in \mathcal{R}.$   (2).
Plug equation (2) into equation (1) gives:
$H^\prime=H-\partial_t(m\dot{X}q+c)=H-m\partial_t(q\dot{X})$, now looking at the final form of the equation that you have to show I guess that $\dot{q}=0$ such that $H^\prime=H-mq\ddot{X}$. Where $H=(p^\prime)^2/2m+V(q-X(t))-m\dot{X}^2/2$ is the Hamiltonian you already derived. 
Warning: I am not sure what exactly $q$ is in your case and whether $\dot{q}=0$ holds. This is something you should check yourself since I do not know the context and so on. But this works completely fine.
A: Hint: Try a type-2 generating function $$F_2(q,P,t)~=~\left(P-m\dot{X}(t)\right)q,$$
for a CT $$(q,p,t)~\to~ (Q,P,t),$$ such that 
$$K-H ~=~\frac{\partial F_2}{\partial t}~=~\color{red}{-m\ddot{X}(t)q}, \qquad Q~=~\frac{\partial F_2}{\partial P}~=~q,  \qquad p~=~\frac{\partial F_2}{\partial q}~=~P-m\dot{X}(t). $$
