<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/how-to-self-publish-a-book-customizing-bookdown/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://blog.datascienceheroes.com/how-to-self-publish-a-book-customizing-bookdown/"> R - Data Science Heroes Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<div><img alt="How to self-publish a book: Customizing Bookdown" src="https://i1.wp.com/blog.datascienceheroes.com/content/images/2018/08/sn-x1313-bg-f8f8f8.jpg?w=456&ssl=1" data-recalc-dims="1">
<p>tl;dr: This post is related to <a rel="nofollow" href="https://blog.datascienceheroes.com/how-to-self-publish-a-book/" target="_blank">How to self-publish a book: A handy list of resources</a>. It’s centered around <strong>Bookdown</strong> and some non-standard customizations I found useful to create the <em>Data Science Live Book</em>.</p>
<ul>
<li>The first steps into Bookdown</li>
<li>Amazon Kindle format</li>
<li>Building the book</li>
<li>Be mindful of the line width</li>
<li>At the beginning of every ‘.Rmd’</li>
<li>Some parameters in index.Rmd</li>
<li>Images (position, caption, index, and size)</li>
<li>Book size</li>
</ul>
<p>If you want to check the Github of the book: <a rel="nofollow" href="https://github.com/pablo14/data-science-live-book" target="_blank">https://github.com/pablo14/data-science-live-book</a></p>
<p><em>This may not seem relevant if you are not developing writing a book, a manual or any kind of document; however, if you do start to write, then Google will probably bring you here for the answer.</em></p>
<h2>The first steps into Bookdown</h2>
<p><img alt="How to self-publish a book: Customizing Bookdown" src="https://i0.wp.com/blog.datascienceheroes.com/content/images/2018/08/Screen-Shot-2018-08-25-at-19.26.06.png?w=200&ssl=1" data-recalc-dims="1"></p>
<p>This is not an introductory post; on the contrary, for the very first step on Bookdown:</p>
<ul>
<li>The official <a rel="nofollow" href="https://bookdown.org/yihui/bookdown" target="_blank">Bookdown book</a>, (created using Bookdown):</li>
<li><a rel="nofollow" href="https://rmarkdown.rstudio.com/lesson-1.html" target="_blank"><em>What is R markdown?</em></a> by RStudio.</li>
<li><a rel="nofollow" href="https://github.com/rstudio/bookdown-demo" target="_blank">A minimum reproducible example</a> I suggest that you clone and run (and <a rel="nofollow" href="https://bookdown.org/yihui/bookdown-demo/" target="_blank">here the output</a>)</li>
</ul>
<h2>Amazon Kindle format</h2>
<p>The extension for Amazon books is &quot;.mobi&quot;, and the one delivered by Bookdown is: &quot;.epub&quot;.<br>
You have to download the converter from: <a rel="nofollow" href="https://www.amazon.com/gp/feature.html?ie=UTF8&docId=1000765211" target="_blank">https://www.amazon.com/gp/feature.html?ie=UTF8&amp;docId=1000765211</a></p>
<p>If the epub version is in the same directory as the ‘kindlegen’ program, then convert the book by typing:<br>
<code>./kindlegen my-amazing-book.epub</code><br>
This will create the new file: <code>my-amazing-book.mobi</code>.</p>
<p>I’m aware that <a rel="nofollow" href="https://calibre-ebook.com/" target="_blank">Calibre</a> can convert from epub to mobi. But just to be sure, I recommend the kindlegen from Amazon. Calibre is useful to check the ebook. It’s free.</p>
<h2>Building the book</h2>
<p>Get the script that I use to create the pdf and the html <a rel="nofollow" href="https://github.com/pablo14/data-science-live-book/blob/master/create_site_dbg.R" target="_blank">from here</a>.</p>
<p>To create the epub, I use the RStudio button (in the <code>bookdown::epub_book</code> option)<br>
<img alt="How to self-publish a book: Customizing Bookdown" src="https://i2.wp.com/blog.datascienceheroes.com/content/images/2018/08/create_ebook.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h3>bookdown.yml</h3>
<p>In this file, you can define what files to process and the order in which to display the pages.</p>
<p>The default is to run all the pages; however, if the book is big, then it will run a lot of unnecessary code. As you can see, there are two comments that I use when debugging.</p>
<p><img alt="How to self-publish a book: Customizing Bookdown" src="https://i1.wp.com/blog.datascienceheroes.com/content/images/2018/08/bookdown-yml.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>Please note the <code>new_session: yes</code>: it’s important to always look at the last run.</p>
<h2>Be mindful of the line width</h2>
<p><img alt="How to self-publish a book: Customizing Bookdown" src="https://i0.wp.com/blog.datascienceheroes.com/content/images/2018/08/machine.gif?w=300&ssl=1" data-recalc-dims="1"></p>
<p>Although we never usually think about line width, that is, how many characters fit in a single line, Amazon will not publish a book if a single character is outside of the margins of the page format you define.</p>
<p><strong>formatR</strong> to the rescue! Yet another package from Yihui… what does it do? It formats R code automatically.</p>
<p>And yes, it allows rearranging the code in order to &quot;fit&quot; (Note 1) the line width. Hurray!</p>
<p>Live application: <a rel="nofollow" href="https://yihui.shinyapps.io/formatR/" target="_blank">https://yihui.shinyapps.io/formatR/</a></p>
<p>Note 1: However, under certain conditions, it does not work as we want (but as expected). It uses the &quot;deparse&quot; R base function.</p>
<p>Without technical details, I had to do a &quot;<em>dirty-while(1)-hack</em>&quot; to the formatR package because the width of the <code>deparse</code> function doesn’t work as I needed.</p>
<p>More technical info:: <a rel="nofollow" href="https://github.com/yihui/formatR/pull/71" target="_blank">https://github.com/yihui/formatR/pull/71</a><br>
I forked formatR to work with this functionality. I cannot warranty it works in all scenarios: <a rel="nofollow" href="https://github.com/pablo14/formatR" target="_blank">https://github.com/pablo14/formatR</a></p>
<p>I spent around 30 hours fixing this - I hope it only takes you 1 minute. ðŸ�»</p>
<p><strong>Important:</strong> Check the parameters <code>width.cutoff=56</code> and <code>options(&quot;width&quot;=56)</code> from the next section.</p>
<h2>At the beginning of every ‘.Rmd’</h2>
<p>This is what I have:</p>
<pre><code>```{r include = FALSE}
if(!knitr:::is_html_output())
{
  options(&quot;width&quot;=56)
  knitr::opts_chunk$set(tidy.opts=list(width.cutoff=56, indent = 2), tidy = TRUE)
  knitr::opts_chunk$set(fig.pos = 'H')
}```
</code></pre>
<p>That will run in the case of PDF output. This sets the global options for all the chunks in the file, but we can overwrite each chunk's behavior.</p>
<p>Now, I don’t remember if the <code>options(&quot;width&quot;=56)</code> is redundant with the <code>width.cutoff=56</code>, but my maximum line width is 56 characters according to the book size.</p>
<p><code>indent = 2</code>: two spaces of indentation.</p>
<p><code>tidy=TRUE</code> puts <code>formatR</code> to work to produce a beautiful and standardized layout code. Keep in mind that I used my own forked version of <code>formatR</code>.</p>
<p>When the width adjustment doesn’t work, you have to change it by hand in the Rmd (not the md!). In this case, you have to set <code>tidy=FALSE</code> in all of the chunks that will be adjusted by hand:</p>
<pre><code>```{r, tidy=FALSE}
print(&quot;bla bla bla * 1000&quot;)```
</code></pre>
<p>For example, a .Rmd from the book:<br>
<img alt="How to self-publish a book: Customizing Bookdown" src="https://i2.wp.com/blog.datascienceheroes.com/content/images/2018/08/02_book.png?w=456&ssl=1" data-recalc-dims="1"><br>
See the <code>tidy=FALSE</code> and how each line has a max width of 56. Amazon and I can assure that ðŸ˜�.</p>
<p>Here’s the web-live version of this section: <a rel="nofollow" href="https://livebook.datascienceheroes.com/exploratory-data-analysis.html#selecting_best_vars_mic" target="_blank">https://livebook.datascienceheroes.com/exploratory-data-analysis.html#selecting_best_vars_mic</a></p>
<p>And here is the pdf version:</p>
<p><img alt="How to self-publish a book: Customizing Bookdown" src="https://i0.wp.com/blog.datascienceheroes.com/content/images/2018/08/03_book.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>The work of Bookdown and its capabilities are stunning!</p>
<h2>Some parameters in index.Rmd</h2>
<p><code>links-as-notes: true</code><br>
Use this option if you want to have a printable version of your book. It will convert all the hyperlinks to footnotes (because you cannot click the paper).</p>
<p>A nice-to-have feature: When we are referencing a chapter inside the book, the page number does not appear in the footnotes or beside the reference position. For an example, see more in the data preparation chapter (Page 54)&quot;_</p>
<p><code>linestretch: 1.15</code><br>
Use this parameter in the case where the lines in the of having the sentences in the PDF are really close. It will give some air to your paragraphs. <img alt="😉" src="https://s.w.org/images/core/emoji/11/72x72/1f609.png" class="wp-smiley"></p>
<h2>Images</h2>
<p><img alt="How to self-publish a book: Customizing Bookdown" src="https://i1.wp.com/blog.datascienceheroes.com/content/images/2018/08/hanks.gif?w=350&ssl=1" data-recalc-dims="1"></p>
<h3>Image position</h3>
<p><code>knitr::opts_chunk$set(fig.pos = 'H')</code> — Remember this line was at the beginning of the Rmd.</p>
<p>The parameter <code>fig.pos='H'</code> (plus another one) forces all the images to be in the place we create them; otherwise, they will be at the bottom of every page.</p>
<p>The other one is <code>out.extra=''</code> (double single quote), which you have to define it in each chunk.</p>
<p>Taking the same example as before:</p>
<pre><code>```{r importance-variable-ranking, fig.width=6, fig.height=4.5, 
tidy=FALSE, fig.cap=&quot;Correlation using information theory&quot;, 
out.extra=''}```
</code></pre>
<p>More info about image position: <a rel="nofollow" href="https://stackoverflow.com/questions/42486617/knitr-ignoring-fig-pos" target="_blank">https://stackoverflow.com/questions/42486617/knitr-ignoring-fig-pos</a></p>
<p>This is not necessary when you add images, just like:</p>
<pre><code>knitr::include_graphics(&quot;exploratory_data_analysis/mic_mutual_info.png&quot;)
</code></pre>
<h3>Image caption and index</h3>
<p><code>fig.cap</code>: Add the caption and an autoincrement number (1.18 in this case) to the figure as follows:</p>
<p><img alt="How to self-publish a book: Customizing Bookdown" src="https://i0.wp.com/blog.datascienceheroes.com/content/images/2018/08/03_book.png?w=456&ssl=1" data-recalc-dims="1"></p>
<h3>Image size</h3>
<p>From the last example: <code>fig.width=6</code> and <code>fig.height=4.5</code> define the size in the same way as if you were on RStudio: if you decrease it, then the numbers will be larger.<br>
The size will depend on your plot.</p>
<p>There are other ways to define size, e.g., <code>out.width</code> and <code>out.height</code> in which you can adjust the size in percentage of the screen or in pixels. Please refer to official documentation: <a rel="nofollow" href="https://bookdown.org/yihui/bookdown/figures.html" target="_blank">https://bookdown.org/yihui/bookdown/figures.html</a></p>
<h2>Wrapping long urls</h2>
<p>When citing a bibliography, a long url will be out of margin. Fix it this way:</p>
<p>In the template.tex add: <code>\Urlmuskip=0mu plus 1mu\relax</code></p>
<p>Regarding long urls in the book’s content, I couldn’t find a way to wrap them. My ugly hack: to use a Google shorten url service, for example: &quot;<code>https://goo.gl/2TrDgN</code>&quot; (which is now deprecated), so please choose another service like tinyurl.</p>
<h2>Book size</h2>
<p>I started with an A4 format because it is the default. When the book was almost ready I realized that it would weigh as much as a 7-year-old cat:</p>
<p><img alt="How to self-publish a book: Customizing Bookdown" src="https://i1.wp.com/blog.datascienceheroes.com/content/images/2018/08/05_cat.png?w=456&ssl=1" data-recalc-dims="1"></p>
<p>After checking with the Google oracle, it says that &quot;standard sizes&quot; for technical books are 6 x 9 inches or 15.24 x 22.86 cm.</p>
<p>Again, this is defined in the tempated.tex:</p>
<p><code>\usepackage[paperwidth=6in, paperheight=9in]{geometry}</code></p>
<p>You will have to check all line widths as before.</p>
<h2>Final words</h2>
<p>I really hope that I have encouraged you to write your own stories in a book. If you want a second opinion on some material you plan to publish, let me know.</p>
<p>Don’t be shy, start with a minimum html site with a few pages, or with a post in <a rel="nofollow" href="https://rpubs.com/about/getting-started" target="_blank">Rpubs</a> or <a rel="nofollow" href="https://pages.github.com/" target="_blank">Github Pages</a>. Let it grow and enjoy what you do ðŸŒ±ðŸ&quot;š.</p>
<p><img alt="How to self-publish a book: Customizing Bookdown" src="https://i1.wp.com/s3.amazonaws.com/datascienceheroes.com/img/blog/dslb_2.gif?w=300&ssl=1" data-recalc-dims="1"></p>
<hr>
<p>Thanks for reading! ðŸš€</p>
<p><a rel="nofollow" href="https://blog.datascienceheroes.com/how-to-self-publish-a-book-customizing-bookdown/blog.datascienceheroes.com" target="_blank">Blog</a></p>
<p><a rel="nofollow" href="https://www.linkedin.com/in/pcasas/" target="_blank">Linkedin</a></p>
<p><a rel="nofollow" href="https://twitter.com/pabloc_ds" target="_blank">Twitter</a></p>
<p><a rel="nofollow" href="http://livebook.datascienceheroes.com/" target="_blank">Data Science Live Book</a></p>
</div>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://blog.datascienceheroes.com/how-to-self-publish-a-book-customizing-bookdown/"> R - Data Science Heroes Blog</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 rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/OJIbgF_SOVA" height="1"></a></div>