In order to reproduce your problem, I used the following code. This is taken from section 3 of your second image.
You can see the result of after publishing below.
Based on this, it looks like the problem is arising due to the clc,clf commands beings used. Since after these you normally would expect a clear window, it looks like that is comming as a result.
There are two ways to address this.
Either you can remove all the redundant clc,clf, clear or similar commands. You can refer the following.
The other thing you can do it to stop the evaluation before conversion to pdf. You can refer the following.
Hope this was helpful.