// Path: latex-template/src/main/java/de/vsfexperts/latex/template/tools/LatexEscapeTool.java
// public class LatexEscapeTool {
// 
// 	public String escape(final String input) {
// 		if (input == null) {
// 			return null;
// 		}
// 
// 		String output = input;
// 
// 		// replace 1st, otherwise the substitution of \ will be applied twice (to other substitutions)
// 		output = output.replace("\\", "\\textbackslash ");
// 
// 		output = output.replace("€", "\\euro ");
// 		output = output.replace("<", "\\textless ");
// 		output = output.replace(">", "\\textgreater ");
// 		output = output.replace("§", "\\S ");
// 
// 		// LaTeX special characters
// 		output = output.replace("&", "\\&");
// 		output = output.replace("%", "\\%");
// 		output = output.replace("$", "\\$");
// 		output = output.replace("#", "\\#");
// 		output = output.replace("_", "\\_");
// 		output = output.replace("{", "\\{");
// 		output = output.replace("}", "\\}");
// 		output = output.replace("~", "\\textasciitilde ");
// 		output = output.replace("^", "\\textasciicircum ");
// 
// 		return output;
// 	}
// }


import static java.util.Objects.requireNonNull;
import static org.apache.velocity.runtime.RuntimeConstants.INPUT_ENCODING;
import static org.apache.velocity.runtime.RuntimeConstants.OUTPUT_ENCODING;
import static org.apache.velocity.runtime.RuntimeConstants.RESOURCE_LOADER;
import static org.apache.velocity.runtime.RuntimeConstants.RUNTIME_LOG_LOGSYSTEM;
import java.io.StringWriter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.log.CommonsLogLogChute;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.velocity.tools.generic.MathTool;
import de.vsfexperts.latex.template.tools.LatexEscapeTool;
		final VelocityContext velocityContext = prepareContext(context);

		return evaluateTemplate(velocityContext, velocityEngine);
	}

	private final VelocityEngine initializeEngine() {
		final VelocityEngine engine = new VelocityEngine();
		engine.setProperty(INPUT_ENCODING, "UTF-8");
		engine.setProperty(OUTPUT_ENCODING, "UTF-8");
		engine.setProperty(RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());

		customize(engine);
		engine.init();

		return engine;
	}

	protected void customize(final VelocityEngine engine) {
		engine.setProperty(RESOURCE_LOADER, "classpath");
		engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
	}

	private final VelocityContext prepareContext(final LatexContext context) {
		final VelocityContext velocityContext = new VelocityContext(context.asMap());
		customize(velocityContext);

		return velocityContext;
	}

	protected void customize(final VelocityContext velocityContext) {
// Path: latex-template/src/main/java/de/vsfexperts/latex/template/tools/LatexEscapeTool.java
// public class LatexEscapeTool {
// 
// 	public String escape(final String input) {
// 		if (input == null) {
// 			return null;
// 		}
// 
// 		String output = input;
// 
// 		// replace 1st, otherwise the substitution of \ will be applied twice (to other substitutions)
// 		output = output.replace("\\", "\\textbackslash ");
// 
// 		output = output.replace("€", "\\euro ");
// 		output = output.replace("<", "\\textless ");
// 		output = output.replace(">", "\\textgreater ");
// 		output = output.replace("§", "\\S ");
// 
// 		// LaTeX special characters
// 		output = output.replace("&", "\\&");
// 		output = output.replace("%", "\\%");
// 		output = output.replace("$", "\\$");
// 		output = output.replace("#", "\\#");
// 		output = output.replace("_", "\\_");
// 		output = output.replace("{", "\\{");
// 		output = output.replace("}", "\\}");
// 		output = output.replace("~", "\\textasciitilde ");
// 		output = output.replace("^", "\\textasciicircum ");
// 
// 		return output;
// 	}
// }


// Path: latex-template/src/main/java/de/vsfexperts/latex/template/LatexTemplate.java
import static java.util.Objects.requireNonNull;
import static org.apache.velocity.runtime.RuntimeConstants.INPUT_ENCODING;
import static org.apache.velocity.runtime.RuntimeConstants.OUTPUT_ENCODING;
import static org.apache.velocity.runtime.RuntimeConstants.RESOURCE_LOADER;
import static org.apache.velocity.runtime.RuntimeConstants.RUNTIME_LOG_LOGSYSTEM;
import java.io.StringWriter;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.log.CommonsLogLogChute;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.apache.velocity.tools.generic.MathTool;
import de.vsfexperts.latex.template.tools.LatexEscapeTool;

		final VelocityContext velocityContext = prepareContext(context);

		return evaluateTemplate(velocityContext, velocityEngine);
	}

	private final VelocityEngine initializeEngine() {
		final VelocityEngine engine = new VelocityEngine();
		engine.setProperty(INPUT_ENCODING, "UTF-8");
		engine.setProperty(OUTPUT_ENCODING, "UTF-8");
		engine.setProperty(RUNTIME_LOG_LOGSYSTEM, new CommonsLogLogChute());

		customize(engine);
		engine.init();

		return engine;
	}

	protected void customize(final VelocityEngine engine) {
		engine.setProperty(RESOURCE_LOADER, "classpath");
		engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
	}

	private final VelocityContext prepareContext(final LatexContext context) {
		final VelocityContext velocityContext = new VelocityContext(context.asMap());
		customize(velocityContext);

		return velocityContext;
	}

	protected void customize(final VelocityContext velocityContext) {
		velocityContext.put("e", new LatexEscapeTool());