How to show math equations with DDoc?

84 views Asked by At

I'd like to use math equations in documentation using DDoc. What's a good way to make it look good?

Something like:

/**
 * Returns x + sqrt(2).
 */
double add_sqrttwo(double x) {
    return x + sqrt(2);
}
3

There are 3 answers

0
Richard Andrew Cattermole On

I'm afraid DDOC itself cannot do this. You will have to use e.g. Javascript to accomplish this or pre-generate them as images.

0
Adam D. Ruppe On

If you are targeting HTML, there's a few options, though none are ddoc per se: you can link to an image with the formula (<img src="...">), you can put tags around it and process later (whether a post processor in your generation step or a Javascript library on the web), or you can try to write MathML in the document https://developer.mozilla.org/en-US/docs/Web/MathML/Authoring ...

You might also try using various unicode characters like returns X + √2 but that's also pretty limited.

But all of these are working around ddoc, not with it. ddoc just can't do it on its own.

3
johan On

You can import MathJax, and then put the math in-between \( \) or \[ \].

An example:

/**
* Macros:
* DDOC =
* <!DOCTYPE html>
* <html lang="en-US">
* <head>
* <script type="text/javascript" async src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"></script>
* <title>$(TITLE)</title>
* </head>
* <body><h1>$(TITLE)</h1>$(BODY)</body>
* </html>
*
*/

module example;

import std.math;

/**
 * Jay! \( a + \sqrt{2} \)
 *
 * You must prefix parameter names with an underscore: \( _x + \sqrt{2} \).
 * 
 */
double add_sqrttwo(double x) {
    return x + sqrt(2.0);
}