{
 "metadata":{},
 "nbformat":4,
 "nbformat_minor":4,
 "cells":[
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- jupyter-book 02_01_testdoc.ipynb -->\n",
    "# Subsection 1\n",
    "\n",
    "<div id=\"subsec1\"></div>\n",
    "\n",
    "<!-- Refer to section/appendix etc. at the beginning of the line -->\n",
    "<!-- and other special fix situations for HTML. -->\n",
    "\n",
    "More text, with a reference back to\n",
    "the section [Section 1](02_testdoc.html#sec1) and [Subsection 1](subsec1.html#subsec1), and further to the\n",
    "the sections [Subsection 1](subsec1.html#subsec1) and [URLs](02_22_testdoc.html#subsubsec:ex), which\n",
    "encourages you to do the tasks in [Problem: Flip a Coin](demo:ex:1.html#demo:ex:1) and [Exercise: Make references to projects and problems](06_01_testdoc.html#exer:some:formula).\n",
    " [Appendix: Just for testing; part I](#app1) and [Appendix: Just for testing; part II](#app2) are also nice elements.\n",
    "\n",
    "### Test Section reference at beginning of line and after a sentence\n",
    "\n",
    "The section [Subsection 1](subsec1.html#subsec1) is fine.\n",
    "The section [URLs](02_22_testdoc.html#subsubsec:ex) too.\n",
    "\n",
    "<!-- sphinx code-blocks: pycod=python cod=fortran cppcod=c++ sys=console -->\n",
    "\n",
    "### Computer code\n",
    "\n",
    "Let's do some copying from files too. First from subroutine up to the very end,"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "              subroutine test()\n",
    "              integer i\n",
    "              real*8 r\n",
    "              r = 0\n",
    "              do i = 1, i\n",
    "                 r = r + i\n",
    "              end do\n",
    "              return\n",
    "        C     END1\n",
    "        \n",
    "              program testme\n",
    "              call test()\n",
    "              return\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "and then just the subroutine,"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        \n",
    "              subroutine test()\n",
    "              integer i\n",
    "              real*8 r\n",
    "              r = 0\n",
    "              do i = 1, i\n",
    "                 r = r + i\n",
    "              end do\n",
    "              return\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "and finally the complete file with a plain text verbatim environment\n",
    "(`envir=ccq`):"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        C     a comment\n",
    "        \n",
    "              subroutine test()\n",
    "              integer i\n",
    "              real*8 r\n",
    "              r = 0\n",
    "              do i = 1, i\n",
    "                 r = r + i\n",
    "              end do\n",
    "              return\n",
    "        C     END1\n",
    "        \n",
    "              program testme\n",
    "              call test()\n",
    "              return\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Testing other code environments. First Python:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        !bc pycod\n",
    "        def f(x):\n",
    "            return x+1\n",
    "        !ec\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "which gets rendered as"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "```Python\n",
    "        def f(x):\n",
    "            return x+1\n",
    "```"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Test paragraph and subsubsection headings before\n",
    "before code.\n",
    "\n",
    "**Paragraph heading before code.**"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "```Python\n",
    "        import sys\n",
    "        sys.path.insert(0, os.pardir)\n",
    "```"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "### Subsubsection heading before code"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "```Python\n",
    "        def h(z):\n",
    "            return z+1\n",
    "```"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Here is a program that is supposed to be interactive via\n",
    "Python Online Tutorial, but that service is not accessible\n",
    "for the present format."
   ]
  },
  {
   "cell_type":"code",
   "execution_count":1,
   "metadata":{
    "collapsed":false,
    "editable":true
   },
   "outputs":[],
   "source":[
    "class Line:\n",
    "    def __init__(self, a, b):\n",
    "        self.a, self.b = a, b\n",
    "\n",
    "    def __call__(self, x):\n",
    "        a, b = self.a, self.b\n",
    "        return a*x + b\n",
    "\n",
    "line = Line(2, 1)\n",
    "y = line(x=3)\n",
    "print(y)"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Some more Python code (actually specified as a sage cell, but\n",
    "such cells are not supported by this format)."
   ]
  },
  {
   "cell_type":"code",
   "execution_count":2,
   "metadata":{
    "collapsed":false,
    "editable":true
   },
   "outputs":[],
   "source":[
    "%matplotlib inline\n",
    "\n",
    "a = 2\n",
    "b = 3\n",
    "print('a+b:', a + b)\n",
    "\n",
    "# In a sage cell we can also plot\n",
    "from matplotlib.pyplot import *\n",
    "from numpy import *\n",
    "x = linspace(0, 4*pi, 101)\n",
    "y = exp(-0.1*x)*cos(x)\n",
    "plot(x, y)\n",
    "xlabel('x'); ylabel('y')\n",
    "show()"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Then Cython (with -h option so it is hidden in html/sphinx):"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        cpdef f(double x):\n",
    "            return x + 1\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Standard Python shell sessions:"
   ]
  },
  {
   "cell_type":"code",
   "execution_count":3,
   "metadata":{
    "collapsed":false,
    "editable":true
   },
   "outputs":[],
   "source":[
    "from numpy import linspace, sin\n",
    "# Some comment\n",
    "x = linspace(0, 2, 11)\n",
    "y = sin(x)\n",
    "y[0]"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Similar IPython sessions:"
   ]
  },
  {
   "cell_type":"code",
   "execution_count":4,
   "metadata":{
    "collapsed":false,
    "editable":true
   },
   "outputs":[],
   "source":[
    "from numpy import linspace, sin\n",
    "# Some comment\n",
    "x = linspace(0, 2, 11)\n",
    "y = sin(x)\n",
    "y[0]"
   ]
  },
  {
   "cell_type":"code",
   "execution_count":5,
   "metadata":{
    "collapsed":false,
    "editable":true
   },
   "outputs":[],
   "source":[
    "import matplotlib.pyplot as plt\n",
    "plt.plot(x, y)\n",
    "a='multiple-\\nline\\noutput'\n",
    "a"
   ]
  },
  {
   "cell_type":"code",
   "execution_count":6,
   "metadata":{
    "collapsed":false,
    "editable":true
   },
   "outputs":[],
   "source":[
    "print(a)"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Here is the interactive session again, but with `pyshell-t`."
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "```Python\n",
    "        >>> from numpy import linspace, sin\n",
    "        >>> # Some comment\n",
    "        >>> x = linspace(0, 2, 11)\n",
    "        >>> y = sin(x)\n",
    "        >>> y[0]\n",
    "        0\n",
    "        >>> import matplotlib.pyplot as plt\n",
    "        >>> plt.plot(x, y)\n",
    "```"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- This one tests a + sign before a code environment -->\n",
    "C++:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "```Cpp\n",
    "        #include <iostream>\n",
    "        \n",
    "        int main()\n",
    "        {\n",
    "           std::cout << \"Sample output\" << std::endl;\n",
    "           return 0\n",
    "        }\n",
    "```"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- The next should get correctly typset in sphinx (cod is fcod) -->\n",
    "<!-- It also tests emoji before code -->\n",
    "And a little bit of Fortran: :dizzy_face:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        !bc cod\n",
    "              subroutine midpt(x, length, a, b)\n",
    "              real*8 a, b, x\n",
    "              x = (a + b)/2\n",
    "              length = b - a\n",
    "              return\n",
    "              end\n",
    "        !ec\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "which then is typeset as"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "```Python\n",
    "        subroutine midpt(x, length, a, b)\n",
    "              real*8 a, b, x\n",
    "              x = (a + b)/2\n",
    "              length = b - a\n",
    "              return\n",
    "              end\n",
    "```"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "HTML:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        <table>\n",
    "        <tr><td>Column 1</td><td>Column 2</td></tr>\n",
    "        <tr><td>0.67526 </td><td>0.92871 </td></tr>\n",
    "        <!-- comment -->\n",
    "        </table>\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "But inline HTML code is also important, like text that starts with\n",
    "`<a href=\"` (which can destroy the following text if not properly\n",
    "quoted).\n",
    "\n",
    "Matlab with comments requires special typesetting:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        % Comment on the beginning of the line can be escaped by %%\n",
    "        if a > b\n",
    "          % Indented comment needs this trick\n",
    "          c = a + b\n",
    "        end\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "And here is a system call:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "```\n",
    "        Terminal> mkdir test\n",
    "        Terminal> cd test\n",
    "        Terminal> myprog -f\n",
    "        output1\n",
    "        output2\n",
    "```"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Any valid pygments lexer/language name can appear to, e.g.,"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        !bc restructuredtext\n",
    "        =======\n",
    "        Heading\n",
    "        =======\n",
    "        \n",
    "        Some text.\n",
    "        !ec\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "results in"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        =======\n",
    "        Heading\n",
    "        =======\n",
    "        \n",
    "        Some text.\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "<!-- Here goes hidden code. -->\n",
    "<!-- Python can be treated by some formats, Fortran is always out. -->"
   ]
  },
  {
   "cell_type":"code",
   "execution_count":7,
   "metadata":{
    "collapsed":true,
    "editable":true
   },
   "outputs":[],
   "source":[
    "def f(x):\n",
    "    \"\"\"Secret, hidden function to be defined.\"\"\"\n",
    "    return 42"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "Finally, `!bc do` supports highlighting of DocOnce source:"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        ======= DocOnce test file =======\n",
    "        \n",
    "        ===== Computer code =====\n",
    "        \n",
    "        Inline verbatim code, as in `import numpy as np`, is allowed, as well as\n",
    "        code blocks:\n",
    "        \n",
    "        !bc pycod\n",
    "        from math import sin\n",
    "        \n",
    "        def f(x):\n",
    "            \"\"\"Example on a function.\"\"\"\n",
    "            return sin(x) + 1\n",
    "        \n",
    "        print(f(0))\n",
    "        !ec\n",
    "        \n",
    "        \n",
    "        ===== Mathematics =====\n",
    "        \n",
    "        Formulas can be inline, as in $\\nabla\\cdot\\boldsymbol{u} = 0$, or typeset\n",
    "        as equations:\n",
    "        \n",
    "        !bt\n",
    "        \\begin{align*}\n",
    "        \\nabla\\cdot\\boldsymbol{u} &= 0,\\\\\n",
    "        \\boldsymbol{u} &= \\nabla\\phi .\n",
    "        \\end{align*}\n",
    "        !et\n",
    "        \n",
    "        === Subsubsection heading ===\n",
    "        \n",
    "        DocOnce files can have chapters, sections, subsections, and subsubsections.\n",
    "        \n",
    "        __Paragraph heading.__ Paragraphs may have headings.\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "It is time to test `verbatim inline font` especially with `a newline\n",
    "inside the text` and an exclamation mark at the end: `BEGIN`! For\n",
    "spellcheck, test `a verbatim expression` in `another` in a `third`.\n",
    "Also test exclamation mark as in `!bc` and `!ec` as well as `a != b`.\n",
    "Also test backslashes and braces like `\\begin`, `\\begin{enumerate}`,\n",
    "`\\end{this}\\end{that}`, and `{something \\inside braces}`.\n",
    "\n",
    "The following attempt to exemplify colored text does not work in\n",
    "format pandoc.\n",
    "Here is some <font color=\"red\">red color</font> and an attempt to write <font color=\"green\">with\n",
    "green color containing a linebreak\n",
    "code.</font> Some formats will only display \n",
    "this correctly when `html` \n",
    "is the output format.\n",
    "But here some more running text is added which is not part of\n",
    "the previous blocks with line breaks.\n",
    "\n",
    "### Running OS commands"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "        Terminal> python -c 'print(\"Testing\\noutput\\nfrom\\nPython.\")'\n",
    "        Testing\n",
    "        output\n",
    "        from\n",
    "        Python.\n"
   ]
  },
  {
   "cell_type":"markdown",
   "metadata":{
    "editable":true
   },
   "source":[
    "### Footnotes\n",
    "\n",
    "Here is a test of footnotes [^footnote], which are handy in text.\n",
    "They are used in different flavors, now in\n",
    "\n",
    " * list items (note below that footnotes work after math, verbatim, and URLs - bin fact old and emphasize too!)\n",
    "\n",
    " * even with math $\\nabla^2u$[^math1]\n",
    "\n",
    " * and code `h[i] += 1`[^code]\n",
    "   (*must* have space between inline code and footnote!)\n",
    "\n",
    " * and [links](https://google.com)[^google-search]\n",
    "\n",
    "which gives flexibility in writing.\n",
    "This is the third[^example-of-the-third-footnote] example.\n",
    "\n",
    "  [^footnote]: Typesetting of the footnote depends on the format.\n",
    "Plain text does nothing, LaTeX removes the\n",
    "definition and inserts the footnote as part of the LaTeX text.\n",
    "reStructuredText and Sphinx employ a similar type of typesetting\n",
    "as Extended Markdown and DocOnce, and in HTML we keep the same\n",
    "syntax, just displayed properly in HTML.\n",
    "[^math1]: Math footnotes can be dangerous since it\n",
    "interferes with an exponent.\n",
    "[^code]: One-line footnote.\n",
    "\n",
    "[^google-search]: <google.com> is perhaps the most famous\n",
    "web site today.\n",
    "\n",
    "Here is some more text before a new definition of a footnote that was\n",
    "used above.\n",
    "\n",
    "**Non-breaking space character.**\n",
    "\n",
    "This paragraph aims to test [non-breaking space character](https://en.wikipedia.org/wiki/Non-breaking_space), and a typical\n",
    "example where this is needed is in physical units: 7.4 km is traveled\n",
    "in $7.4/5.5\\approx 1.345$ s.  Also check that a~[link](https://google.com) is\n",
    "not broken across lines (drag the browser window to test this).\n",
    "(On the other hand, the tilde is used in\n",
    "computer code, e.g., as in `[~x for x in y]` or in `y=~x`, and should\n",
    "of course remain a tilde in those contexts.)"
   ]
  }
 ]
}