title_body: Software recommendation for writing ebook and exporting to multiple formats
upvoted_answer: Pandoc is your friend here.  
  
  Pandoc can take your input file (LaTeX, markdown, other) and transform it to PDF, EPUB, HTML (and many more).
  
  Pandoc is cross-platform and free under the GPL, although donations are appreciated.
  
  With regards to formatting your code, both LaTeX and markdown have components that will help.  LaTeX has many more layout options.
  
  I've used both LaTeX and markdown to produce ebook (ePub) files.  An example of using pandoc with LaTeX is shown in another ebooks.stackexchange.com question I posted.   
  
  It's great to have a range of options.