Convert Latex to HTML online

Yesterday I wanted to convert a snippet of latex code to html. Unfortunately, the results when Googling for “latex to html online” are only cr*p. I knew a tool that was really great at converting latex files to html files. This tool is called Pandoc. However, it seemed like a bit of overhead to always create a file, store the latex snippet, remember what the pandoc command was to convert, enter the entire command (e.g. pandoc mysnippet.tex -o mysnippet.html), open the html file and get the snippet… That’s why I’d much prefer an online tool that does this chore for me. Since I couldn’t find any, I implemented it myself.

This is the result:

It’s open sourced, the code can be found on Github at https://github.com/bersling/latex2html.

It’s a small self-contained piece of code with one html file and one TypeScript file at its core.

But here’s the interesting part, if I hadn’t been so bad at googling, I would have found that there is the exact same functionality available at

… So there might be even a better learning for me than the additional coding experience I gained: Before you build something, Google, google, google if it already exists. For example had I tried googling “convert with pandoc online” this converter would have been the first hit. For me, it was a small price to pay, since I built the solution in little time and it was quite fun, but if you’re working on a larger project and didn’t research what’s already out there, you’re gonna have a bad time.

You probably don’t need Java StringBuilder – Use a templating engine instead

NoString Builder

Does your code look like somewhat like this? Do you find \n in your code? Then you are doing it wrong. Building strings is a very, very cumbersome process. You’ll miss newlines, you can’t see what’s happening in the process, it’s inefficient if you want to wrap something and so on… Overall very error prone and time consuming. As a rule of thumb: When you write something, that could go into a file (e.g. a .tex file or a .html file), then write it into a file, and not intermixed with Java, C++ or Python code!

But I want to generate my file dynamically, so how can I do it?

Templating Engines

Instead of writing derpy stuff like

String start = "\\escaping \n too much \n"
String middle = "wow... that's weird"
String end = "homg the middle forgot the \n what amigonnadu noww\n"
String full = start + middle + end;

there is a more elegant concept for generating files dynamically. It’s called Templating Engine. In a templating engine, you write your file as you would have anyways, for example:

Hello, I'm just a .txt file... Almost... I have some special syntax enabled like variables denoted by $imAVariable.

And then I just continue writing. No chance to forget a new line.

… with a few “enhancements” to the default file:

  • write variables, for example with $variableName notation in Apache Velocity Templating engine.
  • write for loops, for example with
    #foreach ($name in $myArray)
     ...some normal derpy text
    put a $name in between
    lalala
    #end
  • … and some more functionalities, depending on the power of your templating engine.

Like this you’ll never miss a \n again and your file-to-be becomes much, much, much more readable. If you’re programming in Java, the best choice for you is probably Apache Velocity Templating Engine.

After writing your template file for example helloworld.vm

Hello $name!  Welcome to Velocity!

you can use Java to fill in the gaps, as in:

/*  first, get and initialize an engine  */
    VelocityEngine ve = new VelocityEngine();
    ve.init();
/*  next, get the Template  */
    Template t = ve.getTemplate( "helloworld.vm" );
/*  create a context and add data */
    VelocityContext context = new VelocityContext();
    context.put("name", "World");
/* now render the template into a StringWriter */
    StringWriter writer = new StringWriter();
    t.merge( context, writer );
/* show the World */
    System.out.println( writer.toString() );

Well and that’s it! Files built efficiently, readable, maintainable.

Summary

Writing files via string-concatenation is bad idea since the code is non-readable, errorprone and takes forever to write. A much better idea is to use a templating engine, where you’ll write your file as usual, with the exception of the dynamic parts. Those are inserted by the templating engine later on.

How to Solve Vagrant Spikes CPU to 100% on OSX

Does this look familiar to you…? Then you’ve come to the right place.

Since one year I’ve been developing now using Vagrant and about every day I had to reload the freakin virtual machine because I computer suddenly started getting hot… I’ve been looking for answers but couldn’t find any, until I gave it another shot today. I’ve found a solution for my problem in this thread. So big shoutout for pgrunwald… Anyways, the problem seems to appear when you’re doing port forwarding.

That was my setup when I had the problem:Screen Shot 2016-05-21 at 21.18.05

That’s my setup now:

config.vm.network "private_network", ip: "192.168.3.4"

And THAT’S IT. Now you just have to vagrant reload --provision and you’re Mac won’t go crazy no longer.