Answers.Body: <p>It is possible to use Markdown to do that, but it is not in my experience, the best option. Reason for this is that what you should really do is incorporate the code from from real programs that can be tested using some (unit-)test harness. 
  That way there is far less chance that the code you include in your book contains some error that would go unnoticed because it was never run.</p>
  
  <p>markdown is however kept very simple and does not support inclusions of code from somewhere else. </p>
  
  <ul>
  <li>There are ways around that limitation using <code>pandoc</code>, as it allows merging of files as it allows merging of files, but for including code snippets of a few lines at a time that is a bit too course to my liking, and you loose the overview in your markdown text where goes what. </li>
  <li>There are other add-ons for markdown to <a href="http://assemble.io/docs/Markdown.html" rel="nofollow">assemble</a> that keep the include information within the file. You would need to somehow generate the snippets of code to include in your text in separate files and include them.</li>
  </ul>
  
  <p>If you have anything more than a few snippets you should look at the <a href="http://docutils.sourceforge.net/rst.html" rel="nofollow">reStructuredText format</a>, and especially its use within <a href="http://sphinx-doc.org/" rel="nofollow">sphinx</a>. It was particularly written to document source code. Originally for Python but also supporting other programming languages. It has native support for the including/assembling. </p>
  
  <p>Sphinx has build in support for epub and pdf generation, the latter without having to go through LaTeX. With <code>pandoc</code>, you would have to use LaTeX which is not always trivial to set up, depending on your OS.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-02-14T12:28:02.653Z
Answers.Text: rST looks interesting. The code in the book won't change very often so I won't need to include source files. I updated the question. For the last point, is there a neat way to do this in rST?
Answers.UserDisplayName: Sourabh
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-02-14T05:56:52.257Z
Answers.LastActivityDate: 2014-02-14T06:14:36.713Z
Answers.LastEditDate: 2014-02-14T06:14:36.713Z
Answers.UserDisplayName: Anthon
Answers.Body: <p>Leanpub might be a solution. See <a href="https://leanpub.com/help/manual#leanpub-auto-code" rel="nofollow">this section</a> (html) of their manual on code snippets. If you have a lot of code you can put it in an external file and reference it. Leanpub uses markdown and a dropbox folder you share with Leanpub for each book. It generates pdf, epub and mobi so it seems perfect for what you want to do. Their Syntax highligher, <a href="http://pygments.org/languages" rel="nofollow">pygments</a> supports prolog.</p>
  
  <p>The only downside I have found is the way it handles images. The images have to be at least 300PPI (pixels per inch) so unless you want your book to be huge you have to limit how many images you use. The images look terrible if they are less than 300 PPI.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-02-19T17:02:50.047Z
Answers.LastActivityDate: 2014-02-19T17:02:50.047Z
Answers.UserDisplayName: rosswd
Answers.Body: <p>If you don't have to include long blocks of code, maybe the simplest way is to use some form of styled html/xhtml where you will define that code text will use a fixed width font, that you usually should include. Starting from html will make subsequent conversion very easy.</p>
  
  <p>If you have to include long blocks of code, maybe there are better solutions, but I'm not very helpful here.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-02-14T08:37:05.540Z
Answers.LastActivityDate: 2014-02-14T08:37:05.540Z
Answers.UserDisplayName: Sekhemty
Answers.Body: <p>You can use LaTeX. Packages like <a href="http://www.ctan.org/pkg/listings" rel="nofollow">listings</a> or <a href="http://www.ctan.org/pkg/minted" rel="nofollow">minted</a> can be used to include code snippets and syntax highlighting. Both are supported by <code>tex4ht</code>, so conversion to html and then epub is possible. And as bonus, you can get perfect PDF file.</p>
  
  <p>If you are interested, I have a sample document (in Czech language, but I hope the code is readable) where packages <code>minted</code> for syntax highlighting, <code>biblatex</code> for bibliographies, and <code>glossaries</code> for acronyms were used: <a href="https://github.com/michal-h21/abcclanek/blob/master/abcclanek.tex" rel="nofollow">TeX source</a> and <a href="http://michal-h21.github.io/abcclanek/abcclanek.html" rel="nofollow">generated html</a>. For conversion, <a href="https://github.com/michal-h21/make4ht" rel="nofollow">make4ht</a> was used. <code>make4ht</code> is frontend for <code>tex4ht</code> which allow you to use your own <a href="https://github.com/michal-h21/abcclanek/blob/master/abcclanek.mk4" rel="nofollow">build scripts</a> and it has some filters for generated html cleanup. </p>
  
  <p>There is also another tool, <a href="https://github.com/michal-h21/tex4ebook" rel="nofollow">tex4ebook</a>, based on <code>make4ht</code>, which can be used to convert <code>LaTeX</code> to <code>epub</code>, <code>epub3</code> and <code>mobi</code> formats. Note that I am the author of <code>make4ht</code> and <code>tex4ebook</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-02-14T15:23:14.330Z
Answers.LastActivityDate: 2014-02-14T15:23:14.330Z
Answers.UserDisplayName: michal.h21
Answers.Body: <p>Embedding code in EPUB can be accomplished with pure HTML and CSS. The <code>&lt;code&gt;</code> tag and <code>monospace</code> attribute will bring you most of the way. See <a href="https://github.com/oreillymedia/HTMLBook" rel="noreferrer">https://github.com/oreillymedia/HTMLBook</a> for a lot of detail.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-02-21T00:41:47.140Z
Answers.Text: For backwards compatibility with older readers, <pre><code>stuff here</code></pre> might be a good idea - if I recall, some older devices don't like the <code> tag, also font selection through CSS might fail for various reasons.
Answers.UserDisplayName: Hazzit
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-02-18T16:50:59.120Z
Answers.LastActivityDate: 2014-02-19T09:39:18.647Z
Answers.LastEditDate: 2014-02-19T09:39:18.647Z
Answers.UserDisplayName: user386
Body: <p>What's a good format for making an ebook that contains code. Is markdown a good option or is there something better available? I may want to convert it to ePub/Mobi and PDF later.</p>
  
  <p>Is it a good idea to add custom styles to markdown files? Would these styles play well with softwares like pandoc?</p>
  
  <p>What other options do I have?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>The book is about prolog and will contain mainly these types of structure:</p>
  
  <ul>
  <li>Headings- h1, h2, h3 ...</li>
  <li>Paragraphs</li>
  <li>Tables (<strong>without</strong> headers)</li>
  <li>Indented Text</li>
  <li>Code Snippets</li>
  <li>Big example code blocks which may contain text and code or just code (ranging from 15 to +100 lines). I need these parts to look different from code snippets.</li>
  </ul>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-02-14T05:55:46.173Z
Comments.Text: Do you mean source code in some programming language? Or e.g. encrypted data.
Comments.UserDisplayName: Anthon
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-02-14T11:13:00.337Z
Comments.Text: I mean Source code
Comments.UserDisplayName: Sourabh
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2014-02-18T09:55:39.953Z
Comments.Text: Markdown -> pandoc -> Sigil seem to be a decent pipeline. Stylesheets can be added by pandoc or manually later.
Comments.UserDisplayName: Raphael
Tags: <conversion><markdown>
Title: Format for ebook that contains code
UserDisplayName: Sourabh
ThreadText: # Format for ebook that contains code
  
  (Asked by: username_0 on 2014-02-14)
  
  What's a good format for making an ebook that contains code. Is markdown a good option or is there something better available? I may want to convert it to ePub/Mobi and PDF later.
  
  Is it a good idea to add custom styles to markdown files? Would these styles play well with softwares like pandoc?
  
  What other options do I have?
  
  **Edit**
  
  The book is about prolog and will contain mainly these types of structure:
  
  * Headings- h1, h2, h3 ...
  * Paragraphs
  * Tables (**without** headers)
  * Indented Text
  * Code Snippets
  * Big example code blocks which may contain text and code or just code (ranging from 15 to +100 lines). I need these parts to look different from code snippets.
  
  **Tags:** conversion, markdown
  
  > **Comments:**
  > **username_1**: Do you mean source code in some programming language? Or e.g. encrypted data.  
  > **username_0**: I mean Source code  
  > **username_2**: Markdown -> pandoc -> Sigil seem to be a decent pipeline. Stylesheets can be added by pandoc or manually later.  
  
  ---
  
  ## 5 answers
  
  ### Answer by username_6 on 2014-02-18. Score: 8 | Accepted answer
  
  Embedding code in EPUB can be accomplished with pure HTML and CSS. The  `tag and monospace attribute will bring you most of the way. See https://github.com/oreillymedia/HTMLBook for a lot of detail.`
  
  > **Comments:**
  > **username_7**: For backwards compatibility with older readers, <pre><code>stuff here</code></pre> might be a good idea - if I recall, some older devices don't like the <code> tag, also font selection through CSS might fail for various reasons.  
  
  ---
  
  ### Answer by username_1 on 2014-02-14. Score: 1
  
  It is possible to use Markdown to do that, but it is not in my experience, the best option. Reason for this is that what you should really do is incorporate the code from from real programs that can be tested using some (unit-)test harness.
  That way there is far less chance that the code you include in your book contains some error that would go unnoticed because it was never run.
  
  markdown is however kept very simple and does not support inclusions of code from somewhere else.
  
  * There are ways around that limitation using `pandoc`, as it allows merging of files as it allows merging of files, but for including code snippets of a few lines at a time that is a bit too course to my liking, and you loose the overview in your markdown text where goes what.
  * There are other add-ons for markdown to [assemble](http://assemble.io/docs/Markdown.html) that keep the include information within the file. You would need to somehow generate the snippets of code to include in your text in separate files and include them.
  
  If you have anything more than a few snippets you should look at the [reStructuredText format](http://docutils.sourceforge.net/rst.html), and especially its use within [sphinx](http://sphinx-doc.org/). It was particularly written to document source code. Originally for Python but also supporting other programming languages. It has native support for the including/assembling.
  
  Sphinx has build in support for epub and pdf generation, the latter without having to go through LaTeX. With `pandoc`, you would have to use LaTeX which is not always trivial to set up, depending on your OS.
  
  > **Comments:**
  > **username_0**: rST looks interesting. The code in the book won't change very often so I won't need to include source files. I updated the question. For the last point, is there a neat way to do this in rST?  
  
  ---
  
  ### Answer by username_3 on 2014-02-19. Score: 1
  
  Leanpub might be a solution. See [this section](https://leanpub.com/help/manual#leanpub-auto-code) (html) of their manual on code snippets. If you have a lot of code you can put it in an external file and reference it. Leanpub uses markdown and a dropbox folder you share with Leanpub for each book. It generates pdf, epub and mobi so it seems perfect for what you want to do. Their Syntax highligher, [pygments](http://pygments.org/languages) supports prolog.
  
  The only downside I have found is the way it handles images. The images have to be at least 300PPI (pixels per inch) so unless you want your book to be huge you have to limit how many images you use. The images look terrible if they are less than 300 PPI.
  
  
  ---
  
  ### Answer by username_4 on 2014-02-14. Score: 1
  
  If you don't have to include long blocks of code, maybe the simplest way is to use some form of styled html/xhtml where you will define that code text will use a fixed width font, that you usually should include. Starting from html will make subsequent conversion very easy.
  
  If you have to include long blocks of code, maybe there are better solutions, but I'm not very helpful here.
  
  
  ---
  
  ### Answer by username_5 on 2014-02-14. Score: 1
  
  You can use LaTeX. Packages like [listings](http://www.ctan.org/pkg/listings) or [minted](http://www.ctan.org/pkg/minted) can be used to include code snippets and syntax highlighting. Both are supported by `tex4ht`, so conversion to html and then epub is possible. And as bonus, you can get perfect PDF file.
  
  If you are interested, I have a sample document (in Czech language, but I hope the code is readable) where packages `minted` for syntax highlighting, `biblatex` for bibliographies, and `glossaries` for acronyms were used: [TeX source](https://github.com/michal-h21/abcclanek/blob/master/abcclanek.tex) and [generated html](http://michal-h21.github.io/abcclanek/abcclanek.html). For conversion, [make4ht](https://github.com/michal-h21/make4ht) was used. `make4ht` is frontend for `tex4ht` which allow you to use your own [build scripts](https://github.com/michal-h21/abcclanek/blob/master/abcclanek.mk4) and it has some filters for generated html cleanup.
  
  There is also another tool, [tex4ebook](https://github.com/michal-h21/tex4ebook), based on `make4ht`, which can be used to convert `LaTeX` to `epub`, `epub3` and `mobi` formats. Note that I am the author of `make4ht` and `tex4ebook`.
  
  
  ---
  
  