Yes, if the iqaamah of the obligatory prayer has been given, then stop the optional prayer that you were performing so that you catch the takbeerat ul ihraam with the imaam – according to what is established from the statement of the Prophet sal Allaahu alaiyhi wa sallam:
If the iqaamah for the prayer is given then there is no prayer except for the obligatory one.
(Ahmad 2/33, Muslim 1/493, and others)
And with Allaah is the tawfeeq. And may peace and blessings be upon our Prophet Muhammad and his family and his Companions.