title: Difficult workflow writing Latex book full of Python code
body: <p>I'm writing a book on coding in python using Latex. I plan on having a lot of text with python code interspersed throughout, along with its output. What's really giving me trouble is when I need to go back and edit my python code, it's a huge pain to get it back nicely into my latest document.</p>
  
  <p>I've done a whole lot of research and can't seem to find a good solution.</p>
  
  <p>This one includes full files as one, doesn't solve my issues
  <a href="https://tex.stackexchange.com/questions/289385/workflow-for-including-jupyter-aka-ipython-notebooks-as-pages-in-a-latex-docum">https://tex.stackexchange.com/questions/289385/workflow-for-including-jupyter-aka-ipython-notebooks-as-pages-in-a-latex-docum</a> </p>
  
  <p>Same with this one.
  <a href="http://blog.juliusschulz.de/blog/ultimate-ipython-notebook" rel="nofollow noreferrer">http://blog.juliusschulz.de/blog/ultimate-ipython-notebook</a></p>
  
  <p><strong>Found Solution 1 (awful)</strong></p>
  
  <p>I can copy and paste python code into latex ok using the listings latex package. </p>
  
  <p>Pros:</p>
  
  <ol>
  <li>Easy to update only small section of code.</li>
  </ol>
  
  <p>Cons:</p>
  
  <ol>
  <li>For output need to run in python, copy, paste separately.</li>
  <li>Initial writing SLOW, need to do this process hundreds of times per chapter.</li>
  </ol>
  
  <p><strong>Found Solution 2 (bad)</strong></p>
  
  <p>Use jupyter notebook with markdown, export to Latex, \include file into main Latex document.</p>
  
  <p>Pros:</p>
  
  <ol>
  <li>Streamlined</li>
  <li>Has output contained within.</li>
  </ol>
  
  <p>Cons:</p>
  
  <ul>
  <li>To make small changes, need to reimport whole document, any changes made to markdown text within Latex editor are not saved</li>
  <li>Renaming a single variable in python after jupyter notebook could take hours.</li>
  <li>Editing seems like a giant chore.</li>
  </ul>
  
  <p><strong>Ideal solution</strong></p>
  
  <ul>
  <li>Write Text in Latex</li>
  <li>Write python in jupyter notebook, export to latex. </li>
  <li>Somehow include code snippets (small sections of the exported file) into <em>different parts</em> of the main latex book. <strong>This is the part I can't figure out</strong></li>
  <li>When python changes are needed, changes in jupyter, then re-export as latex file with same name </li>
  <li>Latex book is automatically updated from includes.</li>
  </ul>
  
  <p>The key here is that the exported python notebook is being split up and sent to different parts of the document. In order for that to work it needs to somehow be tagged or marked in the markdown or code of the notebook, so when I re-export it those same parts get sent to the same spots in the book.</p>
  
  <p>Pros:</p>
  
  <ol>
  <li>Python edits easy, easily propagated back to book.</li>
  <li>Text written in latex, can use power of latex</li>
  </ol>
  
  <p>Any help in coming up with a solution closer to my ideal solution would be much appreciated. It's killing me.</p>
  
  <p>Probably doesn't matter, but I'm coding both latex and jupyter notebooks in VS Code. I'm open to changing tools if it means solving these problems.</p>