EVERY January, Dilys Rose acknowledges, she cannot help but think of that poor young man, Thomas Aikenhead, making a long and lonely walk to his death, trudging under armed escort through the snow and the slush of the city streets. He was, she adds, probably barefoot, too.

Aikenhead, a student at Edinburgh University, was the last person to be hanged in Britain for blasphemy, having been betrayed by friends in a city riven with overheated religious fervour and suspicion. Religious scepticism of the kind so freely expressed by Aikenhead was never likely to be tolerated by a resurgent Kirk. After a trial, he was sentenced to death, and was hanged at the Gallowlee, down Leith Wynd, on January 8, 1697. He was 20 years old.

In the 1850s, Thomas Babington Macaulay described Edinburgh’s ministers as Aikenhead’s “murderers”. In the three centuries since his death Aikenhead has become a cause celebre. In 2016 a play written by poet Simon Armitage presented the apothecary’s son as a free-speech martyr. Its title, I Am Thomas, called to mind the expression “Je suis Charlie”, which was coined in the aftermath of the Charlie Hebdo massacre in Paris in 2015.

Aikenhead’s story is summarised thus in the Collins Encyclopedia of Scotland (1994): “Scientific method became a major element in university teaching. Intellectually-minded students abandoned a moribund theology for science, and whereas poor Aikenhead had died in a rash endeavour to use the new science to reform theology, his shocking death did inspire a new, moderate and liberal thinking which took wider effect in the 19th century.” A modern history on the Scottish Enlightenment says his execution “haunted” the century that followed.

Rose, 63, is an award-winning novelist, poet and short-story writer, whose titles include the collection War Dolls and the novels The Pest Maiden and Pelmanism. Her partner is the writer Brian McCabe, and she directs the online Masters programme in creative writing at Edinburgh University. In Unspeakable she has devised a grittily persuasive picture of the Edinburgh that Aikenhead would have known. The squalor and the overcrowding, the maimed beggars, the smells and the filth are particularly well conveyed, as are the more civilised haunts frequented by Aikenhead and his friends in their time as students at the College of Edinburgh.

“I’d known about his existence through a rather odd circumstance,” says Rose. “A relative of ours had been investigating into Burke’s Peerage in the hope of finding that we were related to somebody important, and the only person that was thrown up was Thomas Aikenhead himself.

“I didn’t even know his name at that point. I just knew he was somebody who had been hanged for blasphemy. Because I had been an atheist since a very early age, it interested me that he was an atheist, or was classified as one. So he stayed in my head for a while.

“I worked on this novel for close on 10 years,” she continues. “I started to think about it in 2007, 2008, and at that time I was very aware of the way in which the death sentence was being applied for blasphemy – not so much in Britain as in other countries. I felt that the story had contemporary relevance.

“I was also very concerned by the fact that what he lost his life for seemed to be what students of my generation did all the time, which was to shoot their mouths off, and sometimes adopt stances which they didn’t necessarily believe in. For me this is partly for the sake of testing their powers of argument.

“I was also horrified by the fact that it was his fellow students who acted as the principal witnesses [against Aikenhead] and in fact betrayed him – Mungo Craig, in particular.”

The book depicts a chilling moment when Craig, a student friend of Aikenhead’s, coaxes him, in a crowded tavern, to repeat an expression he had used earlier that day: “If ye ask me,” Aikenhead had said, “the doctrine o theology is a rhapsody o feigned and ill-invented nonsense …” That night, in the tavern, his tongue loosed by ale, he repeats the phrase, and his doom is sealed.

Craig, incidentally, would write A Satyr Against Atheistical Deism, a copy of which today lies in Harvard University Library. “Whereas Craig published this pamphlet, denouncing Thomas when he was in jail, Thomas didn’t publish anything,” says Rose. “And, as far as I can gather, he didn’t even try to convert anybody. He didn’t stand on an orange-box anywhere. He was gobby, and he was bright. He had a questing mind, and in a sense that was his downfall.

“The only things I could think of which might have caused Craig to do as he did were intellectual jealousy, the uncertainty of the times and the possibility of self-advancement. People were given money for shopping others.

“This idea of spying on students was current after 7/7 [the London bombings] when a request went round universities to spy on Muslim students for any political activity, which I thought was a shocking way to go about things, because political extremism and expressing your opinions are two quite different things.”

Rose says the betrayal scene in the tavern was an invention on her part. “There is no information about that kind of thing, and that was one of the challenges for me. I stuck to the facts I knew, but you have to construct a story around that: how did it happen? Where did it happen?

“I hadn’t written in period before, except for an opera libretto about Kaspar Hauser, which I did some research for. But there’s quite a bit in the way of basic facts [about Aikenhead], but facts don't give you character. His case is seen as a kind of watershed between the bad old days and the Enlightenment, and understandably so.”

The book’s salty Scots dialogue was, it emerges, originally written in standard English but it dawned on Rose that it didn’t feel right. “There’s a tang to the speech in the book that I feel probably comes to close to Hogg’s Confessions Of A Justified Sinner … We don’t know what people sounded like then. You can tell how they wrote, however. I did check a lot of vocabulary and made frequent use of the online Dictionary Of The Scots Language, to make sure I wasn’t using anachronisms.”

The case has for good reason resonated down the generations, she believes. “Sadly, it’s perhaps even more resonant today. Our society and many others are becoming very polarised and religious intolerance seems to be on the increase. I’m very concerned about that and about the desire by people to restrict freedom of speech.

“I’m not saying that Aikenhead wasn’t offensive, or that what he said wouldn’t have offended some people. But I wonder whether, if an institution like a religion is secure in itself, is it not able to ignore jokes, to say, ‘This doesn’t touch us, it doesn’t matter’? But blasphemy is still seen as a crime and I was quite surprised to discover that Ireland introduced a blasphemy law in 2010.”

We are, as it happens, sitting not far from where poor Aikenhead would have walked down to the Gallowlee from the Old Tolbooth in the High Street. To get to the site today you go down Leith Walk and take a left at Pilrig St Paul’s church. “Every January”, says Rose, “I think about that, about somebody having to walk down there, dressed in sackcloth, in the snow. He would have been part of a slow procession. It would probably have taken Thomas 40 minutes to get to the place where he was hanged. The thought of it all just makes me wince.”

Unspeakable is published by Freight Books, £9.99