<p>你基本上要求两件事:</p>
<ol>
<li><p><strong>标记单元格输出代码结果。</strong></p>
<blockquote>
<p>I'd like to count some stuff, generate some results and include them in markdown. [...] I'd like to have <strong><em>a template in markdown and insert values</em></strong> generated by the program in the notebook</p>
</blockquote></li>
<li><p><strong>代码单元格输出标记</strong></p>
<blockquote>
<p>I'd like such command: <code>print '$\phi$'</code> to generate phi symbol, just like in markdown.</p>
</blockquote></li>
</ol>
<p>从2号开始。已经被<a href="https://stackoverflow.com/questions/36288670/jupyter-notebook-output-in-markdown/36313217#36313217">another answer</a>所覆盖(基本上:使用<code>Latex()</code>或<code>Markdown()</code>从<code>IPython.display</code>导入),我将关注第一个:</p>
<hr/>
<h3>一。带插入变量的标记模板</h3>
<p>使用Jupyter扩展<a href="https://github.com/ipython-contrib/jupyter_contrib_nbextensions/tree/master/src/jupyter_contrib_nbextensions/nbextensions/python-markdown" rel="noreferrer">Python Markdown</a>实际上可以完全按照您描述的那样进行操作。</p>
<p><a href="https://github.com/ipython-contrib/jupyter_contrib_nbextensions#installation" rel="noreferrer">Installation instructions</a>可以在nbextensions的github页面上找到。确保使用<a href="https://github.com/ipython-contrib/jupyter_contrib_nbextensions#3-enablingdisabling-extensions" rel="noreferrer">jupyter command</a>或<a href="https://github.com/Jupyter-contrib/jupyter_nbextensions_configurator" rel="noreferrer">extension configurator</a>启用python标记扩展。</p>
<p>通过扩展,变量可以通过<code>{{var-name}}</code>访问。这种降价模板的示例如下:</p>
<blockquote>
<p><strong>Python Code in Markdown Cells</strong></p>
<p>The variable a is {{a}} </p>
<p>You can also embed LateX: {{b}} in here!</p>
<p>Even images can be embedded: {{i}}</p>
</blockquote>
<p>当然,所有变量或图像<code>a</code>、<code>b</code>、<code>i</code>都应该在前面的代码中设置。当然,您也可以在不使用print命令的情况下使用标记乳胶样式的表达式(如<code>$\phi$</code>)。这张图片来自扩展的wiki,展示了该功能。</p>
<p><a href="https://i.stack.imgur.com/NhDBk.png" rel="noreferrer"><img src="https://i.stack.imgur.com/NhDBk.png" alt="example from wiki"/></a></p>
<hr/>
<p>有关集成到ipython/jupyter中的此功能的更多信息,请参见<a href="https://github.com/ipython/ipython/issues/2958" rel="noreferrer">ipython</a>和<a href="https://github.com/jupyter/notebook/issues/1098" rel="noreferrer">jupyter</a>的问题跟踪程序。</p>