I am planning on writing a science text under the CC BY-SA 4.0 license. The text will contain code blocks for commonly preformed analysis from the relevant field of study. Additionally, it will use common packages from R and Python. I only want to ensure that text remains open and free to use to ensure that it doesn't end up exclusively behind a pay wall like all other texts covering the topic of interest. The code in the code blocks in the text are generally adapted from established code or there are only few ways to wite it because it's a specific analysis.

My questions are as follows;

  1. Given that the text and LaTex code used to build the document will be licensed under CC BY-SA 4.0 license, if someone use the publish code blocks in their analysises do they also have apply CC BY-SA 4.0 license for any works that use the analysises and code?

  2. Should I license only the text, the LaTex code, and images under CC BY-SA 4.0 license and specify the code blocks are licensed under a different license?

  3. If the answer to (2) is "yes", what are good license options to allow individuals to use the code blocks without any restrictions or requirements? Also what the most appropriate way to specify the license in this case or are there examples? Alternatively, can I specify that code examples in the text are excluded from the license?

  • Could you read How could I copyleft a document that contains a substantial amount of both code and prose and let us know whether it answers your question, and if not, what remains unanswered? Also, when you say code blocks in the text are generally adapted from established code, what licence(s) was that established code published under?
    – MadHatter
    Mar 17, 2022 at 6:19
  • It doesn't directly address the same issue. The code I'm writing would be hard to call my own at times and I think some would fall under fair use, but not all. More or less if a research uses "recommendations" or code I show in the book to preform analysis for a paper they plan to put through the peer review process, I don't want them to deal with issues related to license. The other question seems to deal more with adaptation or copying of chunks, and not separate products produced based on recommendations or usage of book. I thought about putting an exception for license on code blocks. Mar 17, 2022 at 11:44

1 Answer 1


We have already dealt with the question of how best to licence a free document containing a mixture of prose and code, but I think you have a bigger problem. When you write that the "code in the code blocks in the text are generally adapted from established code", and "The code I'm writing would be hard to call my own at times and I think some would fall under fair use, but not all", you seem to be suggesting that quite a lot of the code in your book is adapted from someone else's code.

That makes your code a derivative of their work(s), which is something you generally need permission to make. In some cases the original code may be permissively licensed, in which case you probably only have attribution and possible text-reproduction obligations. In some cases it may be under a copyleft licence, in which case you may have no choice about how you license your adaptation. In some cases it may not be licensed to you at all, in which case you have no rights to use it at all in this way, or nearly any other.

You mention fair use as a defence, but many jurisdictions have distinctly limited fair use rights, or indeed none at all. I would strongly advise against relying on that defence for a work which is distributed over the internet, at least not without taking professional legal advice.

In short, you may well not have any choice about which licence to distribute your chunks of code under, and you may have no permission to distribute them at all.

  • The code I'm talking about would be like a regression or a standard growth model in R. There is only a handful of ways to right the code to do the analysises and the analysises are commonly used and the code is generally not licensed. Now code for simulation models, that would be a different case, i.e. original work. Typically I would just cite someone's paper than used the analysis unless super common practice for any peer-reviewed publications that I right. Mar 17, 2022 at 13:46
  • Not so much software as it is a script to perform a commonly used analysis of data. Mar 17, 2022 at 13:50
  • That's software. And I take your point about the only way to code it defence, but note that it's a defence, ie, you have to make the argument in the hope that a court sides with you, not the person who's suing you for breach of copyright. It's a fair old risk to take.
    – MadHatter
    Mar 17, 2022 at 15:15
  • Some code/ code from packages are under Gpl2 or greater. I will have to use a different licensing. However, you were a huge help in getting into and understanding open source publishing. Mar 17, 2022 at 19:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.