Robin Peterson and Dale Steyn led a South African fightback which left the second Test against Pakistan evenly poised at the end of the third day at Newlands on Saturday.
Pakistan were 100 for three in their second innings at the close, a lead of 112.
Peterson, drafted in as a specialist spinner, shone with the bat to hit 84 and enable South Africa to get within 12 runs of Pakistan's first innings total of 338 and Steyn bowled at blistering pace to take two wickets as Pakistan lost their first three second innings wickets for 45.
At that stage South Africa were slightly on top but Azhar Ali and captain Misbah-ul-Haq shared a largely cautious unbeaten partnership of 55 to see Pakistan through to the close without further loss.
South Africa were hampered by a recurrence of a hamstring injury to Morne Morkel, who left the field after bowling one ball in his second spell.
He had to stop bowling because of the same injury in the first innings as well and seems unlikely to take the ball again in the match.
Peterson led a fighting performance by South Africa's lower order batsmen as they recovered from an overnight 139 for five to reach 326 all out.
Pakistan's lead of 12 runs was soon shown to be negligible as Steyn and Vernon Philander both took wickets in their first overs.
Overnight batsmen AB de Villiers and Dean Elgar started South Africa's recovery when they took their sixth wicket stand to 55 before Elgar was caught at slip off Saeed Ajmal.
At that stage Ajmal had taken all six wickets but he had no further success, finishing with 6/96.
The left-handed Peterson batted positively from the start, shrugging off a poor run of batting form which had yielded just 15 runs in his previous seven international knocks, including five noughts. He went to his second Test fifty off 73 balls and went on to pass his previous best of 61 against Bangladesh 10 years ago.
Pakistan 338 and 100/3 (Ali 45*, Misbah 36*, Steyn 2/24) vs SA 326 (R Peterson 84, AB de Villiers 61, A Jamal 6/96, M Irfan 3/86)