Brexit: How could Scotland stay in the EU?
First Minister Nicola Sturgeon has pledged to do all she can to ensure Scotland remains in the EU despite the UK as a whole voting for Brexit in last week's referendum.
Ms Sturgeon is to ask Holyrood to give the Scottish government a mandate to negotiate with the UK government, the EU institutions and individual member states to try and secure Scotland's relationship with the EU and its place in the single market.
She has also raised the possibility of a second independence referendum, which could see Scotland potentially split with the rest of the UK in a bid to continue its membership of the EU.
Here Dr Cormac Mac Amhlaigh, a lecturer in public law at Edinburgh University and an expert in EU constitutional law, looks at how events could in theory unfold.
The UK has voted for Brexit. What happens next?
The repercussions for Scotland are similar to most of the UK. As it currently stands the UK would exit the European Union - so all the free movement rights that Scottish residents currently enjoy would have to be renegotiated.
We still don't know what the detail of that renegotiation would be, we don't know what the terms of withdrawal would be yet, we do not know what subsequent relationship between the UK and EU will ensue.
So it's all a little up in the air at the moment.
What are the options for Scotland if it wants to stay in the EU?
It's a very tricky question. Formally speaking, it's the UK that enjoys EU membership and the EU would negotiate with the UK for withdrawal, both the terms and any subsequent relationship.
Scotland is not an independent state, so it does not formally have a direct relationship with the EU - that occurs through the UK.
So formally speaking, it seems Scotland would have to follow the procedures of becoming an independent state first, and then apply for accession to the EU, under the conditions that are stipulated in treaties at the moment.
Now that's the formal position, but if the EU was willing, it is likely they could suspend that or amend that, and start forming negotiations with Scotland if they so wished.
The rules surrounding this are often quite open and flexible, and I think there's nothing to stop the EU institutions negotiating with Scotland, making some sort of transitional arrangement and making some sort of arrangement so Scotland can inherit the UK's EU membership.
It would be complex, it would be tricky, it would be quite unorthodox, but I think it's certainly not impossible.
And really what we would be relying on here would be the political will - the political will of the remaining EU state governments, the EU institutions and of course the Scottish government and whether a remaining British government would be in agreement. So it is quite a complex question, but not impossible.
What would that situation look like?
I think this could only work as part of a transition to an independent Scotland.
It simply would not work that Scotland could retain these things (as part of the UK), for example free movement and trade, when England didn't. I think that's an arrangement that simply wouldn't happen.
I think any arrangements within the EU - relating specifically to Scotland - would be part of a broader context of an emergent independent Scotland, which would obviously require another referendum and all the complications with it.
I don't think Scotland could enjoy semi-EU membership while England didn't. It would be part of a broader transition of Brexit and an independent Scotland.
Are there any precedents? What about Greenland?
I've seen this touted, but I'm not sure how comparable it is. England and Scotland are more tightly bound than Greenland was to the Danish Crown.
Also it was a case that Greenland was leaving the EU with the consent of Denmark. This would be a case of Denmark leaving the EU and Greenland staying in.
I think the main thing is the same rules apply here, but it really is down to the political negotiations. It does depend on the political will of the different governments involved, and really if the will is there, anything can happen, anything can be arranged.