Group Code Blocks into Tabs =========================== Here is an example showing grouping code blocks into three tabs. Example ------- Let's implement :math:`a+b`. We first show instructions, then demonstrate the codes. .. raw:: html
pythonnumpycpython
.. raw:: html
You need to have python installed .. raw:: latex \diilbookstyleinputcell .. code:: python a = [1,1,1] b = [2,2,2] [ia+ib for ia, ib in zip(a,b)] .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [3, 3, 3] .. raw:: html
.. raw:: html
You can install numpy by .. raw:: latex \diilbookstyleinputcell .. code:: bash pip install numpy .. raw:: latex \diilbookstyleinputcell .. code:: python import numpy as np a = np.ones(3) b = np.ones(3)*2 a + b .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([3., 3., 3.]) .. raw:: html
.. raw:: html
Please install cpython .. raw:: latex \diilbookstyleinputcell .. code:: python # Just a place holder print(1+2) .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output 3 .. raw:: html
.. raw:: html
Next let's implement :math:`a - b` .. raw:: html
pythonnumpy
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = [1,1,1] b = [2,2,2] [ia-ib for ia, ib in zip(a,b)] .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output [-1, -1, -1] .. raw:: html
.. raw:: html
.. raw:: latex \diilbookstyleinputcell .. code:: python a = np.ones(3) b = np.ones(3)*2 a - b .. raw:: latex \diilbookstyleoutputcell .. parsed-literal:: :class: output array([-1., -1., -1.]) .. raw:: html
.. raw:: html
Usages ------ To enable multi-tabs, first configure the ``tabs`` entry in the ``config.ini`` file. For example, here we use ``tabs = python, numpy, cpython``. ``python`` is the default tab. To specify a code block that doesn't belong to the default tab, add ``#@tab``, followed by the tab name (case insensitive), in the first line of the code block. Sometimes these codes blocks conflict with each others. We can activate one tab at a time, so only code blocks belong to this tab can be evaluated in Jupyter. For example .. raw:: latex \diilbookstyleinputcell .. code:: bash d2lbook activate default user/code_tabs.md # activate the default tab d2lbook activate numpy user/code_tabs.md # activate the numpy tab d2lbook activate all user/code_tabs.md # activate all tabs