LaTeX has a package for nomenclatures (see documentation) that allows a user to issue a simple command
\nomenclature{symbol}{definition}
to add a symbol and its definition to a nomenclature. The
command \printglossary inserts the nomenclature table in the document.
We can easily create someting similar in DocOnce with the aid of Mako. Here are the basic ideas:
.nomenclature.do.txt.${insert_nomenclature()} to make a fresh
.nomenclature.do.txt file and return a @@@CODE command
for inserting the nomenclature table.${nomenclature(symbol, definition)}
for adding a new line in the nomenclature table in the file.${end_nomenclature()} to finish the nomenclature
table, i.e., insert the last table line.
<%
# Nomenclature functionality
def insert_nomenclature():
# Make new file
with open('.nomenclature.do.txt', 'w') as f:
f.write("""\
|---------------------------------|
| symbol | definition |
|---l---------l-------------------|
""")
# Use envir=None to make plain include
return '@@@CODE .nomenclature.do.txt envir=None'
def nomenclature(symbol, definition):
with open('.nomenclature.do.txt', 'a') as f:
f.write('| ' + symbol + ' | ' + definition + ' |\n')
return ''
def end_nomenclature():
with open('.nomenclature.do.txt', 'a') as f:
f.write('|---------------------------------|\n')
return ''
%>
The typical application in a DocOnce document is
TITLE: ...
AUTHOR: ...
DATE: today
!split
TOC: on
!split
${insert_nomenclature()}
# Here comes a lot of text...
The equation becomes
!bt
\[ \nabla\cdot\sigma = \varrho f,\]
!et
where $\sigma$ is the stress tensor, $\varrho$ is the density,
and $f$ is a body force.
${nomenclature(r'$\sigma$', 'stress tensor')}
${nomenclature(r'$\varrho$', 'density')}
${nomenclature(r'$f$', 'body force')}
## More text....
## At the end:
${end_nomenclature()}
Make it a habit to use raw strings r"..."
for the symbol in ${nomenclature(r"...", ....
(sometimes backslashes enter the definition too and raw strings are
required).
Here is
the resulting DocOnce after mako is run (found in tmp_mako__mydoc.do.txt if
mydoc.do.txt is the file above):
TITLE: Test
AUTHOR: HPL
DATE: today
!split
TOC: on
!split
@@@CODE .nomenclature.do.txt envir=None
# Here comes a lot of text...
The equation becomes
!bt
\[ \nabla\cdot\sigma = \varrho f,\]
!et
where $\sigma$ is the stress tensor, $\varrho$ is the density,
and $f$ is a body force.
The file .nomenclature.do.txt reads
|---------------------------------|
| symbol | definition |
|---l---------l-------------------|
| $\sigma$ | stress tensor |
| $\varrho$ | density |
| $f$ | body force |
|---------------------------------|
Here is an example where we want to illustrate how to calculate a
double integral. All mathematics is to be done in SymPy, and results
are supposed to be embedded in the document's text.
We can include Python code to be executed, anywhere in the document,
and a variable var in the Python code is reached by ${var}.
Here, we run SymPy and use SymPy's LaTeX converter to make LaTeX
code out of computed SymPy expressions:
# Execute Python code
<%
import sympy as sm
x, y, a = sm.symbols('x y a')
f = a*x + sm.sin(y)
step1 = sm.Integral(f, x, y)
step2 = sm.Integral(sm.Integralf, x).doit(), y)
step3 = step2.doit()
%>
# Make use of results in the above block when writing LaTeX math
!bt
\begin{align*}
${sm.latex(step1)} &= ${sm.latex(step2)}\\
&= ${sm.latex(step3)}
\end{align*}
!et
The result of the LaTeX block above, after Mako is run, becomes
\begin{align*}
\iint a x + y^{2} \sin{\left (y \right )}\, dx\, dy &=
\int \frac{a x^{2}}{2} + x y^{2} \sin{\left (y \right )}\, dy\\
&= \frac{a y}{2} x^{2} + x \left(- y^{2} \cos{\left (y \right )} +
2 y \sin{\left (y \right )} + 2 \cos{\left (y \right )}\right)
\end{align*}
Debugging Python code in Mako is less convenient than debugging Python files directly, so one may prefer to just include the Python code that Mako is supposed to run by
<%
# #include "src/ex1.py"
%>
This is the recommended way to make use of SymPy to automate the mathematical derivations: first develop and test the SymPy code files, include the files in the document inside Mako's Python code environment.
Remark. Executing Python code inside the DocOnce document is closely related to literate programming. Tools supporting creating a document while running a Python programming cover Pweave, Python literate, PyWebTool, Antiweb, Literate Programming in Python, Pyreport, and also IPython notebooks.