Scientific Writing and Publishing Anno 2013

Scientific Writing and Publishing Anno 2013

Hans Petter Langtangen

Nov 28, 2013

Challenges with tools for scientific writing

Scientific writing = LaTeX

\providecommand{\shadedskip}{}
\definecolor{shadecolor}{rgb}{0.87843, 0.95686, 1.0}
\renewenvironment{shadedskip}{
\def\FrameCommand{\colorbox{shadecolor}}\FrameRule0.6pt
\MakeFramed {\FrameRestore}\vskip3mm}{\vskip0mm\endMakeFramed}
\providecommand{\shadedquoteBlue}{}
\renewenvironment{shadedquoteBlue}[1][]{
\bgroup\rmfamily\fboxsep=0mm\relax
\begin{shadedskip}
\list{}{\parsep=-2mm\parskip=0mm\topsep=0pt\leftmargin=2mm
\rightmargin=2\leftmargin\leftmargin=4pt\relax}
\item\relax}{\endlist\end{shadedskip}\egroup}\begin{shadedquoteBlue}
\fontsize{9pt}{9pt}
\begin{Verbatim}
print 'Hello, World!'
\end{Verbatim}

Big late 1990s question: will MS Word replace LaTeX? It never did!

Scientific publishing needs to address new media

The book will probably survive

The classical report will survive

Scope of this presentation

The default answer is LaTeX, but there are many recent popular alternative tools: HTML w/MathJax, Sphinx, Markdown, MediaWiki, IPython notebook.

Does your scientific writing today need to address new media (n the future)?

Can we factor pieces from a heterogeneous world to one coherent piece in the future?

When I write some scientific material,

and later want to collect the pieces into a larger document, maybe some book, or one big web document, or a set of slides, is that at all feasible?

Probably not, but I have a solution :-)

Popular tools anno 2013 and their math support

LaTeX is very rich; other tools support much less

LaTeX is very rich; other tools support much less

Conclusion: Highly non-trivial to translate a LaTeX document into something based on HTML and vice versa.

Typesetting concerns I

Typesetting concerns II

Typesetting concerns III

Solution I: Use a format that translates to many

Solution II: Use Doconce

Doconce offers minimalistic typing, great flexibility wrt format, especially for scientific writing with much math and code.

Doconce

Doconce demos

http://hplgit.github.com/teamods/writing_reports/

A tour of Doconce

Title, authors, date, toc

TITLE: Some Title
AUTHOR: name1 at institution1, with more info, and institution2
AUTHOR: name2 email:name2@web.com at institution
DATE: today

# A table of contents is optional:
TOC: on

Notice. Title and authors must have all information on a single line!

Abstract

__Abstract.__
Here goes the abstract...

Or:

__Summary.__
Here goes the summary...

Section headings

Headings are surrounded by = signs:

========= This is an H1/chapter heading =========

======= This is an H2/section heading =======

===== This is an H3/subsection heading =====

=== This is an H4/paragraph heading ===

__This is a paragraph heading.__

Result:

This is an H1/chapter heading

This is an H2/section heading

This is an H3/subsection heading

This is an H4/paragraph heading

This is a paragraph heading.

Markup and lists

 * Bullet list items start with `*`
   and may span several lines
 * *Emphasized words* are possible
 * _Boldface words_ are also possible
 * color{red}{colored words} too
 * `inline verbatim code` is featured
   o and sublists with enumerated items starting with `o`
   o items are just indented as you would do in email

This gets rendered as

Labels, references, index items

# Insert index items in the source
idx{key word1} idx{key word2}

# Label
===== Some section =====
label{this:section}

# Make reference
As we saw in Section ref{this:section}, references, index
items and labels follow a syntax similar to LaTeX
but without backslashes.

# Make reference to equations
See \eqref{eq1}-\eqref{myeq}.

# Make hyperlink
"some link text": "https://github.com/hplgit/doconce"

# Hyperlink with complete URL as link text
URL: "https://github.com/hplgit/doconce"

Figures and movies

Important: Figures with HTML and LaTeX size info, and caption: everything on one line

FIGURE: [figdir/myfig, width=300 frac=1.2] My caption. label{fig1}

Movies are also supported:

MOVIE: [http://youtu.be/IDeGDFZSYo8, width=420 height=315]

and rendered as

Math

Inline math as in LaTeX:

...where $a=\int_{\Omega}fdx$ is an integral.

gets rendered as ...where \( a=\int_{\Omega}fdx \) is an integral.

An equation environment is surrounded by !bt and !et tags, the rest is plain LaTeX:

!bt
\begin{align}
\frac{\partial u}{\partial t} &= \nabla^2 u,
label{a:eq}\\ 
\nabla\cdot\pmb{v} & = 0
label{b:eq}
\end{align}
!et

which is rendered as

$$ \begin{align} \frac{\partial u}{\partial t} &= \nabla^2 u, \label{a:eq}\\ \nabla\cdot\pmb{v} & = 0 \label{b:eq} \end{align} $$

Math flexibility

Limit math environments to

\[ ... \]

\begin{equation*}
\end{equation*}

\begin{equation}
\end{equation}

\begin{align*}
\end{align*}

\begin{align}
\end{align}

Doconce fix of shortcomings.

Displaying code

Code is enclosed in !bc and !ec tags:

!bc pycod
def solver(I, a, T, dt, theta):
    """Solve u'=-a*u, u(0)=I, for t in (0,T] with steps of dt."""
    dt = float(dt); N = int(round(T/dt)); T = N*dt
    u = zeros(N+1); t = linspace(0, T, N+1)

    u[0] = I
    for n in range(0, N):
        u[n+1] = (1 - (1-theta)*a*dt)/(1 + theta*dt*a)*u[n]
    return u, t
!ec

This gets rendered as

def solver(I, a, T, dt, theta):
    """Solve u'=-a*u, u(0)=I, for t in (0,T] with steps of dt."""
    dt = float(dt); N = int(round(T/dt)); T = N*dt
    u = zeros(N+1); t = linspace(0, T, N+1)

    u[0] = I
    for n in range(0, N):
        u[n+1] = (1 - (1-theta)*a*dt)/(1 + theta*dt*a)*u[n]
    return u, t

Copying code from source files

We recommend to copy as much code as possible directly from the source files:

@@@CODE path/to/file
@@@CODE path/to/file   fromto: start-regex@end-regex

For example, copying a code snippet starting with def solver( and ending with (line not included) def next(x, y, is specified by start and end regular expressions:

@@@CODE src/dc_mod.py  fromto: def solver\(@def next\(x,\s*y,

Typesetting of code is implied by the file extension

Demonstrating code execution; Online Python Tutor

With !bc pyoptpro or a file *.pyopt, the code applies the Online Python Tutor for displaying program flow and state of variables:

Demonstrating code execution; Sage Cell Server

With !bc pyscpro or a file *.pysc, the code is typeset in a sage cell:

a = 2
b = 3
print 'a+b:', a + b

# In a sage cell we can also plot
from matplotlib.pyplot import *
from numpy import *
x = linspace(0, 4*pi, 101)
y = exp(-0.1*x)*cos(x)
plot(x, y)
xlabel('x'); ylabel('y')
show()

Demonstrating code execution; IPython notebook

Can take a Doconce source and transform to an IPython notebook with source

Tables

  |--------------------------------|
  |time  | velocity | acceleration |
  |---r-------r-----------r--------|
  | 0.0  | 1.4186   | -5.01        |
  | 2.0  | 1.376512 | 11.919       |
  | 4.0  | 1.1E+1   | 14.717624    |
  |--------------------------------|

Gets rendered as

time velocity acceleration
0.0 1.4186 -5.01
2.0 1.376512 11.919
4.0 1.1E+1 14.717624

Newcommands for math

Labels, citations, index, bibliography

Lables, citations, index, and bibliography follow the ideas of LaTeX, but without backslashes:

===== My Section =====
label{sec:mysec}

idx{key equation} idx{$\u$ conservation}

We refer to Section ref{sec:yoursec} for background material on
the *key equation*. Here we focus on the extension


!bt
\begin{equation}
\Ddt{\u} = \mycommand{v} label{mysec:eq:Dudt}
\end{equation}
!et
Equation \eqref{mysec:eq:Dudt} is important, see
cite{Larsen_et_al_2002,Johnson_Friedman_2010a}.
Also, cite{Miller_2000} supports such a view.

Figure ref{mysec:fig:myfig} displays the features.

FIGURE: [fig/myfile, width=600] My figure. label{mysec:fig:myfig}

===== References =====

BIBFILE: papers.pub

The papers.pub file must be in Publish format (easy to make from BibTeX).

Exercises

Doconce offers a special format for exercises, problems, projects, and examples:

===== Problem: Flip a Coin =====
label{demo:ex:1}
files=flip_coin.py, flip_coin.pdf
solutions=mysol.txt, mysol_flip_coin.py
keywords = random numbers; Monte Carlo simulation

!bsubex
Make a program that simulates flipping a coin $N$ times.

!bhint
Use `r = random.random()` and define head as `r <= 0.5`.
!ehint
!esubex

!bsubex
Compute the probability of getting heads.

!bans
0.5.
!eans
!esubex

Rendering of the previous page

Problem 1: Flip a Coin

a) Make a program that simulates flipping a coin \( N \) times.

Hint. Use r = random.random() and define head as r <= 0.5.

b) Compute the probability of getting heads.

Answer. 0.5.

Filenames: flip_coin.py, flip_coin.pdf.

Exercises

All exercises, problems, and projects in a document are parsed and available in a data structure (list of dicts) for further processing (e.g., making a book of problems).

[{'answer': '',
  'closing_remarks': '',
  'file': ['flip_coin.py', 'flip_coin.pdf'],
  'hints': [],
  'keywords': ['random numbers', 'Monte Carlo simulation'],
  'label': 'demo:ex:1',
  'solution_file': ['mysol.txt', 'mysol_flip_coin.py'],
  'subex': [{'answer': '',
             'file': None,
             'hints': ['Use `r = random.random()` ...'],
             'solution': '',
             'text': 'Make a program that simulates ...'},],
  'title': 'Flip a Coin',
  'type': 'Problem'}]

Use of preprocessors

Doconce admonitions

Use with caution! Such environments may light up the document, but can be disturbing too.

Going deeper. More details can be separated from the rest.

Time for review!

Conclusions.

Slides

Very effective way to generate slides from running text:

Example on slide code

!split
======= Headline =======

 * Key point 1
 * Key point 2
 * Key point 3: Although long
   bullet points are not recommended in general, we need
   it here for demonstration purposes to investigate
   what happens with the slide layout where there is
   so much text under one point

FIGURE: [fig/teacher1, width=100 frac=0.5]

Key equation:

!bt
\[ -\nabla^2 u = f \quad\hbox{in }\Omega \]
!et

And maybe a final comment?

!split
======= Next slide... =======

Example on slide code

Last page gets rendered to

Headline

Key equation:

$$ -\nabla^2 u = f \quad\hbox{in }\Omega $$

And maybe a final comment?

Grid layout of slide: MxN cells

Example with a bullet list to the left and a figure to the right (two cells: 00 and 01):

!split
======= Headline =======

!bslidecell 00
!bpop
 * Key point 1
 * Key point 2
 * Key point 3
!epop

!bpop
!bt
\[ -\nabla^2 u = f \quad\hbox{in }\Omega \]
!et
!epop

!eslidecell

!bslidecell 01
FIGURE: [fig/broken_pen_and_paper, width=400, frac=0.8]
!eslidecell

!split
======= Next slide... =======

Grid layout of slide: MxN cells

Last page gets rendered to

Headline

$$ -\nabla^2 u = f \quad\hbox{in }\Omega $$

Slide styles

Output in HTML

Run in terminal window:

doconce format html doconcefile

# Solarized HTML style
doconce format html doconcefile --html_solarized

# Control pygments typesetting of code
doconce format html doconcefile --pygments_html_style=native

# Or use plain <pre> tag for code
doconce format html doconcefile --no_pygments_html

# Further making of slides
doconce slides_html doconcefile reveal --html_slide_theme=darkgray

Output for blog posts

Two formats of blog posts are supported:

For wordpress, add --wordpress:

doconce format html doconcefile --wordpress

and paste the code into the text area.

Output in pdfLaTeX

doconce format pdflatex doconcefile

# Result: doconcefile.p.tex (ptex2tex file)
# Run either
ptex2tex doconcefile
# or
doconce ptex2tex doconcefile -DHELVETICA envir=minted

pdflatex doconcefile
bibtex doconcefile
pdflatex doconcefile

# More control of how code is typeset
doconce format pdflatex doconcefile --minted_latex_style=trac
doconce ptex2tex doconcefile envir=minted

doconce format pdflatex doconcefile
doconce ptex2tex doconcefile envir=ans:nt

Output in Sphinx

doconce format sphinx doconcefile

# Autocreate sphinx directory
doconce sphinx_dir theme=pyramid doconcefile

# Copy files and build HTML document
python automake-sphinx.py

google-chrome sphinx-rootdir/_build/html/index.html

Much easier than running the Sphinx tools manually!

Output for wiki

Only MediaWiki supports math.

doconce format mwiki doconcefile

Recommended site:

Publishing of "official" documents:

Output in other formats

doconce format pandoc doconcefile  # (Pandoc extended) Markdown
doconce format gwiki  doconcefile  # Googlecode wiki
doconce format cwiki  doconcefile  # Creole wiki (Bitbucket)
doconce format rst    doconcefile  # reStructuredText
doconce format plain  doconcefile  # plain, untagged text for email

Installation

Writing tips for LaTeX writers who want to convert to Doconce

Doconce writing tips

Figures:

Doconce writing tips

Writing tips for sphinx and other formats

For output formats different from latex, pdflatex, and html: