I tried embedding a formula in Javadoc

Introduction

I've been using Doxygen for many years because I wanted to embed formulas in Java API documentation, but I tried it because Javadoc also seems to be able to embed formulas using MathJax.

environment

POM settings

With Apache Maven Javadoc Plugin, if you add MathJax CDN to the header, you should be able to embed formulas in Javadoc API documentation, but an error message saying that JavaScript cannot be embedded in comments. Apparently it has become stricter since Java 8. Therefore, specifying --allow-script-in-comments as an option with ``` `` does not work. Upon further investigation, Javadoc Plugin 3.0.0 changed the option specification from ```` to ````. By the way, I added a Doclet that can use Markdown so that Markdown can be used. The final POM file settings are as follows. (`` ... </ plugins> </ build> )

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-javadoc-plugin</artifactId>
    <version>3.0.0</version>
    <configuration>
        <additionalOptions>
            <additionalOption>--allow-script-in-comments</additionalOption>
        </additionalOptions>
    <header>
        <![CDATA[
        <script type="text/javascript"
        src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
        </script>
        ]]>
    </header>
    <doclet>ch.raffael.mddoclet.MarkdownDoclet</doclet>
    <docletArtifact>
        <groupId>ch.raffael.markdown-doclet</groupId>
        <artifactId>markdown-doclet</artifactId>
        <version>1.4</version>
    </docletArtifact>
    <useStandardDocletOptions>true</useStandardDocletOptions>

Formula sample

As a test, I embedded the following formula sample in javadoc.

/**
 * 
 * @author hoge
 * 
 * MathJax Samples
 * ===
 * 
 * This javadoc API document is configured to use MathJax's CommonHTML mode with
 * web fonts to display the equations, which produces uniform layout and 
 * typesetting across browsers. But MathJax can also be configured to use 
 * HTML-CSS (for legacy browsers), SVG, and native MathML rendering when 
 * available in a browser. You can try the various output modes using the 
 * MathJax context Menu (which you access by ctrl+clicking / alt-clicking an 
 * equation) or the button below. 
 * 
 * - The Quadratic Formula
 * $$ x = {-b \pm \sqrt{b^2-4ac} \over 2a} $$
 * 
 * - Cauchy's Integral Formula
 * 
 * $$ f(a) = \frac{1}{2\pi i} \oint\frac{f(z)}{z-a}dz $$
 * 
 * - Double angle formula for Cosines
 * 
 * $$ \cos(\theta+\phi)=\cos(\theta)\cos(\phi)−\sin(\theta)\sin(\phi) $$
 * 
 * 
 */
public class Main {
    
}

The figure below is an API document automatically generated from the above Javadoc. The formula is displayed neatly. I was able to confirm that I could embed formulas in Javadoc without having to bother to use Doxygen.

Summary

I've been using Doxygen to embed formulas in API documentation, but I've confirmed that Javadoc can handle pretty formulas as well. Javadoc is a standard tool that generates HTML-format API specifications from Java source code, so it is a great advantage to be able to migrate from Doxygen to Javadoc. Doxygen used the Javadoc style, so migration doesn't seem to be that difficult either. This time, the newly introduced Doclet that can use Markdown can also automatically generate UML, so it is expected that the expression of API documents will be more diverse.

Reference material

Recommended Posts

I tried embedding a formula in Javadoc
I tried embedding a formula in Javadoc
I tried a calendar problem in Ruby
I tried to make a login function in Java
I tried metaprogramming in Java
I tried to convert a string to a LocalDate type in Java
I tried to implement a buggy web application in Kotlin
I tried to make a client of RESAS-API in Java
I created a PDF in Java.
I tried a little digdag docker.run_options
I tried putting Domino11 in CentOS7
I tried to create a simple map app in Android Studio
I tried to write code like a type declaration in Ruby
I tried using Elasticsearch API in Java
I tried the new era in Java
I tried playing with BottomNavigationView a little ①
I tried to build a Firebase application development environment with Docker in 2020
I tried to make a talk application in Java using AI "A3RT"
I made a primality test program in Java
I tried to easily put CentOS-7 in a PC that I no longer need
[For beginners] I tried using DBUnit in Eclipse
I tried to implement polymorphic related in Nogizaka.
[For beginners] I tried using JUnit 5 in Eclipse
I tried to organize the session in Rails
java I tried to break a simple block
[Rails / JavaScript / Ajax] I tried to create a like function in two ways.
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I tried to implement deep learning in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
I made a rock-paper-scissors game in Java (CLI)
I tried to create a LINE clone app
I made a Ruby extension library in C
I tried Spring.
I tried tomcat
I tried to output multiplication table in Java
I tried youtubeDataApi.
I tried running Ansible on a Docker container
I tried refactoring ①
I tried to build Micra mackerel in 1 hour!
I tried setting up a Maven remote repository
I tried to develop an application in 2 languages
I tried to create Alexa skill in Java
I wrote a prime factorization program in Java
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)
I tried running Java on a Mac terminal
I tried JHipster 5.1
I tried Firebase Local Emulator Suite in Flutter
I stumbled when I tried using neo4j in the jenv environment, so make a note
I made a simple calculation problem game in Java
I tried Mastodon's Toot and Streaming API in Java
I tried to organize the cases used in programming
[Swift] I implemented a Twitter timeline in UITableView (Part 2)
I tried to implement Firebase push notification in Java
I tried to develop a ramen shop sharing website.
I tried using Google Cloud Vision API in Java
I tried to decorate the simple calendar a little
# 2 [Note] I tried to calculate multiplication tables in Java.