<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/writing-a-r-book-and-self-publishing-it-in-amazon/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://msperlin.github.io/2017-02-16-Writing-a-book/"> R and Finance</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> My experience and some advices about the process – </p>
<p>Many people, including my university colleagues and friends, have asked<br>
me about the process of writing a book and self publishing it in Amazon.<br>
You can find the details about the book<br>
<a target="_blank" rel="nofollow" href="https://www.amazon.com/Processamento-Modelagem-Dados-Financeiros-Portuguese-ebook/dp/B01N4VXYPM/ref=sr_1_1?ie=UTF8&qid=1487077106&sr=8-1&keywords=processamento">here</a><br>
and<br>
<a target="_blank" rel="nofollow" href="https://sites.google.com/view/r-financas/p%C3%A1gina-inicial">here</a>.<br>
Given so much interest, I’m going to report the whole process in this<br>
post.</p>
<p>First, motivation. <strong>Why did I write a book?</strong></p>
<p>I am a university professor. Writing is a major part of my work and I<br>
really enjoy it. Think about it, it is a magical process. I press a<br>
specific and long combination of strokes in my keyboard and that<br>
translates into information distributed all over the world. This<br>
information helps people in a particular problem, just as I have been<br>
helped in the past by reading the work of other people.</p>
<p>A longer answer, I wrote the book because I simply felt like studying<br>
and learning more about R. Writing a book is an excellent opportunity<br>
for doing so as it forces you to think about a topic and communicate it<br>
clearly and objectively. After reading the <a target="_blank" rel="nofollow" href="https://www.amazon.com/Reproducible-Research-Studio-Second-Chapman/dp/1498715370">book from<br>
Grangrud</a>,<br>
I got some inspiration for writing my own book about R, reproducible<br>
research and Finance. My experience in the academia tells me that people<br>
often learn programming by themselves, without any consideration for the<br>
structure and re-usability of the code. You can see examples of it when<br>
you download code from other researchers. In most cases, it is a big<br>
mess and I often found it easier (and less risky) to rewrite it all from<br>
scratch. So, writing a book is a way to show to people how to use R for<br>
data analysis in finance and how to organize code that can be used<br>
later, saving lots of time of development. As I mentioned in a <a target="_blank" rel="nofollow" href="https://msperlin.github.io/2017-02-06-My-Book-is-out/">previous<br>
post</a>, another<br>
argument for writing the book is that, even if I didn’t publish it, I<br>
would still have very good class notes for my graduate students or<br>
perhaps a pdf to host in my website.</p>
<p>Off course you may wonder about the financial side. After all, I am a<br>
finance professor and finance is the science of money. While you can<br>
make money selling books, the financial incentives for writing a new<br>
technical book are quite low. In financial terms, writing a technical<br>
book is a project with negative expected Net Present Value (NPV). This<br>
means that, from the <em>ex ante</em> point of view of a potential writer,<br>
working in a technical book simply does not create financial value.<br>
There is a high production cost in terms of how much time you spend<br>
writing and formatting the book and a high uncertainty about the future<br>
royalties. If you look at it from the financial side, it is just a bad<br>
gamble. This is why you should only write a book about something you<br>
love and want to learn more about. Otherwise, it can become a big<br>
frustration.</p>
<h2>The writing process</h2>
<p>The first step in writing my book was to set out a summary, that is, the<br>
names of the chapters. The first part of it was pretty obvious, it<br>
covered the basics of using R: packages, objects, functions, loops and<br>
so on. The rest of the topics were all based on my experience. I asked<br>
the question <strong>What are the most important data tasks that a finance<br>
student should learn?</strong>. Once you have a TOC (table of contents) ready,<br>
it is all about filling up the book with content.</p>
<p>I really enjoyed the process of writing the book. Taking a long term<br>
project such as a book requires discipline. One strategy that really<br>
helped me was to work very early in the morning. As soon as the sun<br>
rised at 0630, I’m up and in my way to the university. From 07:00 am to<br>
0900 am, nothing really happens at the university, giving me plenty of<br>
time to write peacefully. Some days I could write all morning, if I felt<br>
like it and had a free schedule. One tip for anyone that is thinking<br>
about writing a book (or article), only write when you fell like<br>
writing. It is really difficult to write well if your mind is not into<br>
it. Not having a deadline for this project meant that I could take my<br>
time in developing it.</p>
<p>As for software, the first version of the book was written in<br>
<a target="_blank" rel="nofollow" href="http://www.texstudio.org/">TexStudio</a> with the content in <code>Sweave</code><br>
files (.Rnw), and a <a target="_blank" rel="nofollow" href="https://www.r-bloggers.com/use-sweave-with-texmaker-and-make-synctex-work-properly-with-it/">simple<br>
hack</a><br>
to call Sweave from R and compile the resulting tex file. I’m used to<br>
work with latex, so the choice for textudio was obvious. I like this<br>
latex editor as it integrates nicely with grammar checking tools and<br>
latex compilation. Why not <code>knitr</code>? Well, at the time I was very<br>
comfortable with <code>Sweave</code>. It offered everything I needed. I didn’t see<br>
a reason for change. Clearly, I was wrong. Once I started to investigate<br>
how to format better the code in the book, it became clear that I should<br>
be using <code>knitr</code>, which has all these extra options that are not<br>
available in <code>Sweave</code>. I switched it as soon as I realized how much I<br>
would gain in terms of control over the output.</p>
<p>After 6 months of work, I had a first readable version of the book. This<br>
is when I started to investigate how to turn a latex file into an ebook<br>
file. To my surprise, this is not easy! It could be the case that I had<br>
lots of code and figures from the compilation. I tried all the existing<br>
software and, to my frustration, it just didn’t work. When I didn’t got<br>
an error message that stopped the conversion, the resulting <em>epub</em> file<br>
look awful in the kindle reader. The warning and error messages from the<br>
latex2ebook compiler also didn’t help a lot.</p>
<p>By looking for solutions in the internet and with a bit of luck, I’ve<br>
found that Yihui Xie had just published his <a target="_blank" rel="nofollow" href="https://bookdown.org/">bookdown<br>
package</a>. I tried out the first chapters and it<br>
worked perfectly! The great thing about <code>bookdown</code> is that you can<br>
output to pdf, html or epub with the same files. I was very happy that,<br>
finally, I would be able to get all formats that I needed in a single<br>
platform. I’m not going to go into the details about how to use<br>
<code>bookdown</code>. You can find the official tutorial<br>
<a target="_blank" rel="nofollow" href="https://bookdown.org/yihui/bookdown/">here</a>. It is worth to point out<br>
that the written content is just RMarkdown so, if you know latex, you’ll<br>
fell right at home. At this point, I switched all content from latex to Rmarkdown.</p>
<p>The only feature in <code>bookdown</code> that didn’t work out of the box was the<br>
equation in the kindle format. The formulas just didn’t print well. The<br>
weird part is that they looked fine in any other ebook reader, but not<br>
in Kindle. Apparently, there is no solution for this problem besides<br>
using figure files for the equations. What I did to solve it was to use<br>
R switches within the book so that the epub compilation used a figure<br>
file created in <a target="_blank" rel="nofollow" href="https://www.codecogs.com/latex/eqneditor.php">codecogs</a><br>
and the latex/html compilation used the normal code for equations. See<br>
here an example of code for the first equation in chapter 9.</p>
<p><img alt="An example of how to get equations in Epub and
pdf" src="https://i1.wp.com/msperlin.github.io/img/screenshot-ebook-figures.png?w=456" data-recalc-dims="1"></p>
<p>As you can see, I used a switch in the <code>knitr</code> command as in<br>
<code>include=identical(knitr:::pandoc_to(), 'epub3')</code> to run each code<br>
conditional to the type of output. As for inline equations, I just used<br>
straight text to indicate coefficients such as <em>beta</em>, <em>alpha</em>. I know I<br>
could have used the same strategy, but I didn’t fell the need since<br>
there were so few inline equations in the text.</p>
<p>After switching to bookdown, I added a couple more chapters in the<br>
following months. A comment here is that I tried writing in RStudio but<br>
it didn’t worked out for me. Perhaps it was due to the large files I was<br>
handling, but it felt really clunky, slow and not reactive. Also, the<br>
grammar check system in Rstudio needs a lot of work. I tried going back<br>
to Texstudio but it didn’t had support for markdown highlighting. I then<br>
switched software by doing the writing in<br>
<a target="_blank" rel="nofollow" href="https://notepad-plus-plus.org/">notepad++</a> + <a target="_blank" rel="nofollow" href="https://github.com/Edditoria/markdown_npp">markdown<br>
extension</a> and only used<br>
RStudio to compile the book. If you haven’t tried notepad++, give it a<br>
go. I really like it and I find myself using it more and more.</p>
<h2>Publishing in Amazon</h2>
<p>Around october of 2016, the content of the book started to take a good<br>
shape and it was time to investigate where and how to publish it. I got<br>
in touch with a major local publisher here in Brazil and, after one<br>
month, they informed me that they were not interested in the book. While<br>
I could try other publishers, I really didn’t felt like going through<br>
another month of evaluation. I studied for while and decided to<br>
<strong>self-publish the book</strong>.</p>
<p>One positive aspect of self publishing is that big publishers downsized<br>
over the years and good professionals are now in the market for hire.<br>
This means that a large range of good editors and designers are<br>
available to the independent author. In my case, I fought the urge to do<br>
the cover myself and searched for a cover designer. After a lot of<br>
search, I hired <a target="_blank" rel="nofollow" href="http://capista.com.br/capas-de-livros/">Capista</a> to do<br>
the book cover. I really liked his portfolio and, he offers a discount<br>
for self-publish authors, which is very nice of him.</p>
<p>I also hired professionals to check the text for mistakes and grammar.<br>
This is particular important as grammar mistakes are normal and expected<br>
in large documents. But, selecting a reviewer was an interactive<br>
process. I searched the web and hired tree different reviewers and<br>
offered them three different chapters of the book. Once I had all<br>
revisions, I selected the reviewer the offered the best result and sent<br>
him the rest of the chapters. You can find his site<br>
<a target="_blank" rel="nofollow" href="http://revisaotextual.com.br/">here</a>. I’m glad I did this way, It would<br>
be a nightmare if I hired the wrong person to do this job.</p>
<p>The main advantage of self publication is that the royalties for the<br>
author are higher and the cost to publish are lower. This setup<br>
translates into a lower price of the book and more sells. Specific to<br>
Brazil, where the exchange rate of dollar to Real (the Brazilian<br>
currency) is very unfavourable, it makes sense to offer a lower priced<br>
book. This way, it is easier for students and instructor to acquire and<br>
use the book.</p>
<p>Since ebooks is a growing market, you can find lots of tutorials about<br>
this topic in the internet, <a target="_blank" rel="nofollow" href="https://www.cnet.com/how-to/how-to-self-publish-an-ebook/">like this<br>
one</a> and<br>
<a target="_blank" rel="nofollow" href="http://www.thecreativepenn.com/how-to-self-publish-an-ebook/">this<br>
one</a>. The<br>
problem here is that they are mostly about self-publishing <em>text</em><br>
ebooks, as in fiction stories. The only article I’ve found about<br>
self-publishing technical books is <a target="_blank" rel="nofollow" href="http://cacm.acm.org/magazines/2015/2/182651-do-it-yourself-textbook-publishing/abstract#">this<br>
one</a>,<br>
where the author describe their rather good experience in publishing a<br>
software-engineering textbook.</p>
<p>I checked <a target="_blank" rel="nofollow" href="https://www.lulu.com/">Lulu</a>,<br>
<a target="_blank" rel="nofollow" href="https://www.smashwords.com/">Smashwords</a> and <a target="_blank" rel="nofollow" href="https://kdp.amazon.com/">Amazon<br>
KDP</a>. What made my choice towards Amazon was<br>
that it was a established platform, where almost everyone is registered.<br>
Buying an Ebook in Amazon is just one click away. Also, I’ve found that<br>
the KDP (self publish) program of Amazon is quite good. If the ebook<br>
sells for less than 9 dollars and is enrolled in the <a target="_blank" rel="nofollow" href="https://www.amazon.com/Browse-Kindle-Unlimited-Books/b?ie=UTF8&node=9069934011">Amazon unlimited<br>
program</a>,<br>
the author gets 70% of the sticker price in royalties, which is a nice<br>
percentage! A bit of information, the normal royalty rate for an author<br>
of a publishing company is around 5%-10% of the book price. Just a quick<br>
comparison, for each ebook sold for<br>
9 dollar,the author gets 6.3 (70%*9). In other<br>
scenario, assuming an author has a publisher that offered 10% royalty,<br>
you would get the same royalty for a a book priced at 63$ (6.3/10%). The<br>
big difference here is that a 9$ ebook sells a lot more than a 63$<br>
ebook, resulting in higher royalties and higher impact. More people read<br>
your book and you make more money. My only disappointment with Amazon is<br>
that the printed books are shipped from the USA. A buyer of the printed<br>
book in Brazil will need to wait at least 10-15 days to get the book.<br>
But, overall, I find this to be a small cost to bear.</p>
<p>When the final version of the content of the book was ready, I started<br>
to organize everything I needed in order to publish the book. This is<br>
the list:</p>
<ol>
<li>
<p>A ISBN number – A unique id that indexes your book, one for the<br>
ebook and another for the print version. In order to get an ISBN,<br>
you need to find the <a target="_blank" rel="nofollow" href="https://www.isbn-international.org/">representation of ISBN in your<br>
country</a>. Amazon also offers<br>
their own ISBN, but I don’t advise to use it. Having your own ISBN<br>
gives you more control. Just get one yourself. It is easy and cheap.<br>
In my case, I sent the request from the<br>
<a target="_blank" rel="nofollow" href="http://www.isbn.bn.br/website/">website</a> by filling up forms in<br>
less than 10 minutes and got the number in five days.</p>
</li>
<li>
<p>Cover – The cover is processed as a figure file (.jpg). An important<br>
information here is that you need to define the size of you book and<br>
the number of pages at this stage. In my case, I used a 7×11 inches<br>
configuration for 200+ pages. It looks quite nice. Have a look:</p>
</li>
</ol>
<p><img alt="My book and my spanish iguana!" src="https://i0.wp.com/msperlin.github.io/img/MyBook_printed.jpg?w=456" data-recalc-dims="1"></p>
<p>Beautiful cover! Isn’t it? Once you have the figure file, you just add<br>
it to the <em>book creator</em> system in amazon. At this stage, you will also<br>
need a summary about the book and its author and a picture for the back<br>
cover of the printed version.</p>
<ol>
<li>
<p>Registration in Amazon. The site asks you a lot of information. It<br>
took me at least half an hour to fill up the forms, which also<br>
includes tax details for us and non-us residents.</p>
</li>
<li>
<p>Description of the book. This is the text that goes in the webpage<br>
of the book in Amazon. In my case, I just used the same text as the<br>
back cover.</p>
</li>
<li>
<p><a target="_blank" rel="nofollow" href="https://sites.google.com/view/r-financas/p%C3%A1gina-inicial">A book<br>
site</a>.<br>
I wanted to distribute the code from the book and also exercises<br>
over the web. I used <a target="_blank" rel="nofollow" href="https://sites.google.com/">Google Sites</a>, but<br>
I’m sure you have plenty of options here. On a side note, I’m also<br>
considering writing a CRAN package for distributing this content.</p>
</li>
</ol>
<p>For the ebook, the next step is easy. Just upload the cover, the <code>.epub</code><br>
file from <code>bookdown</code>, write the description and other details and you<br>
are good to go. With everything ready to go, you can check the ebook<br>
page by page with the online viewer. I didn’t have any problems at this<br>
stage. As for the print version, it is far more complicated. In my case,<br>
I used the pdf file to build the printed version. Once you upload the<br>
pdf, you can check the result in a online viewer. This is the final<br>
proof of the book and shows how it would look like when printed. There<br>
are lots of error checking from the system and, if you got an error, you<br>
cannot publish the book. I’ve spent a lot of time formatting everything<br>
so that the book comes out perfectly. The problem is that, each attempt<br>
to solve errors demanded processing time. At least 10 minutes to reach<br>
the whole cycle of compiling the pdf, uploading and checking the result.<br>
So, every time I tried to solve an error, I got the result 10 minutes<br>
later. I repeated this process over and over until I had a perfect book.</p>
<h2>The publication</h2>
<p>The book was launched in the sixth of February 2017. I advertised it in<br>
here, my <a target="_blank" rel="nofollow" href="https://msperlin.github.io/">blog</a>, Facebook and email. I was<br>
very surprised with the strong reception. In the first and second day,<br>
it became the <em>best seller</em> book in the section of finance in the<br>
Brazilian Amazon. I am really happy for the success of the book. Right<br>
now, the sales have gone down, which is normal and expected, but I’m<br>
getting all sorts of good responses. What surprised me is that, even<br>
though the book was published primarily for Brazil, I’m receiving lots<br>
of replies (and sales) of people from other Portuguese speaking<br>
countries such as Cabo Verde and Portugal!</p>
<h2>Some advices</h2>
<p>If I could go back in time and advice myself about the book, this is<br>
what I would have said. I hope these advices are helpful for others as<br>
well.</p>
<ul>
<li>
<p>From the first version, make up your mind about book size and<br>
letter size. This decision will impact everything else. Problems are<br>
easier to detect and solve if you stick to a single format.</p>
</li>
<li>
<p>RStudio and <code>bookdown</code> are your friends. I’m not aware of any other<br>
free platform that offers anything like it for developing a<br>
technical book.</p>
</li>
<li>
<p>When possible, use <code>tidy=TRUE</code> in the chunks of code. This simple<br>
command forces the code within the boundaries of the pdf. Sometimes<br>
it doesn’t look good so you always need to mannualy check the chunks<br>
with lots of code.</p>
</li>
<li>
<p>Avoid using function <code>str</code> in the content. For some reason, it does<br>
not respect the boundaries of the page. There is a solution but it<br>
would not look good in the book.</p>
</li>
<li>
<p>Use a dark theme for writing. Your eyes will hurt less and you will<br>
be able to write more.</p>
</li>
<li>
<p>Talk to people that understands the material you are writing about.<br>
Listen to their advices and make changes when necessary. Don’t over<br>
commit to sections that you are not sure will be included.</p>
</li>
<li>
<p>For all figure files in your book, always get the highest possible<br>
resolution (at least 300 dpi). Amazon will not let you publish<br>
figures with low resolution.</p>
</li>
<li>
<p>Don’t even try to do yourself the book cover or grammar check. Hire<br>
someone to do it for you. Your future self will thank you later!</p>
</li>
</ul>
<h2>Whats next?</h2>
<p>I really enjoyed the experience of self-publishing a book and I strongly<br>
advise for anyone to give it a try. A cautionary note, <strong>I never worked<br>
with a established publisher and, therefore, I cannot talk about how it<br>
is or compare it to self-publish</strong>. If someone has had that experience,<br>
fell free to use the comments to discuss it.</p>
<p>The impact of a technical book is uncertain. Only time will tell if the<br>
book is successful or not. I have high hopes though. The feedback from<br>
the community has been strongly positive. I also have been working on<br>
extra material such as exercises and slides that are going to be<br>
distributed for free. This should motivate professors to use the book in<br>
class and increase distribution.</p>
<p>I hope this post was helpful and enlightening for potential authors.<br>
Comments or suggestions, please use the comment section or drop me an<br>
email at <a target="_blank" rel="nofollow" href="http://msperlin.github.io/2017-02-16-Writing-a-book/marceloperlin@gmail.com">marceloperlin@gmail.com</a>.</p>
<p>Best, Marcelo.</p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://msperlin.github.io/2017-02-16-Writing-a-book/"> R and Finance</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/0SUgnJ1QEPE"></a></div>