Can one demand to see code used to generate an article? An article is published and the results depend heavily on a piece of code that the author wrote themselves.

The author has not shared the code with the community, even though they have been using it possibly for years now.

If I request the source code from the author and they refuse to share it, can I "demand" to see it (for example, making a formal request to one of the journals that published one of the articles)?
I believe you are asking the wrong question. 

You are asking: Can one demand to see code used to generate an article?

To which I would answer: Assuming you're in a country with free speech, you can "demand" the code. Just make sure you do not threaten the author or otherwise break the law with your "demand". However, you are unlikely to be successful in forcing the author to share the code with you. A "demand" for code to me would come off as arrogant and I would ignore or decline a "demand". 

I think a better question is:  How can I get the code from an article I am interested in looking at or using?

To which I would answer: It depends upon your subfield and why the author will not share her or his code. Some subfields now require/expect authors to share code and data. For example, the American Statistical Association recommends sharing code to create reproducible results. Here are the steps I would follow to get the code from an article I wanted: 


I would first try reaching out to the author and asking nicely if she or he would be willing to share their code with you.
If the author declined, I would ask the journal editor if they could get the code to support the conclusions article because you wish to reproduce the article's results. Also, some journals now have open code/open data requirements. Check the policy of the journals the author published in. 
If you end up in hostile situation with the author, and you think they are being misleading based upon their findings, I would contact the research integrity and ethics office at the author's university (e.g., Illinois or Rochester). 
If you truly suspect scientific misconduct as the reason behind not sharing code, you could take this farther by writing a response article questioning their findings and state that they refused to share code with you. For example, watch how this author investigated a cancer scandal. The end result was prison time for the fraudulent scientist. 
If the author's funding agency requires open access to code, you could contact them and request they force the author to share their code. For example, all data and code development funded by the US Government is required to be open.


I would suggest working with the intellectual property office or similar office at your institute if step 1 fails. They can guide you on what options, if any, you have. 

Last, you might need to accept that you cannot force someone to share his or her code with you.