He was seen by a staff member at Barnsley Central Library and was immediately asked to leave.
Michael Barrett, 40, pleaded guilty at Barnsley Magistrates Court to outraging public decency.
But sentencing could not take place on Tuesday as probation reports were needed.
Prosecuting, Tayo Dasadu said the libertarian noticed Barrett’s hand was jerking as he sat at the computer.
“She realised he was masturbating and was disgusted,” she said. “She saw he was watching on screen a woman wearing a very small bra. His right arm was going up and down under his coat.
“The librarian asked him to stop and to leave, which he did.
Eric Bray, defending, said Barrett, of Wyborn Grove, Rotherham, had gone through problems with drink and drugs.
“But it’s fair to say that due to a probation programme he has made
great strides in dealing with these issues,” he said.
“So much so, he says he has not taken any drugs or substance medication for some time. He might have the occasional drink - very different to the way things used to be.”
Mr Bray said accommodation had also been difficult but Barrett was now in a rented flat in Rotherham. “He is happy and seems to be thriving,” he said.
“He has moved on since the library incident.”
Mr Bray said the librarian accepted that during the incident in October 2018, Barrett had not actually exposed himself.
Barrett, formerly of Cope Street, Barnsley, was remanded on bail until March 19.